/** Check what happens when the delegate store fails with null return. */
  @Test
  public void testUpdateFailsWithException() {
    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(searchRequest1);

    deletegateStore.update(searchRequest1);
    mockController.setThrowable(new RuntimeException("testUpdateFailsWithException"));

    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(null);

    mockController.replay();

    // put a value in the cache for the test.
    cachingStore.getSearchRequest(searchRequest1.getId());

    try {
      cachingStore.update(searchRequest1);
      fail("This exception should be thrown.");
    } catch (RuntimeException expected) {
    }

    // this call should not be cached.
    assertNull(cachingStore.getSearchRequest(searchRequest1.getId()));

    mockController.verify();
  }
  /** Test the we can't get a particular request when it does exist. We don't */
  @Test
  public void testGetSearchRequestRuntimeException() {
    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setThrowable(new RuntimeException());
    deletegateStore.getSearchRequest(searchRequest2.getId());
    mockController.setReturnValue(searchRequest2);
    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(searchRequest1);

    mockController.replay();

    try {
      cachingStore.getSearchRequest(searchRequest1.getId());
      fail("Expecting an exception to be thrown by the store.");
    } catch (RuntimeException expected) {
      // ignored.
    }

    // this should call through because it is not in the cache.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));

    // this should call through because it is not in the cache.
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));

    // these calls should be cached.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));

    mockController.verify();
  }
  /** Check what happens when the portal page is not created by the store.. */
  @Test
  public void testCreateException() {
    deletegateStore.create(searchRequest1);
    mockController.setThrowable(new RuntimeException("testCreateException"));

    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(searchRequest1);

    mockController.replay();

    try {
      cachingStore.create(searchRequest1);
      fail("Expecting an exception to be thrown by the store.");
    } catch (RuntimeException expected) {

    }

    // this should not be cached.
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));

    // this should now be cached.
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));

    mockController.verify();
  }
  /** Make sure we can delete a search request that is in the cache. */
  @Test
  public void testDeleteInCache() {
    deletegateStore.getAllOwnedSearchRequests(adminUser);
    mockController.setReturnValue(EasyList.build(searchRequest1, searchRequest2));

    deletegateStore.delete(searchRequest2.getId());

    deletegateStore.getSearchRequest(searchRequest2.getId());
    mockController.setReturnValue(searchRequest2);

    mockController.replay();

    // prime the cache.
    cachingStore.getAllOwnedSearchRequests(adminUser);

    // execute the tests.
    cachingStore.delete(searchRequest2.getId());

    // these should be cached.
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));
    assertEqualsNotSame(
        EasyList.build(searchRequest1), cachingStore.getAllOwnedSearchRequests(adminUser));

    // this should not be cached.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));

    mockController.verify();
  }
 @Override
 public void writeTo(StreamOutput out) throws IOException {
   super.writeTo(out);
   out.writeVInt(requests.size());
   for (SearchRequest request : requests) {
     request.writeTo(out);
   }
 }
 @Override
 public void readFrom(StreamInput in) throws IOException {
   super.readFrom(in);
   int size = in.readVInt();
   for (int i = 0; i < size; i++) {
     SearchRequest request = new SearchRequest();
     request.readFrom(in);
     requests.add(request);
   }
 }
  /** Make sure we can delete a search request that is not in the cache. */
  @Test
  public void testDeleteNotInCache() {
    deletegateStore.delete(searchRequest4.getId());

    deletegateStore.getSearchRequest(searchRequest4.getId());
    mockController.setReturnValue(searchRequest4);

    mockController.replay();

    cachingStore.delete(searchRequest4.getId());

    // this should not be cached.
    assertEqualsNotSame(searchRequest4, cachingStore.getSearchRequest(searchRequest4.getId()));
  }
  /** Test the update when the page is not in the cache. */
  @Test
  public void testUpdateNotInCache() {
    deletegateStore.update(searchRequest1);
    mockController.setReturnValue(searchRequest3);

    mockController.replay();

    // add the search request. Should now be in the cache.
    assertEqualsNotSame(searchRequest3, cachingStore.update(searchRequest1));

    // all of these calls should be cached.
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));

    mockController.verify();
  }
