Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder. These examples are extracted from open source projects. 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
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 Project: streams   Source File: ElasticsearchReindexIT.java    License: 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 3
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 4
Source Project: foxtrot   Source File: DistinctAction.java    License: 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 5
Source Project: conductor   Source File: ElasticSearchDAOV6.java    License: 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 6
Source Project: elasticsearch-helper   Source File: BaseClient.java    License: 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 7
Source Project: spider   Source File: CommonWebpageDAO.java    License: 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 8
Source Project: fess   Source File: BsRoleCB.java    License: 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 9
Source Project: usergrid   Source File: EsEntityIndexImpl.java    License: 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 10
@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 11
Source Project: fess   Source File: BsThumbnailQueueCB.java    License: 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 12
Source Project: vertexium   Source File: ElasticsearchSearchQueryBase.java    License: 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 13
Source Project: fess   Source File: BsKeyMatchCB.java    License: 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
/**
     * 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 15
Source Project: streams   Source File: TwitterUserstreamElasticsearchIT.java    License: 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 16
Source Project: dremio-oss   Source File: ScanBuilder.java    License: 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 17
Source Project: streams   Source File: MongoElasticsearchSyncIT.java    License: 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 18
Source Project: conductor   Source File: ElasticSearchDAOV6.java    License: 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 19
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 20
Source Project: elasticsearch-sql   Source File: QueryAction.java    License: 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));
            }
        }
    }
}
 
Example 21
Source Project: sfs   Source File: GetNewestMasterKey.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Optional<PersistentMasterKey>> call(Void aVoid) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();


    TermQueryBuilder query = termQuery("algorithm_name", algorithmDef.getAlgorithmName());

    SearchRequestBuilder request =
            elasticSearch.get()
                    .prepareSearch(
                            elasticSearch.masterKeyTypeIndex())
                    .setVersion(true)
                    .setTypes(elasticSearch.defaultType())
                    .addSort("create_ts", DESC)
                    .setQuery(query)
                    .setSize(1)
                    .setTimeout(timeValueMillis(elasticSearch.getDefaultSearchTimeout() - 10));


    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Search Request {%s,%s} = %s", elasticSearch.defaultType(), elasticSearch.masterKeyTypeIndex(), Jsonify.toString(request)));
    }

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultSearchTimeout())
            .flatMap(oSearchResponse -> {
                SearchResponse searchResponse = oSearchResponse.get();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(format("Search Response {%s,%s} = %s", elasticSearch.defaultType(), elasticSearch.masterKeyTypeIndex(), Jsonify.toString(searchResponse)));
                }
                if (oSearchResponse.isPresent()) {
                    return from(oSearchResponse.get().getHits());
                } else {
                    return from(emptyList());
                }
            })
            .map(PersistentMasterKey::fromSearchHit)
            .map(Optional::of)
            .singleOrDefault(absent());
}
 
Example 22
protected Pair<SearchRequestBuilder,QueryBuilder> prepareSearchRequest(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInformation) {
    // All this Pair<SearchRequestBuilder,QueryBuilder> business b/c:
    //    a) Legacy eventing in search() needs the QueryBuilder, not just the SearchRequestBuilder, and
    //    b) SiteId handling entails manipulation of both objects, so presumably completeSearchRequestBuilders()
    //       would as well
    //    c) There is no getQuery() on SearchRequestBuilder
    Pair<SearchRequestBuilder,QueryBuilder> builders = newSearchRequestAndQueryBuilders(searchTerms, references, siteIds);
    builders = addSearchCoreParams(builders, searchTerms, references, siteIds);
    builders = addSearchQuery(builders, searchTerms, references, siteIds, additionalSearchInformation);
    builders = pairOf(addSearchResultFields(builders.getLeft()), builders.getRight());
    builders = pairOf(addSearchPagination(builders.getLeft(), start, end), builders.getRight());
    builders = pairOf(addSearchFacetting(builders.getLeft()), builders.getRight());
    return completeSearchRequestBuilders(builders, searchTerms, references, siteIds);
}
 
Example 23
@SuppressWarnings("unused")
private List<Map<String, Object>> queryWithConstraints(final String indexName, final String fieldName, final String fieldValue, final Map<String, String> constraints, boolean latest) throws IOException {
    SearchRequestBuilder request = this.elasticsearchClient.prepareSearch(indexName)
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setFrom(0);

    BoolQueryBuilder bFilter = QueryBuilders.boolQuery();
    bFilter.must(QueryBuilders.constantScoreQuery(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(fieldName, fieldValue))));
    for (Object o : constraints.entrySet()) {
        @SuppressWarnings("rawtypes")
        Map.Entry entry = (Map.Entry) o;
        bFilter.must(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery((String) entry.getKey(), ((String) entry.getValue()).toLowerCase())));
    }
    request.setQuery(bFilter);

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

    // evaluate search result
    ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits) {
        Map<String, Object> map = hit.getSourceAsMap();
        result.add(map);
    }

    return result;
}
 
Example 24
Source Project: cicada   Source File: AnnotationRepository.java    License: MIT License 5 votes vote down vote up
/**
 * 根据traceId和spanId获取annotationModel列表.
 */
