org.elasticsearch.action.search.SearchRequestBuilder Java Examples

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchExtendTransportFactory.java    From database-transform-tool with Apache License 2.0 7 votes vote down vote up
public String selectTermAll(String indexs,String types,String field,String value){
	try {
		if(client==null){
			init();
		}
		SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
		request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.setQuery(QueryBuilders.termQuery(field, value));
		request.highlighter(new HighlightBuilder().field(field));
		request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
		request.setExplain(false);
		SearchResponse response = request.get();
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #2
Source File: BsThumbnailQueueCB.java    From fess with Apache License 2.0 6 votes vote down vote up
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }

    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }

    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }

    return builder;
}
 
Example #3
Source File: ElasticsearchReindexIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(ElasticsearchReindexConfiguration.class).detectCustomConfiguration("ElasticsearchReindexIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getSource()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertThat(clusterHealthResponse.getStatus(), not(ClusterHealthStatus.RED));

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getSource().getIndexes().get(0));
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertThat(indicesExistsResponse.isExists(), is(true));

  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getSource().getIndexes().get(0))
      .setTypes(testConfiguration.getSource().getTypes().get(0));
  SearchResponse countResponse = countRequest.execute().actionGet();

  count = (int)countResponse.getHits().getTotalHits();

  assertThat(count, not(0));

}
 
Example #4
Source File: MongoElasticsearchSyncIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@Test
public void testSync() throws Exception {

  MongoElasticsearchSync sync = new MongoElasticsearchSync(testConfiguration);

  sync.run();

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getDestination().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertTrue(indicesExistsResponse.isExists());

  // assert lines in file
  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getDestination().getIndex())
      .setTypes(testConfiguration.getDestination().getType());
  SearchResponse countResponse = countRequest.execute().actionGet();

  assertEquals((int)countResponse.getHits().getTotalHits(), 89);

}
 
Example #5
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<Message> getMessages(String queue) {
    try {
        BoolQueryBuilder fq = boolQueryBuilder("queue='" + queue + "'", "*");

        String docType = StringUtils.isBlank(docTypeOverride) ? MSG_DOC_TYPE : docTypeOverride;
        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(messageIndexPrefix + "*")
                .setQuery(fq)
                .setTypes(docType)
                .addSort(SortBuilders.fieldSort("created").order(SortOrder.ASC));

        return mapGetMessagesResponse(srb.execute().actionGet());
    } catch (Exception e) {
        LOGGER.error("Failed to get messages for queue: {}", queue, e);
    }
    return null;
}
 