Пример #9
0
  public SearchResponse search(SearchRequest request) {
    SolrQuery query = new SolrQuery();
    query.setRows(request.getLimit());
    query.setStart(request.getOffset() * request.getLimit());
    BooleanQuery aggregate = new BooleanQuery();
    if (!StringUtils.isBlank(request.getText())) {
      TermQuery termQuery = new TermQuery(new Term("", request.getText()));
      aggregate.add(termQuery, BooleanClause.Occur.SHOULD);
    }
    if (!StringUtils.isBlank(request.getEventId())) {
      aggregate.add(
          new TermQuery(new Term("eventid", request.getEventId())), BooleanClause.Occur.MUST);
    }
    query.setQuery(aggregate.toString());

    log.info("QUERY IS: " + query.toString());
    try {
      QueryResponse queryResponse = solrServer.query(query);
      log.info("RESPONSE FROM QUERY WAS: " + queryResponse);
      SolrDocumentList results = queryResponse.getResults();
      log.info("RESULTS WAS: " + results);
      ArrayList<SearchResponse.Hit> hits = new ArrayList<SearchResponse.Hit>();
      for (SolrDocument result : results) {
        hits.add(
            new SearchResponse.Hit(
                ObjectType.session, String.valueOf(result.getFieldValue("id")), null));
      }
      return new SearchResponse(results.getNumFound(), queryResponse.getElapsedTime(), hits);
    } catch (SolrServerException e) {
      e.printStackTrace();
    }
    return null;
  }
  /** Make sure that the adjust favourite count works when entity is in the cache. */
  @Test
  public void testAdjustFavouriteCountInCache() {
    deletegateStore.adjustFavouriteCount(searchRequest2.getId(), 10);
    mockController.setReturnValue(searchRequest2);

    mockController.replay();

    // adjust the search request.
    assertEqualsNotSame(
        searchRequest2, cachingStore.adjustFavouriteCount(searchRequest2.getId(), 10));

    // all of these calls should be cached.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));

    mockController.verify();
  }
Пример #11
0
  public SearchResponse search(String index, String type, String query, Integer size, String field)
      throws IOException {
    SearchRequest.Builder builder = SearchRequest.builder().setQuery(query).setSize(size);
    if (field != null) {
      builder.setFields(field);
    }
    SearchRequest request = builder.build();

    return search(index, type, request);
  }
Пример #12
0
  public SearchResponse search(String index, String type, SearchRequest searchRequest)
      throws IOException {
    logger.debug("search [{}]/[{}], request [{}]", index, type, searchRequest);

    String path = "/";

    if (index != null) {
      path += index + "/";
    }
    if (type != null) {
      path += type + "/";
    }

    path += "_search";

    Map<String, String> params = new HashMap<>();
    if (searchRequest.getQuery() != null) {
      params.put("q", searchRequest.getQuery());
    }
    if (searchRequest.getFields() != null) {
      // If we never set elasticsearch behavior, it's time to do so
      if (FIELDS == null) {
        setElasticsearchBehavior();
      }
      params.put(FIELDS, String.join(",", (CharSequence[]) searchRequest.getFields()));
    }
    if (searchRequest.getSize() != null) {
      params.put("size", searchRequest.getSize().toString());
    }
    Response restResponse = client.performRequest("GET", path, params);
    SearchResponse searchResponse = JsonUtil.deserialize(restResponse, SearchResponse.class);

    logger.trace("search response: {}", searchResponse);
    return searchResponse;
  }
  /** Check that we can cache the owned search. */
  @Test
  public void testGetAllOwnedSearchRequest() {
    final List expectedRequests = EasyList.build(searchRequest1, searchRequest2, searchRequest3);

    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(searchRequest1);

    deletegateStore.getSearchRequest(searchRequest4.getId());
    mockController.setReturnValue(searchRequest4);

    deletegateStore.getAllOwnedSearchRequests(adminUser);
    mockController.setReturnValue(expectedRequests);

    mockController.replay();

    // put a value in the cache for the test.
    cachingStore.getSearchRequest(searchRequest1.getId());
    cachingStore.getSearchRequest(searchRequest4.getId());

    // this first call should not be cached and should delegate to getAllOwnedSearchRequests.
    assertEqualsNotSame(expectedRequests, cachingStore.getAllOwnedSearchRequests(adminUser));

    // these calls will now be cached.
    assertEqualsNotSame(expectedRequests, cachingStore.getAllOwnedSearchRequests(adminUser));
    assertEqualsNotSame(expectedRequests, cachingStore.getAllOwnedSearchRequests(adminUser));
    assertEqualsNotSame(expectedRequests, cachingStore.getAllOwnedSearchRequests(adminUser));

    // these should not also be cached.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));

    mockController.verify();
  }
  /**
   * Make sure that the adjust favourite count works when wrapped store throws runtime exception.
   */
  @Test
  public void testAdjustFavouriteFailWithRuntimeException() {
    deletegateStore.getSearchRequest(searchRequest3.getId());
    mockController.setReturnValue(searchRequest3);

    deletegateStore.adjustFavouriteCount(searchRequest3.getId(), Integer.MAX_VALUE);
    mockController.setThrowable(
        new RuntimeException("testAdjustFavouriteFailWithRuntimeException"));

    deletegateStore.getSearchRequest(searchRequest3.getId());
    mockController.setReturnValue(searchRequest3);

    mockController.replay();

    // prime the cache for the test.
    deletegateStore.getSearchRequest(searchRequest3.getId());

    // adjust should fail.
    try {
      cachingStore.adjustFavouriteCount(searchRequest3.getId(), Integer.MAX_VALUE);
      fail("Exception should have been thrown.");
    } catch (Exception expected) {

    }

    // this call should not be cached.
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));

    mockController.verify();
  }
  /** Check what happens when the portal page is not created by the store.. */
  @Test
  public void testCreateNullReturn() {
    deletegateStore.create(searchRequest1);
    mockController.setReturnValue(null);

    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(searchRequest1);

    mockController.replay();

    // add the search request. Should not be in he cache because of failure.
    assertNull(cachingStore.create(searchRequest1));

    // this call goes to the cache.
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));

    // these should be cached.
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));
    assertEqualsNotSame(searchRequest1, cachingStore.getSearchRequest(searchRequest1.getId()));

    mockController.verify();
  }
  /** Check what happens when the delegate store fails with null return. */
  @Test
  public void testUpdateFailsWithNull() {
    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(searchRequest1);

    deletegateStore.update(searchRequest1);
    mockController.setReturnValue(null);

    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(null);

    mockController.replay();

    // put a value in the cache for the test.
    cachingStore.getSearchRequest(searchRequest1.getId());

    // this call should fail.
    assertNull(cachingStore.update(searchRequest1));

    // this call should not be cached.
    assertNull(cachingStore.getSearchRequest(searchRequest1.getId()));

    mockController.verify();
  }