public List<AnnotationModel> getSpanAnnotations(final String traceId, final String spanId) {
  // 声明SearchRequestBuilder实例
  final String indice = getDefaultIndice();
  final SearchRequestBuilder builder = client.prepareSearch(indice);
  builder.setTypes(props.getEsTypeName());

  // 设置查询条件
  final BoolQueryBuilder query = new BoolQueryBuilder();
  query.must(QueryBuilders.termQuery("traceId", traceId)) //
      .must(QueryBuilders.termQuery("spanId", spanId));

  // 执行查询
  final SearchResponse response = builder.setQuery(query).execute().actionGet();

  // 处理返回结果
  final List<AnnotationModel> annos = new LinkedList<AnnotationModel>();
  for (final SearchHit hit : response.getHits().hits()) {
    final String docStr = hit.getSourceAsString();
    try {
      final AnnotationModel model = JSON.parseObject(docStr, AnnotationModel.class);
      annos.add(model);
    } catch (JSONException ex) {
      log.error("failed load data {} to AnnotationModel, error {}", docStr, ex);
      continue;
    }
  }

  return annos;
}
 
Example 25
public void testLangDetectBinary() throws Exception {
    try {
        CreateIndexRequestBuilder createIndexRequestBuilder =
                new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE).setIndex("test");
        createIndexRequestBuilder.addMapping("someType", jsonBuilder()
                .startObject()
                .startObject("properties")
                .startObject("content")
                .field("type", "binary")
                .startObject("fields")
                .startObject("language")
                .field("type", "langdetect")
                .field("binary", true)
                .endObject()
                .endObject()
                .endObject()
                .endObject()
                .endObject());
        createIndexRequestBuilder.execute().actionGet();
        IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client(), IndexAction.INSTANCE)
                .setIndex("test").setType("someType").setId("1")
                //\"God Save the Queen\" (alternatively \"God Save the King\"
                .setSource("content", "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg==");
        indexRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
                .execute().actionGet();
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE)
                .setIndices("test")
                .setQuery(QueryBuilders.termQuery("content.language", "en"))
                .addStoredField("content.language");
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
        assertEquals(1L, searchResponse.getHits().getTotalHits());
        assertEquals("en", searchResponse.getHits().getAt(0).field("content.language").getValue());
    } finally {
        DeleteIndexRequestBuilder deleteIndexRequestBuilder =
                new DeleteIndexRequestBuilder(client(), DeleteIndexAction.INSTANCE, "test");
        deleteIndexRequestBuilder.execute().actionGet();
    }
}
 
Example 26
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 27
@Override
protected Pair<SearchRequestBuilder,QueryBuilder> completeSearchSuggestionsRequestBuilders(Pair<SearchRequestBuilder, QueryBuilder> builders,
                                                                                           String searchString,
                                                                                           String currentSite,
                                                                                           boolean allMySites) {
    return builders;
}
 
Example 28
Source Project: fess   Source File: EsAbstractBehavior.java    License: 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 29
Source Project: elasticsearch-sql   Source File: HashJoinElasticExecutor.java    License: Apache License 2.0 5 votes vote down vote up
private List<SearchHit> fetchAllHits(TableInJoinRequestBuilder tableInJoinRequest) {
    Integer hintLimit = tableInJoinRequest.getHintLimit();
    SearchRequestBuilder requestBuilder = tableInJoinRequest.getRequestBuilder();
    if (hintLimit != null && hintLimit < MAX_RESULTS_ON_ONE_FETCH) {
        requestBuilder.setSize(hintLimit);
        SearchResponse searchResponse = requestBuilder.get();
        updateMetaSearchResults(searchResponse);
        return Arrays.asList(searchResponse.getHits().getHits());
    }
    return scrollTillLimit(tableInJoinRequest, hintLimit);
}
 
Example 30
Source Project: vertexium   Source File: ElasticsearchSortingStrategy.java    License: Apache License 2.0 5 votes vote down vote up
void updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    SearchRequestBuilder q,
    QueryParameters parameters,
    SortDirection direction
);