@Override
 public ResultPage<T> search(ResultPage<T> resultPage, Mapper<T> mapper) {
   ElasticSearchCriteria criteria = resultPage.getCriteria();
   if (criteria == null) return resultPage;
   SearchRequestBuilder srb = criteria2builder(criteria);
   if (resultPage.isPaginating()) {
     srb.setFrom(resultPage.getStart());
     srb.setSize(resultPage.getPageSize());
   } else {
     srb.setFrom(0);
     srb.setSize(ResultPage.DEFAULT_MAX_PAGESIZE);
   }
   try {
     SearchResponse response = srb.execute().get();
     SearchHits shs = response.getHits();
     if (shs != null) {
       resultPage.setTookInMillis(response.getTookInMillis());
       resultPage.setTotalResults(shs.getTotalHits());
       List list = new ArrayList(shs.getHits().length);
       resultPage.setResult(list);
       for (SearchHit sh : shs.getHits()) {
         T data = (T) indexManager.searchHitToEntity(sh);
         data = mapper == null ? data : mapper.map(data);
         if (data != null) list.add(data);
       }
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return resultPage;
 }
  @Test
  public void testMultipleRescores() throws Exception {
    int numDocs = indexRandomNumbers("keyword", 1);
    QueryRescorer eightIsGreat =
        RescoreBuilder.queryRescorer(
                QueryBuilders.functionScoreQuery(
                        QueryBuilders.termQuery("field1", English.intToEnglish(8)))
                    .boostMode(CombineFunction.REPLACE)
                    .add(ScoreFunctionBuilders.scriptFunction("1000.0f")))
            .setScoreMode("total");
    QueryRescorer sevenIsBetter =
        RescoreBuilder.queryRescorer(
                QueryBuilders.functionScoreQuery(
                        QueryBuilders.termQuery("field1", English.intToEnglish(7)))
                    .boostMode(CombineFunction.REPLACE)
                    .add(ScoreFunctionBuilders.scriptFunction("10000.0f")))
            .setScoreMode("total");

    // First set the rescore window large enough that both rescores take effect
    SearchRequestBuilder request = client().prepareSearch().setRescoreWindow(numDocs);
    request.addRescorer(eightIsGreat).addRescorer(sevenIsBetter);
    SearchResponse response = request.get();
    assertFirstHit(response, hasId("7"));
    assertSecondHit(response, hasId("8"));

    // Now squash the second rescore window so it never gets to see a seven
    response =
        request
            .setSize(1)
            .clearRescorers()
            .addRescorer(eightIsGreat)
            .addRescorer(sevenIsBetter, 1)
            .get();
    assertFirstHit(response, hasId("8"));
    // We have no idea what the second hit will be because we didn't get a chance to look for seven

    // Now use one rescore to drag the number we're looking for into the window of another
    QueryRescorer ninetyIsGood =
        RescoreBuilder.queryRescorer(
                QueryBuilders.functionScoreQuery(QueryBuilders.queryString("*ninety*"))
                    .boostMode(CombineFunction.REPLACE)
                    .add(ScoreFunctionBuilders.scriptFunction("1000.0f")))
            .setScoreMode("total");
    QueryRescorer oneToo =
        RescoreBuilder.queryRescorer(
                QueryBuilders.functionScoreQuery(QueryBuilders.queryString("*one*"))
                    .boostMode(CombineFunction.REPLACE)
                    .add(ScoreFunctionBuilders.scriptFunction("1000.0f")))
            .setScoreMode("total");
    request.clearRescorers().addRescorer(ninetyIsGood).addRescorer(oneToo, 10);
    response = request.setSize(2).get();
    assertFirstHit(response, hasId("91"));
    assertFirstHit(response, hasScore(2001.0f));
    assertSecondHit(
        response, hasScore(1001.0f)); // Not sure which one it is but it is ninety something
  }
Beispiel #3
0
  private SearchRequestBuilder standardSearchRequest(
      String query, Set<String> indices, int limit, int offset, TimeRange range, Sorting sort)
      throws IndexHelper.InvalidRangeFormatException {
    if (query == null || query.trim().isEmpty()) {
      query = "*";
    }

    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(indices.toArray(new String[] {}));

    if (query.trim().equals("*")) {
      srb.setQuery(matchAllQuery());
    } else {
      QueryStringQueryBuilder qs = queryString(query);
      qs.allowLeadingWildcard(server.getConfiguration().isAllowLeadingWildcardSearches());
      srb.setQuery(qs);
    }

    srb.setFrom(offset);

    if (limit > 0) {
      srb.setSize(limit);
    }

    if (range != null) {
      srb.setFilter(IndexHelper.getTimestampRangeFilter(range));
    }

    if (sort != null) {
      srb.addSort(sort.getField(), sort.asElastic());
    }

    return srb;
  }
  private SearchRequestBuilder prepareSearch(Query query) {
    Assert.notNull(query.getIndices(), "No index defined for Query");
    Assert.notNull(query.getTypes(), "No type defined for Query");

    int startRecord = 0;
    SearchRequestBuilder searchRequestBuilder =
        client
            .prepareSearch(toArray(query.getIndices()))
            .setSearchType(query.getSearchType())
            .setTypes(toArray(query.getTypes()));

    if (query.getPageable() != null) {
      startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
      searchRequestBuilder.setSize(query.getPageable().getPageSize());
    }
    searchRequestBuilder.setFrom(startRecord);

    if (!query.getFields().isEmpty()) {
      searchRequestBuilder.addFields(toArray(query.getFields()));
    }

    if (query.getSort() != null) {
      for (Sort.Order order : query.getSort()) {
        searchRequestBuilder.addSort(
            order.getProperty(),
            order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC : SortOrder.ASC);
      }
    }

    if (query.getMinScore() > 0) {
      searchRequestBuilder.setMinScore(query.getMinScore());
    }
    return searchRequestBuilder;
  }
 @Override
 public List<T> search(SearchCriteria searchCriteria, Mapper mapper, int limit) {
   ElasticSearchCriteria criteria = (ElasticSearchCriteria) searchCriteria;
   if (criteria == null) return null;
   SearchRequestBuilder srb = criteria2builder(criteria);
   srb.setFrom(0);
   if (limit > 0 && limit < ResultPage.DEFAULT_MAX_PAGESIZE) srb.setSize(limit);
   else srb.setSize(ResultPage.DEFAULT_MAX_PAGESIZE);
   List list = null;
   try {
     SearchResponse response = srb.execute().get();
     SearchHits shs = response.getHits();
     list = new ArrayList(shs.getHits().length);
     for (SearchHit sh : shs.getHits()) {
       Object data = indexManager.searchHitToEntity(sh);
       data = mapper == null ? data : mapper.map(data);
       if (data != null) list.add(data);
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return list;
 }
Beispiel #6
0
  private SearchHit oneOfIndex(String index, QueryBuilder q, SortOrder sort) {
    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(index);
    srb.setQuery(q);
    srb.setSize(1);
    srb.addSort("timestamp", sort);

    SearchResponse r = c.search(srb.request()).actionGet();
    if (r.getHits() != null && r.getHits().totalHits() > 0) {
      return r.getHits().getAt(0);
    } else {
      return null;
    }
  }
 public final long countModels(
     final Logger logger, final Marker logMarker, final SearchRequestBuilder requestBuilder)
     throws IoExceptionT {
   logger.debug(
       logMarker, "counting documents in index {} with request {}", indexName, requestBuilder);
   try {
     return requestBuilder.setSize(0).execute().actionGet().getHits().getTotalHits();
   } catch (final IndexNotFoundException e) {
     logger.warn(logMarker, "tried to count documents in missing index {}", indexName);
     return 0l;
   } catch (final ElasticsearchException e) {
     throw exceptionFactory.newIoException(
         e, String.format("error counting documents in index %s", indexName));
   }
 }
  public SearchResponse doSearch(ActivityQuery query, SearchOptions options) {
    SearchRequestBuilder requestBuilder =
        getClient()
            .prepareSearch(ActivityIndexDefinition.INDEX)
            .setTypes(ActivityIndexDefinition.TYPE);

    requestBuilder.setFrom(options.getOffset());
    requestBuilder.setSize(options.getLimit());
    requestBuilder.addSort(ActivityIndexDefinition.FIELD_CREATED_AT, SortOrder.DESC);

    AndFilterBuilder filter = FilterBuilders.andFilter();
    if (!query.getTypes().isEmpty()) {
      OrFilterBuilder typeFilter = FilterBuilders.orFilter();
      for (String type : query.getTypes()) {
        typeFilter.add(FilterBuilders.termFilter(ActivityIndexDefinition.FIELD_TYPE, type));
      }
      filter.add(typeFilter);
    }

    if (!query.getDataOrFilters().isEmpty()) {
      for (Map.Entry<String, Object> entry : query.getDataOrFilters().entrySet()) {
        OrFilterBuilder orFilter = FilterBuilders.orFilter();
        orFilter.add(
            FilterBuilders.nestedFilter(
                ActivityIndexDefinition.FIELD_DETAILS,
                FilterBuilders.termFilter(
                    ActivityIndexDefinition.FIELD_DETAILS + "." + entry.getKey(),
                    entry.getValue())));
        filter.add(orFilter);
      }
    }

    Date since = query.getSince();
    if (since != null) {
      filter.add(
          FilterBuilders.rangeFilter(ActivityIndexDefinition.FIELD_CREATED_AT)
              .gt(since)
              .cache(false));
    }
    Date to = query.getTo();
    if (to != null) {
      filter.add(
          FilterBuilders.rangeFilter(ActivityIndexDefinition.FIELD_CREATED_AT).lt(to).cache(false));
    }

    requestBuilder.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter));
    return requestBuilder.get();
  }
  /**
   * @param req
   * @return
   * @throws IOException
   */
  public AgroAutocompleteResponse autocomplete(AgroAutocompleteRequest req) throws IOException {
    AgroAutocompleteResponse res = new AgroAutocompleteResponse();

    // create the SearchRequestBuilder
    SearchRequestBuilder searchReqBuilder = esClient.prepareSearch("history").setTypes("query");

    // setup query
    QueryBuilder queryBuilder;
    queryBuilder = QueryBuilders.prefixQuery("query.raw", req.getQuery());
    searchReqBuilder.setQuery(queryBuilder);

    // setup paging
    searchReqBuilder.setFrom(0);
    searchReqBuilder.setSize(200);

    // execute the request and get the response
    SearchResponse response = searchReqBuilder.execute().actionGet();

    // parse the response into AgroAutocompleteResponse
    Iterator<SearchHit> hitsIter = response.getHits().iterator();
    SearchHit hit;
    int i = 0;

    while (hitsIter.hasNext() && i < 10) {
      hit = hitsIter.next();

      String source = hit.getSourceAsString();
      UserQuery uq = objectMapper.reader(UserQuery.class).readValue(source);
      if (res.addResult(uq.getQuery())) {
        i++;
      }
    }

    // add query (meta)data to res
    res.setTotal(response.getHits().getTotalHits());
    res.setTime(response.getTookInMillis());

    // ..and
    return res;
  }
 private void setLimitFromHint(List<Hint> hints) {
   int from = 0;
   int size = 0;
   for (Hint hint : hints) {
     if (hint.getType() == HintType.DOCS_WITH_AGGREGATION) {
       Integer[] params = (Integer[]) hint.getParams();
       if (params.length > 1) {
         // if 2 or more are given, use the first as the from and the second as the size
         // so it is the same as LIMIT from,size
         // except written as /*! DOCS_WITH_AGGREGATION(from,size) */
         from = params[0];
         size = params[1];
       } else if (params.length == 1) {
         // if only 1 parameter is given, use it as the size with a from of 0
         size = params[0];
       }
       break;
     }
   }
   request.setFrom(from);
   request.setSize(size);
 }
 @Override
 public Map<String, Integer> countTermsByField(SearchCriteria searchCriteria, String field) {
   ElasticSearchCriteria criteria = (ElasticSearchCriteria) searchCriteria;
   if (criteria == null) return null;
   SearchRequestBuilder srb = criteria2builder(criteria);
   srb.setFrom(0);
   srb.setSize(0);
   TermsBuilder tb = AggregationBuilders.terms(field);
   tb.field(field);
   srb.addAggregation(tb);
   try {
     SearchResponse response = srb.execute().get();
     StringTerms aggr = response.getAggregations().get(field);
     Map<String, Integer> result = new LinkedHashMap<>();
     for (Terms.Bucket bucket : aggr.getBuckets()) {
       result.put(bucket.getKey(), (int) bucket.getDocCount());
     }
     return result;
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return Collections.emptyMap();
 }
 @Override
 public void customize(SearchRequestBuilder builder) {
   builder.setSize(1);
 }