Пример #17
0
  public static void main(String argv[]) throws Exception {
    RetsSession session = new RetsSession("http://www.dis.com:6103/rets/login");

    if (!session.Login("Joe", "Schmoe")) {
      System.out.println("Invalid login");
      System.exit(2);
    }

    System.out.println("Action: " + session.GetAction());
    RetsVersion version = session.GetDetectedRetsVersion();

    System.out.println("RETS Version: " + ((version == RetsVersion.RETS_1_5) ? "1.5" : "1.0"));

    SearchRequest searchRequest =
        session.CreateSearchRequest("Property", "RES", "(ListPrice=300000-)");

    searchRequest.SetSelect("ListingID,ListPrice,Beds,City");
    searchRequest.SetLimit(SearchRequest.LIMIT_DEFAULT);
    searchRequest.SetOffset(SearchRequest.OFFSET_NONE);
    searchRequest.SetCountType(SearchRequest.CountType.RECORD_COUNT_AND_RESULTS);
    searchRequest.SetStandardNames(true);
    searchRequest.SetRestrictedIndicator("XXXX");
    searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT);
    SearchResultSet results = session.Search(searchRequest);

    System.out.println("Record count: " + results.GetCount());

    StringVector columns = null;

    while (results.HasNext()) {
      if (columns == null) {
        columns = results.GetColumns();
      }
      for (int i = 0; i < columns.size(); i++) {
        System.out.format("%15s: %s\n", columns.get(i), results.GetString(columns.get(i)));
      }
      System.out.println();
    }

    LogoutResponse logout = session.Logout();

    System.out.println("Billing info: " + logout.GetBillingInfo());
    System.out.println("Logout Message:  " + logout.GetLogoutMessage());
    System.out.println("Connect time: " + logout.GetConnectTime());
  }
  @Override
  protected void doExecute(SearchRequest searchRequest, ActionListener<SearchResponse> listener) {
    // optimize search type for cases where there is only one shard group to search on
    if (optimizeSingleShard) {
      try {
        ClusterState clusterState = clusterService.state();
        String[] concreteIndices =
            indexNameExpressionResolver.concreteIndices(clusterState, searchRequest);
        Map<String, Set<String>> routingMap =
            indexNameExpressionResolver.resolveSearchRouting(
                clusterState, searchRequest.routing(), searchRequest.indices());
        int shardCount =
            clusterService
                .operationRouting()
                .searchShardsCount(clusterState, concreteIndices, routingMap);
        if (shardCount == 1) {
          // if we only have one group, then we always want Q_A_F, no need for DFS, and no need to
          // do THEN since we hit one shard
          searchRequest.searchType(QUERY_AND_FETCH);
        }
      } catch (IndexNotFoundException | IndexClosedException e) {
        // ignore these failures, we will notify the search response if its really the case from the
        // actual action
      } catch (Exception e) {
        logger.debug("failed to optimize search type, continue as normal", e);
      }
    }

    if (searchRequest.searchType() == DFS_QUERY_THEN_FETCH) {
      dfsQueryThenFetchAction.execute(searchRequest, listener);
    } else if (searchRequest.searchType() == SearchType.QUERY_THEN_FETCH) {
      queryThenFetchAction.execute(searchRequest, listener);
    } else if (searchRequest.searchType() == SearchType.DFS_QUERY_AND_FETCH) {
      dfsQueryAndFetchAction.execute(searchRequest, listener);
    } else if (searchRequest.searchType() == SearchType.QUERY_AND_FETCH) {
      queryAndFetchAction.execute(searchRequest, listener);
    } else {
      throw new IllegalStateException("Unknown search type: [" + searchRequest.searchType() + "]");
    }
  }
 /** Releases shard targets that are not used in the docsIdsToLoad. */
 protected void releaseIrrelevantSearchContexts(
     AtomicArray<? extends QuerySearchResultProvider> queryResults,
     AtomicArray<IntArrayList> docIdsToLoad) {
   if (docIdsToLoad == null) {
     return;
   }
   // we only release search context that we did not fetch from if we are not scrolling
   if (request.scroll() == null) {
     for (AtomicArray.Entry<? extends QuerySearchResultProvider> entry : queryResults.asList()) {
       QuerySearchResult queryResult = entry.value.queryResult();
       if (queryResult.hasHits()
           && docIdsToLoad.get(entry.index)
               == null) { // but none of them made it to the global top docs
         try {
           DiscoveryNode node =
               nodeIdToDiscoveryNode.apply(entry.value.queryResult().shardTarget().nodeId());
           sendReleaseSearchContext(entry.value.queryResult().id(), node);
         } catch (Exception e) {
           logger.trace("failed to release context", e);
         }
       }
     }
   }
 }
  /** Check what happens when try to save a search whose user has changed. */
  @Test
  public void testUpdateChangedUserName() {
    SearchRequest expectedRequest = new SearchRequest(searchRequest1);
    expectedRequest.setOwnerUserName(fredUser.getName());

    deletegateStore.getAllOwnedSearchRequests(adminUser);
    mockController.setReturnValue(EasyList.build(searchRequest1, searchRequest2, searchRequest3));

    deletegateStore.getAllOwnedSearchRequests(fredUser);
    mockController.setReturnValue(EasyList.build(searchRequest4));

    deletegateStore.update(expectedRequest);
    mockController.setReturnValue(expectedRequest);

    mockController.replay();

    // put a value in the cache for the test.
    cachingStore.getAllOwnedSearchRequests(adminUser);
    cachingStore.getAllOwnedSearchRequests(fredUser);

    // this call should work.
    assertEqualsNotSame(expectedRequest, cachingStore.update(expectedRequest));

    // all of these calls should be cache.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));
    assertEqualsNotSame(searchRequest4, cachingStore.getSearchRequest(searchRequest4.getId()));
    assertEqualsNotSame(expectedRequest, cachingStore.getSearchRequest(expectedRequest.getId()));

    assertEqualsNotSame(
        EasyList.build(searchRequest2, searchRequest3),
        cachingStore.getAllOwnedSearchRequests(adminUser));
    assertEqualsNotSame(
        EasyList.build(expectedRequest, searchRequest4),
        cachingStore.getAllOwnedSearchRequests(fredUser));

    mockController.verify();
  }
  /** Make sure that the adjust favourite count works when wrapped store returns null. */
  @Test
  public void testAdjustFavouriteFailWithNull() {
    deletegateStore.getSearchRequest(searchRequest3.getId());
    mockController.setReturnValue(searchRequest3);

    deletegateStore.adjustFavouriteCount(searchRequest3.getId(), Integer.MAX_VALUE);
    mockController.setReturnValue(null);

    deletegateStore.getSearchRequest(searchRequest3.getId());
    mockController.setReturnValue(searchRequest3);

    mockController.replay();

    // prime the cache for the test.
    deletegateStore.getSearchRequest(searchRequest3.getId());

    // adjust should fail.
    assertNull(cachingStore.adjustFavouriteCount(searchRequest3.getId(), Integer.MAX_VALUE));

    // this call should not be cached.
    assertEqualsNotSame(searchRequest3, cachingStore.getSearchRequest(searchRequest3.getId()));

    mockController.verify();
  }