Example #6
Source File: ElasticsearchTransportFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectAll(String indexs,String types,String condition){
	try {
		if(client==null){
			init();
		}
		SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
		request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.setQuery(QueryBuilders.queryStringQuery(condition));
		request.setExplain(false);
		SearchResponse response = request.get();
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #7
Source File: DistinctAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public SearchRequestBuilder getRequestBuilder(DistinctRequest request) {
    SearchRequestBuilder query;
    try {
        query = getConnection().getClient()
                .prepareSearch(ElasticsearchUtils.getIndices(request.getTable(), request))
                .setIndicesOptions(Utils.indicesOptions());
        query.setQuery(new ElasticSearchQueryGenerator().genFilter(request.getFilters()))
                .setSize(QUERY_SIZE)
                .addAggregation(Utils.buildTermsAggregation(
                        request.getNesting(), Sets.newHashSet(), elasticsearchTuningConfig.getAggregationSize()));

    }
    catch (Exception e) {
        throw FoxtrotExceptions.queryCreationException(request, e);
    }
    return query;
}
 
Example #8
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
private SearchResult<String> search(String structuredQuery, int start, int size, List<String> sortOptions, String freeTextQuery, String docType) {
    try {
        docType = StringUtils.isBlank(docTypeOverride) ? docType : docTypeOverride;
        BoolQueryBuilder fq = boolQueryBuilder(structuredQuery, freeTextQuery);
        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(getIndexName(docType))
                .setQuery(fq)
                .setTypes(docType)
                .storedFields("_id")
                .setFrom(start)
                .setSize(size);

        addSortOptions(srb, sortOptions);

        return mapSearchResult(srb.get());
    } catch (ParserException e) {
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, e.getMessage(), e);
    }
}
 
Example #9
Source File: ScanBuilder.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
protected void applyEdgeProjection(SearchRequestBuilder searchRequest, ElasticIntermediateScanPrel scan) {
  boolean edgeProject = PrelUtil.getPlannerSettings(scan.getCluster()).getOptions().getOption(ExecConstants.ELASTIC_RULES_EDGE_PROJECT);
  if(!edgeProject){
    return;
  }


  final String[] includesOrderedByOriginalTable;
  if (scan.getProjectedColumns().isEmpty()) {
    includesOrderedByOriginalTable = new String[0];
  } else {
    includesOrderedByOriginalTable =
        CalciteArrowHelper.wrap(scan.getBatchSchema().mask(scan.getProjectedColumns(), false))
          .toCalciteRecordType(scan.getCluster().getTypeFactory()).getFieldNames().toArray(new String[0]);
  }

  // canonicalize includes order so we don't get test variability.
  Arrays.sort(includesOrderedByOriginalTable);
  searchRequest.setFetchSource(includesOrderedByOriginalTable, null);
}
 
Example #10
Source File: CustomSearchRepositoryImpl.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
/**
     * Return all records, and truncate the content with the ResultTruncatedContentMapper
     *
     * @param pageable
     * @param version
     * @param project
     * @return
     */
    @Override
    public Page<File> customfindAll(Pageable pageable, List<String> version, List<String> project, List<String> extension) {
        NativeSearchQueryBuilder nativeSearchQueryBuilder = Queries.constructSearchQueryBuilder("");
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        SearchRequestBuilder searchRequestBuilder = constructRequestBuilder(nativeSearchQuery, pageable, version, project, extension);
        SearchResponse response = searchRequestBuilder.execute().actionGet();

        SearchHit[] hits = response.getHits().hits();
        ResultTruncatedContentMapper mapper = new ResultTruncatedContentMapper();
        return mapper.mapResults(response, File.class, nativeSearchQuery.getPageable());

//        }

    }
 
Example #11
Source File: BaseClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
public Long mostRecentDocument(String index) {
    if (client() == null) {
        return null;
    }
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
    SortBuilder sort = SortBuilders.fieldSort("_timestamp").order(SortOrder.DESC);
    SearchResponse searchResponse = searchRequestBuilder.setIndices(index).addField("_timestamp").setSize(1).addSort(sort).execute().actionGet();
    if (searchResponse.getHits().getHits().length == 1) {
        SearchHit hit = searchResponse.getHits().getHits()[0];
        if (hit.getFields().get("_timestamp") != null) {
            return hit.getFields().get("_timestamp").getValue();
        } else {
            return 0L;
        }
    }
    return null;
}
 
Example #12
Source File: BsKeyMatchCB.java    From fess with Apache License 2.0 6 votes vote down vote up
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }

    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }

    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }

    return builder;
}
 
Example #13
Source File: BsRoleCB.java    From fess with Apache License 2.0 6 votes vote down vote up
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }

    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }

    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }

    return builder;
}
 
Example #14
Source File: EsEntityIndexImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
private long getEntitySizeAggregation( final SearchRequestBuilder builder ) {
    final String key = "entitySize";
    SumBuilder sumBuilder = new SumBuilder(key);
    sumBuilder.field("entitySize");
    builder.addAggregation(sumBuilder);

    Observable<Number> o = Observable.from(builder.execute())
        .map(response -> {
            Sum aggregation = (Sum) response.getAggregations().get(key);
            if(aggregation == null){
                return -1;
            }else{
                return aggregation.getValue();
            }
        });
    Number val =   ObservableTimer.time(o,aggregationTimer).toBlocking().lastOrDefault(-1);
    return val.longValue();
}
 