Пример #22
0
 /**
  * The associator only returns a SearchRequest, so this is a wrapper function to return the
  * specified query from the SearchRequest object.
  *
  * @param associator
  * @param summary
  * @param index Index of the query in the SearchRequest object
  * @return
  */
 private ProductIndexQuery getProductIndexQuery(
     Associator associator, ProductSummary summary, int index) {
   SearchRequest request = associator.getSearchRequest(summary);
   List<SearchQuery> queries = request.getQueries();
   return queries.get(index).getProductIndexQuery();
 }
Пример #23
0
  /**
   * Runs the actual query expansion
   *
   * @see
   *     org.terrier.querying.PostProcess#process(org.terrier.querying.Manager,org.terrier.querying.SearchRequest)
   */
  public void process(Manager manager, SearchRequest q) {
    Index index = getIndex(manager);
    lastIndex = index;
    documentIndex = index.getDocumentIndex();
    invertedIndex = index.getInvertedIndex();
    lexicon = index.getLexicon();
    collStats = index.getCollectionStatistics();
    directIndex = index.getDirectIndex();
    metaIndex = index.getMetaIndex();
    if (directIndex == null) {
      logger.error("This index does not have a direct index. Query expansion disabled!!");
      return;
    }
    logger.debug("Starting query expansion post-processing.");
    // get the query expansion model to use
    String qeModel = q.getControl("qemodel");
    if (qeModel == null || qeModel.length() == 0) {
      logger.warn(
          "qemodel control not set for QueryExpansion" + " post process. Using default model Bo1");
      qeModel = "Bo1";
    }
    setQueryExpansionModel(getQueryExpansionModel(qeModel));
    if (logger.isDebugEnabled()) {
      logger.info("query expansion model: " + QEModel.getInfo());
    }
    MatchingQueryTerms queryTerms = ((Request) q).getMatchingQueryTerms();
    if (queryTerms == null) {
      logger.warn("No query terms for this query. Skipping QE");
      return;
    }
    // get the expanded query terms
    try {
      expandQuery(queryTerms, (Request) q);
    } catch (IOException ioe) {
      logger.error("IOException while expanding query, skipping QE", ioe);
      return;
    }
    if (logger.isDebugEnabled()) {
      logger.info("query length after expansion: " + queryTerms.length());
      logger.info("Expanded query: ");
    }
    final String[] newQueryTerms = queryTerms.getTerms();
    StringBuilder newQuery = new StringBuilder();
    for (int i = 0; i < newQueryTerms.length; i++) {
      try {
        if (logger.isDebugEnabled()) {
          logger.info(
              (i + 1)
                  + ": "
                  + newQueryTerms[i]
                  + ", normalisedFrequency: "
                  + Rounding.toString(queryTerms.getTermWeight(newQueryTerms[i]), 4));
        }
        newQuery.append(newQueryTerms[i]);
        newQuery.append('^');
        newQuery.append(Rounding.toString(queryTerms.getTermWeight(newQueryTerms[i]), 9));
        newQuery.append(' ');
      } catch (NullPointerException npe) {
        logger.error("Nullpointer exception occured in Query Expansion dumping of new Query", npe);
      }
    }

    logger.debug("NEWQUERY " + q.getQueryID() + " " + newQuery.toString());
    lastExpandedQuery = newQuery.toString();
    q.setControl("QE.ExpandedQuery", newQuery.toString());
    final boolean no2ndPass =
        Boolean.parseBoolean(ApplicationSetup.getProperty("qe.no.2nd.matching", "false"));
    if (no2ndPass) {
      return;
    }

    // run retrieval process again for the expanded query
    logger.info("Accessing inverted file for expanded query " + q.getQueryID());
    manager.runMatching(q);
  }
Пример #24
0
  public static SearchResult performSearch(SearchRequest sr) {
    // TODO: alter order based on user input
    // TODO: disallow searching on input not filled out in a user profile
    // TODO: call method by string name to get rid of if statemetns

    Mentee mentee = OfyService.ofy().load().type(Mentee.class).id(sr.mentee).get();
    Query<Mentee> q =
        OfyService.ofy()
            .load()
            .type(
                Mentee.class); // TODO: Decide what happens when the user does not select anything.
    q = q.filter("Email !=", sr.mentee); // TODO: optimize this

    for (String param : sr.getParameters()) {

      if (param.equals("Majors")) {
        q = q.filter("Majors in", mentee.getMajors());
      }
      if (param.equals("ZipCode")) {
        q = q.filter("ZipCode", mentee.getZipCode());
      }
      if (param.equals("Interests")) {
        q = q.filter("Interests in", mentee.getInterests());
      }
      if (param.equals("Current_Courses")) {
        q = q.filter("Current_Courses in", mentee.getCurrentCourses());
      }
      if (param.equals("Past_Courses")) {
        q = q.filter("Past_Courses in", mentee.getPastCourses());
      }
      if (param.equals("Classification")) {
        q = q.filter("Classification", mentee.getClassification());
      }
      ArrayList<String> SearchTerms = new ArrayList<String>();
      StringTokenizer st = new StringTokenizer(param, " ");
      while (st.hasMoreElements()) {
        SearchTerms.add((st.nextElement().toString()).replaceAll("\\W", "").toLowerCase());
      }
      if (SearchTerms.contains("muscle")) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Weight Lifting");
        q = q.filter("Interests in", list);
      }
      if (SearchTerms.contains("lifts")) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Weight Lifting");
        q = q.filter("Interests in", list);
      }
      if (SearchTerms.contains("ee")) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Electrical Engineering");
        q = q.filter("Majors in", list);
        list.remove(0);
        if (SearchTerms.indexOf("ee") != (SearchTerms.size() - 1)) {
          list.add("EE " + SearchTerms.get((SearchTerms.indexOf("ee") + 1)).toUpperCase());
          Query<Mentee> q2 = q;
          q = q.filter("Past_Courses in", list);
          q2 = q2.filter("Current_Courses in", list);
          ArrayList<Mentee> union = new ArrayList<Mentee>();
          for (Mentee m : q) union.add(m);
          for (Mentee m : q2) {
            if (!union.contains(m)) union.add(m);
          }
          SearchResult result = new SearchResult();
          result.setMatches(union);
          return result;
        }
      }
      if (SearchTerms.contains("party")
          || SearchTerms.contains("frat")
          || SearchTerms.contains("greek")
          || SearchTerms.contains("social")
          || SearchTerms.contains("fraternity")) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Greek Life");
        list.add("Partying");
        q = q.filter("Interests in", list);
      }
    }

    SearchResult result = new SearchResult();
    result.setMatches(q.list());
    return result;
  }