Example #15
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
@Override
public List<CompletionTime> getTraceCompletions(String tenantId, Criteria criteria) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class);
    SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, criteria.getMaxResponseSize());
    request.addSort(ElasticsearchUtil.TIMESTAMP_FIELD, SortOrder.DESC);
    SearchResponse response = getSearchResponse(request);
    if (response.isTimedOut()) {
        return null;
    }

    return Arrays.stream(response.getHits().getHits())
            .map(AnalyticsServiceElasticsearch::toCompletionTime)
            .filter(c -> c != null)
            .collect(Collectors.toList());
}
 
Example #16
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private SearchResponse getSearchResponse(EnumSet<ElasticsearchDocumentType> elementType, FetchHints fetchHints, int skip, int limit, boolean includeAggregations) {
    SearchRequestBuilder q = buildQuery(elementType, fetchHints, includeAggregations)
        .setFrom(skip)
        .setSize(limit)
        .setTrackTotalHits(true);
    if (QUERY_LOGGER.isTraceEnabled()) {
        QUERY_LOGGER.trace("query: %s", q);
    }

    SearchResponse searchResponse = checkForFailures(q.execute().actionGet());
    if (LOGGER.isDebugEnabled()) {
        SearchHits hits = searchResponse.getHits();
        LOGGER.debug(
            "elasticsearch results %d of %d (time: %dms)",
            hits.getHits().length,
            hits.getTotalHits().value,
            searchResponse.getTook().millis()
        );
    }
    return searchResponse;
}
 
Example #17
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 统计指定网站每天抓取数量
 *
 * @param domain 网站域名
 * @return
 */
public Map<Date, Long> countDomainByGatherTime(String domain) {
    AggregationBuilder aggregation =
            AggregationBuilders
                    .dateHistogram("agg")
                    .field("gatherTime")
                    .dateHistogramInterval(DateHistogramInterval.DAY).order(Histogram.Order.KEY_DESC);
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(QueryBuilders.matchQuery("domain", domain))
            .addAggregation(aggregation);
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    Histogram agg = response.getAggregations().get("agg");
    Map<Date, Long> result = Maps.newHashMap();
    for (Histogram.Bucket entry : agg.getBuckets()) {
        DateTime key = (DateTime) entry.getKey();    // Key
        long docCount = entry.getDocCount();         // Doc count
        result.put(key.toDate(), docCount);
    }
    return result;
}
 
Example #18
Source File: TwitterUserstreamElasticsearchIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@Test
public void testUserstreamElasticsearch() throws Exception {

  TwitterUserstreamElasticsearch stream = new TwitterUserstreamElasticsearch(testConfiguration);

  Thread thread = new Thread(stream);
  thread.start();
  thread.join(60000);

  // assert lines in file
  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getElasticsearch().getIndex())
      .setTypes(testConfiguration.getElasticsearch().getType());
  SearchResponse countResponse = countRequest.execute().actionGet();

  count = (int)countResponse.getHits().getTotalHits();

  assert(count > 0);
}
 
Example #19
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 根据domain获取结果,按照抓取时间排序
 *
 * @param domain 网站域名
 * @param size   每页数量
 * @param page   页码
 * @return
 */
public List<Webpage> getWebpageByDomain(String domain, int size, int page) {
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(QueryBuilders.matchQuery("domain", domain))
            .addSort("gatherTime", SortOrder.DESC)
            .setSize(size).setFrom(size * (page - 1));
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    return warpHits2List(response.getHits());
}
 
Example #20
Source File: EsStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
protected SearchResponse getByDocType(int size) throws Exception {
  SearchRequestBuilder builder = esClient.prepareSearch();
  builder.setIndices(getIndexName()).setTypes(getDocTypeName())
      .setQuery(QueryBuilders.matchAllQuery())
      .setSize(size);

  return builder.execute().actionGet();
}
 