Пример #25
0
  public MultiSearchRequest add(
      BytesReference data,
      boolean contentUnsafe,
      @Nullable String[] indices,
      @Nullable String[] types,
      @Nullable String searchType,
      @Nullable String routing,
      IndicesOptions indicesOptions,
      boolean allowExplicitIndex)
      throws Exception {
    XContent xContent = XContentFactory.xContent(data);
    int from = 0;
    int length = data.length();
    byte marker = xContent.streamSeparator();
    while (true) {
      int nextMarker = findNextMarker(marker, from, data, length);
      if (nextMarker == -1) {
        break;
      }
      // support first line with \n
      if (nextMarker == 0) {
        from = nextMarker + 1;
        continue;
      }

      SearchRequest searchRequest = new SearchRequest();
      if (indices != null) {
        searchRequest.indices(indices);
      }
      if (indicesOptions != null) {
        searchRequest.indicesOptions(indicesOptions);
      }
      if (types != null && types.length > 0) {
        searchRequest.types(types);
      }
      if (routing != null) {
        searchRequest.routing(routing);
      }
      searchRequest.searchType(searchType);

      IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
      boolean ignoreUnavailable = defaultOptions.ignoreUnavailable();
      boolean allowNoIndices = defaultOptions.allowNoIndices();
      boolean expandWildcardsOpen = defaultOptions.expandWildcardsOpen();
      boolean expandWildcardsClosed = defaultOptions.expandWildcardsClosed();

      // now parse the action
      if (nextMarker - from > 0) {
        try (XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from))) {
          // Move to START_OBJECT, if token is null, its an empty data
          XContentParser.Token token = parser.nextToken();
          if (token != null) {
            assert token == XContentParser.Token.START_OBJECT;
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
              if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
              } else if (token.isValue()) {
                if ("index".equals(currentFieldName) || "indices".equals(currentFieldName)) {
                  if (!allowExplicitIndex) {
                    throw new ElasticsearchIllegalArgumentException(
                        "explicit index in multi search is not allowed");
                  }
                  searchRequest.indices(Strings.splitStringByCommaToArray(parser.text()));
                } else if ("type".equals(currentFieldName) || "types".equals(currentFieldName)) {
                  searchRequest.types(Strings.splitStringByCommaToArray(parser.text()));
                } else if ("search_type".equals(currentFieldName)
                    || "searchType".equals(currentFieldName)) {
                  searchRequest.searchType(parser.text());
                } else if ("query_cache".equals(currentFieldName)
                    || "queryCache".equals(currentFieldName)) {
                  searchRequest.queryCache(parser.booleanValue());
                } else if ("preference".equals(currentFieldName)) {
                  searchRequest.preference(parser.text());
                } else if ("routing".equals(currentFieldName)) {
                  searchRequest.routing(parser.text());
                } else if ("ignore_unavailable".equals(currentFieldName)
                    || "ignoreUnavailable".equals(currentFieldName)) {
                  ignoreUnavailable = parser.booleanValue();
                } else if ("allow_no_indices".equals(currentFieldName)
                    || "allowNoIndices".equals(currentFieldName)) {
                  allowNoIndices = parser.booleanValue();
                } else if ("expand_wildcards".equals(currentFieldName)
                    || "expandWildcards".equals(currentFieldName)) {
                  String[] wildcards = Strings.splitStringByCommaToArray(parser.text());
                  for (String wildcard : wildcards) {
                    if ("open".equals(wildcard)) {
                      expandWildcardsOpen = true;
                    } else if ("closed".equals(wildcard)) {
                      expandWildcardsClosed = true;
                    } else {
                      throw new ElasticsearchIllegalArgumentException(
                          "No valid expand wildcard value [" + wildcard + "]");
                    }
                  }
                }
              } else if (token == XContentParser.Token.START_ARRAY) {
                if ("index".equals(currentFieldName) || "indices".equals(currentFieldName)) {
                  if (!allowExplicitIndex) {
                    throw new ElasticsearchIllegalArgumentException(
                        "explicit index in multi search is not allowed");
                  }
                  searchRequest.indices(parseArray(parser));
                } else if ("type".equals(currentFieldName) || "types".equals(currentFieldName)) {
                  searchRequest.types(parseArray(parser));
                } else if ("expand_wildcards".equals(currentFieldName)
                    || "expandWildcards".equals(currentFieldName)) {
                  String[] wildcards = parseArray(parser);
                  for (String wildcard : wildcards) {
                    if ("open".equals(wildcard)) {
                      expandWildcardsOpen = true;
                    } else if ("closed".equals(wildcard)) {
                      expandWildcardsClosed = true;
                    } else {
                      throw new ElasticsearchIllegalArgumentException(
                          "No valid expand wildcard value [" + wildcard + "]");
                    }
                  }
                } else {
                  throw new ElasticsearchParseException(
                      currentFieldName + " doesn't support arrays");
                }
              }
            }
          }
        }
      }
      searchRequest.indicesOptions(
          IndicesOptions.fromOptions(
              ignoreUnavailable,
              allowNoIndices,
              expandWildcardsOpen,
              expandWildcardsClosed,
              defaultOptions));

      // move pointers
      from = nextMarker + 1;
      // now for the body
      nextMarker = findNextMarker(marker, from, data, length);
      if (nextMarker == -1) {
        break;
      }

      searchRequest.source(data.slice(from, nextMarker - from), contentUnsafe);
      // move pointers
      from = nextMarker + 1;

      add(searchRequest);
    }

    return this;
  }