Example #21
Source File: ElasticDocumentSearch.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
private void lookupAnnotationFields(Map<String, Document> idMap) {
	QueryBuilder qb = QueryBuilders.idsQuery(getDocumentType()).addIds(idMap.keySet());
	SearchRequestBuilder srb = getClient().prepareSearch(getIndexName())
			.addFields("*")
			.setQuery(qb)
			.setSize(idMap.size());
	LOGGER.debug("Annotation field lookup query: {}", srb.toString());

	SearchResponse response = srb.execute().actionGet();
	for (SearchHit hit : response.getHits().getHits()) {
		populateAnnotationFields(hit, idMap.get(hit.getId()));
	}
}
 
Example #22
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
private SearchRequestBuilder getBaseSearchRequestBuilder(String type, String index, Criteria criteria, BoolQueryBuilder query, int maxSize) {
    return client.getClient().prepareSearch(index)
            .setTypes(type)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setTimeout(TimeValue.timeValueMillis(criteria.getTimeout()))
            .setSize(maxSize)
            .setQuery(query);
}
 
Example #23
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiSearch() {
    SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
    SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);

    MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();

    // You will get all individual responses from
    // MultiSearchResponse#getResponses()
    long nbHits = 0;
    for (MultiSearchResponse.Item item : sr.getResponses()) {
        SearchResponse response = item.getResponse();
        nbHits += response.getHits().getTotalHits();
    }
}
 
Example #24
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        esCb.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    int count = 0;
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
            }
            count += hits.length;
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }

            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}
 
Example #25
Source File: ElasticsearchParentChildUpdaterIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@Test
public void testParentChildPersistUpdater() throws Exception {

  ElasticsearchPersistUpdater testPersistUpdater = new ElasticsearchPersistUpdater(testConfiguration);
  testPersistUpdater.prepare(null);

  for( Path docPath : files ) {
    LOGGER.info("File: " + docPath );
    FileInputStream testActivityFileStream = new FileInputStream(docPath.toFile());
    Activity activity = MAPPER.readValue(testActivityFileStream, Activity.class);
    activity.setAdditionalProperty("updated", Boolean.TRUE);
    StreamsDatum datum = new StreamsDatum(activity, activity.getVerb());
    if( !StringUtils.isEmpty(activity.getObject().getObjectType())) {
      datum.getMetadata().put("parent", activity.getObject().getObjectType());
      datum.getMetadata().put("type", "activity");
      testPersistUpdater.write(datum);
      LOGGER.info("Updated: " + activity.getVerb() );
    }
  }

  testPersistUpdater.cleanUp();

  SearchRequestBuilder countUpdatedRequest = testClient
      .prepareSearch(testConfiguration.getIndex())
      .setTypes("activity")
      .setQuery(QueryBuilders.queryStringQuery("updated:true"));
  SearchResponse countUpdatedResponse = countUpdatedRequest.execute().actionGet();

  assertEquals(84, countUpdatedResponse.getHits().getTotalHits());

}
 
Example #26
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected Pair<SearchRequestBuilder, QueryBuilder> prepareSearchSuggestionsRequest(String searchString, String currentSite, boolean allMySites) {
    Pair<SearchRequestBuilder,QueryBuilder> builders =
            newSearchSuggestionsRequestAndQueryBuilders(searchString, currentSite, allMySites);
    builders = addSearchSuggestionsCoreParams(builders, searchString, currentSite, allMySites);
    builders = addSearchSuggestionsQuery(builders, searchString, currentSite, allMySites);
    builders = pairOf(addSearchSuggestionResultFields(builders.getLeft()), builders.getRight());
    builders = pairOf(addSearchSuggestionsPagination(builders.getLeft()), builders.getRight());
    return completeSearchSuggestionsRequestBuilders(builders, searchString, currentSite, allMySites);
}
 