Пример #26
0
    public void run() {
      StringBuilder patternString = new StringBuilder();
      for (Iterator<Pattern> it = searchRequest.getSearchPatterns().iterator(); it.hasNext(); ) {
        Pattern pattern = it.next();
        patternString.append(pattern.pattern());
        if (it.hasNext()) {
          patternString.append("  AND  ");
        }
      }
      logger.info("Starting search {} (pattern: {})", searchId, patternString);
      List<SearchResult> results = new ArrayList<>();

      long searchStartTime = System.currentTimeMillis();
      fireSearchStarted();
      long count = 0;
      int total = searchMetadataCache.size();
      int percent = 0;
      for (SearchMetadata searchMetadata : searchMetadataCache) {
        if (!isLatestSearch()) {
          // New search started
          logger.info("    Terminating search {} prematurely", searchId);
          return;
        }
        String text = searchMetadata.getSearchString();
        boolean matchedAllPatterns = true;
        int startIndex = 0;
        ImmutableList.Builder<SearchResultMatch> matchesBuilder = ImmutableList.builder();
        for (Pattern pattern : searchRequest.getSearchPatterns()) {
          if (startIndex >= text.length()) {
            matchedAllPatterns = false;
            break;
          }
          Matcher matcher = pattern.matcher(text);
          if (matcher.find()) {
            SearchResultMatch match =
                new SearchResultMatch(pattern, matcher.start(), matcher.end());
            matchesBuilder.add(match);
            startIndex = matcher.end() + 1;
          } else {
            matchedAllPatterns = false;
            break;
          }
        }
        if (matchedAllPatterns) {

          results.add(new SearchResult(searchMetadata, matchesBuilder.build()));
        }
        count++;
        int nextPercent = (int) ((count * 100) / total);
        if (nextPercent != percent) {
          percent = nextPercent;
          fireSearchProgressed(percent, results.size());
        }
      }
      SearchManager.this.fireSearchFinished();
      long searchEndTime = System.currentTimeMillis();
      long searchTime = searchEndTime - searchStartTime;
      logger.info(
          "    Finished search {} in {} ms ({} results)", searchId, searchTime, results.size());
      fireSearchFinished(results, searchResultHandler);
    }