Example #27
Source File: SiteElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
protected Pair<SearchRequestBuilder,QueryBuilder> completeSearchSuggestionsRequestBuilders(Pair<SearchRequestBuilder, QueryBuilder> builders,
                                                                                           String searchString,
                                                                                           String currentSite,
                                                                                           boolean allMySites) {
    return builders;
}
 
Example #28
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected SearchRequestBuilder addSearchAggregation(SearchRequestBuilder searchRequestBuilder, String field) {
    if(useAggregation) {
        return searchRequestBuilder.addAggregation(
                AggregationBuilders.terms(AGGREGATION_NAME).field(field).size(serverConfigurationService.getInt("samigo.search.maxResults",50))
                        .subAggregation(AggregationBuilders.topHits(AGGREGATION_TOP_HITS).setSize(1).addFieldDataField("assessmentId").addFieldDataField("site").addFieldDataField("questionPoolId").addFieldDataField("typeId").addFieldDataField("tags").addFieldDataField("qText")));
        //.subAggregation(AggregationBuilders.topHits(AGGREGATION_TOP_HITS).setSize(1).addFieldDataField("origin")));

    }
    return searchRequestBuilder;
}
 
Example #29
Source File: StoredLtrQueryIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testScriptFeatureUseCase() throws Exception {
    addElement(new StoredFeature("feature1", Collections.singletonList("query"), "mustache",
            QueryBuilders.matchQuery("field1", "{{query}}").toString()));
    addElement(new StoredFeature("feature6", Arrays.asList("query", "extra_multiplier_ltr"), ScriptFeature.TEMPLATE_LANGUAGE,
            "{\"lang\": \"native\", \"source\": \"feature_extractor\", \"params\": { \"dependent_feature\": \"feature1\"," +
                    " \"extra_script_params\" : {\"extra_multiplier_ltr\": \"extra_multiplier\"}}}"));
    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client());

    builder.request().setFeatureSet("my_set");
    builder.request().setFeatureNameQuery("feature1");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    builder.execute().get();
    builder.request().setFeatureNameQuery("feature6");
    long version = builder.get().getResponse().getVersion();

    CreateModelFromSetRequestBuilder createModelFromSetRequestBuilder = new CreateModelFromSetRequestBuilder(client());
    createModelFromSetRequestBuilder.withVersion(IndexFeatureStore.DEFAULT_STORE, "my_set", version,
            "my_model", new StoredLtrModel.LtrModelDefinition("model/linear", SIMPLE_SCRIPT_MODEL, true));
    createModelFromSetRequestBuilder.get();
    buildIndex();
    Map<String, Object> params = new HashMap<>();
    params.put("query", "hello");
    params.put("dependent_feature", new HashMap<>());
    params.put("extra_multiplier_ltr", 100.0d);
    SearchRequestBuilder sb = client().prepareSearch("test_index")
            .setQuery(QueryBuilders.matchQuery("field1", "world"))
            .setRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
                    .modelName("my_model").params(params).toString()))
                    .setScoreMode(QueryRescoreMode.Total)
                    .setQueryWeight(0)
                    .setRescoreQueryWeight(1));

    SearchResponse sr = sb.get();
    assertEquals(1, sr.getHits().getTotalHits().value);
    assertThat(sr.getHits().getAt(0).getScore(), Matchers.greaterThanOrEqualTo(29.0f));
    assertThat(sr.getHits().getAt(0).getScore(), Matchers.lessThanOrEqualTo(30.0f));
}
 
Example #30
Source File: QueryAction.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected void updateRequestWithTrackTotalHits(Select select, SearchRequestBuilder request) {
    for (Hint hint : select.getHints()) {
        if (hint.getType() == HintType.TRACK_TOTAL_HITS && hint.getParams() != null && 0 < hint.getParams().length) {
            String param = hint.getParams()[0].toString();
            try {
                request.setTrackTotalHitsUpTo(Integer.parseInt(param));
            } catch (NumberFormatException ex) {
                request.setTrackTotalHits(Boolean.parseBoolean(param));
            }
        }
    }
}