Пример #27
0
  public static void main(String argv[]) throws Exception {
    RetsSession session = new RetsSession("http://demo.crt.realtors.org:6103/rets/login");

    if (!session.Login("Joe", "Schmoe")) {
      System.out.println("Invalid login");
      System.exit(2);
    }

    System.out.println("Action: " + session.GetAction());
    RetsVersion version = session.GetDetectedRetsVersion();

    System.out.println("RETS Version: " + ((version == RetsVersion.RETS_1_5) ? "1.5" : "1.0"));

    SearchRequest searchRequest =
        session.CreateSearchRequest("Property", "RES", "(ListPrice=300000-)");

    searchRequest.SetSelect("ListingID,ListPrice,Beds,City");
    searchRequest.SetLimit(SearchRequest.LIMIT_DEFAULT);
    searchRequest.SetOffset(SearchRequest.OFFSET_NONE);
    searchRequest.SetCountType(SearchRequest.CountType.RECORD_COUNT_AND_RESULTS);
    searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT);
    searchRequest.SetStandardNames(true);

    try {
      File f = new File("rawsearch.xml");
      FileOutputStream fop = new FileOutputStream(f);
      byte[] data = session.SearchAsArray(searchRequest);
      fop.write(data);
      fop.flush();
      fop.close();
    } catch (IOException e) {
    }

    LogoutResponse logout = session.Logout();

    SearchResultSet results = new SearchResultSet();

    // Reopen the file now for input
    try {
      File f = new File("rawsearch.xml");
      byte[] buffer = new byte[(int) f.length()];

      FileInputStream fip = new FileInputStream(f);

      int offset = 0;
      int numRead = 0;

      while (offset < buffer.length
          && (numRead = fip.read(buffer, offset, buffer.length - offset)) >= 0) offset += numRead;

      results.SetEncoding(EncodingType.RETS_XML_DEFAULT_ENCODING);
      results.SetDataAsArray(buffer);
    } catch (IOException e) {
    }

    System.out.println("Record count: " + results.GetCount());

    StringVector columns = results.GetColumns();

    while (results.HasNext()) {
      if (columns == null) {
        columns = results.GetColumns();
      }
      for (int i = 0; i < columns.size(); i++) {
        System.out.format("%15s: %s\n", columns.get(i), results.GetString(columns.get(i)));
      }
      System.out.println();
    }

    /*
     * Prototype for returning data in a stream.
    try
    {
        File f=new File("foobarty");
        FileOutputStream fop=new FileOutputStream(f);
        CppInputStream data = session.SearchAsStream(searchRequest);
        byte [] buf = new byte[30];
        int len;
        while ((len = data.read(buf, 0, 30)) > 0)
        {
            fop.write(buf, 0, len);
        }
        fop.flush();
        fop.close();
    }
    catch (IOException e) {}
     * end prototype */

  }
  @EventMapping(SearchConstants.SEARCH_REQUEST_QNAME_STRING)
  public void handleSearchEvents(EventRequest request, EventResponse response) {

    log.debug("Responding to Search Event");

    final Event event = request.getEvent();
    final SearchRequest searchQuery = (SearchRequest) event.getValue();

    final String searchTerms = searchQuery.getSearchTerms();

    final SearchResults searchResults = new SearchResults();

    searchResults.setQueryId(searchQuery.getQueryId());
    searchResults.setWindowId(request.getWindowID());

    for (ContactDomain domain : contactDomains) {

      if (domain.getHasSearch()) {

        ContactSet contacts = domain.search(searchTerms);

        for (Contact contact : contacts) {

          // Build the result object for the match
          final SearchResult searchResult = new SearchResult();
          String title =
              contact.getSurname().toUpperCase()
                  + ", "
                  + contact.getTitle()
                  + " "
                  + contact.getFirstname();
          if (contact.getPosition() != null && !contact.getPosition().equals(""))
            title += " (" + contact.getPosition() + ")";

          List<String> summary = new ArrayList<String>();
          ;

          if (contact.getPrimaryEmailAddress() != null)
            summary.add("E:" + contact.getPrimaryEmailAddress().getEmailAddress());
          if (contact.getPrimaryPhoneNumber() != null)
            summary.add("T:" + contact.getPrimaryPhoneNumber().getPhoneNumber());

          String summaryText = StringUtils.collectionToDelimitedString(summary, " -- ");

          searchResult.setTitle(title);
          searchResult.setSummary(summaryText);

          /*
           * Portlet URL to be added when / if portlet support for
           * deep linking added.
           */
          PortletUrl url = new PortletUrl();
          url.setPortletMode("VIEW");
          url.setWindowState(WindowState.MAXIMIZED.toString());
          PortletUrlParameter domainParam = new PortletUrlParameter();
          domainParam.setName("domain");
          domainParam.getValue().add(domain.getId());
          url.getParam().add(domainParam);
          PortletUrlParameter urnParam = new PortletUrlParameter();
          urnParam.setName("urn");
          urnParam.getValue().add(contact.getURN());
          url.getParam().add(urnParam);

          searchResult.setPortletUrl(url);

          searchResult.getType().add("contact");
          // Add the result to the results and send the event
          searchResults.getSearchResult().add(searchResult);
        }
      }
    }

    response.setEvent(SearchConstants.SEARCH_RESULTS_QNAME, searchResults);

    log.debug(
        "Finished response -- " + searchResults.getSearchResult().size() + " -- results returned");
  }
  /** Test the we can't get a particular request when it does exist. We don't */
  @Test
  public void testGetSearchRequestDoesNotExist() {
    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(null);
    deletegateStore.getSearchRequest(searchRequest2.getId());
    mockController.setReturnValue(searchRequest2);
    deletegateStore.getSearchRequest(searchRequest4.getId());
    mockController.setReturnValue(searchRequest4);
    deletegateStore.getSearchRequest(searchRequest1.getId());
    mockController.setReturnValue(null);

    mockController.replay();

    // this should call through because it is not in the cache.
    assertNull(null, cachingStore.getSearchRequest(searchRequest1.getId()));

    // this should call through because it is not in the cache.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));

    // these calls should be cached.
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));

    // this call should be direct through.
    assertEqualsNotSame(searchRequest4, cachingStore.getSearchRequest(searchRequest4.getId()));

    // these should be cached.
    assertEqualsNotSame(searchRequest4, cachingStore.getSearchRequest(searchRequest4.getId()));
    assertEqualsNotSame(searchRequest4, cachingStore.getSearchRequest(searchRequest4.getId()));
    assertEqualsNotSame(searchRequest4, cachingStore.getSearchRequest(searchRequest4.getId()));
    assertEqualsNotSame(searchRequest2, cachingStore.getSearchRequest(searchRequest2.getId()));

    // this will not be cached.
    assertNull(cachingStore.getSearchRequest(searchRequest1.getId()));

    mockController.verify();
  }