org.elasticsearch.index.query.QueryBuilders Java Examples

The following examples show how to use org.elasticsearch.index.query.QueryBuilders. 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: ESSearchTest.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Test
public void termQuery() throws IOException {
    queryPair.setFieldNames(new String[] {"productName"});
    queryPair.setContent("android");
    queryCondition.setQueryBuilder(QueryBuilders.termQuery(queryPair.getFieldNames()[0], queryPair.getContent()));
    List<Spu> list = elasticsearchTemplate.analyzeSearchResponse(Spu.class,
        elasticsearchTemplate.executeQuery("es_test", queryCondition, "type"));

    log.info("json string is:{}", mapper.writeValueAsString(list));
    log.info("list size is:{}", list.size());

    assertThat(list.size(), is(1));
    assertThat(list.get(0), equalTo(spu1));

    queryPair.setContent("android手机");
    queryCondition.setQueryBuilder(QueryBuilders.termQuery(queryPair.getFieldNames()[0], queryPair.getContent()));
    list = elasticsearchTemplate.analyzeSearchResponse(Spu.class,
        elasticsearchTemplate.executeQuery("es_test", queryCondition, "type"));

    log.info("json string is:{}", mapper.writeValueAsString(list));
    log.info("list size is:{}", list.size());

    assertThat(list.size(), is(0));
}
 
Example #2
Source File: EkmKnowledgeMasterRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public List<EkmKnowledgeMaster> findByOrgi(String orgi) {

	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder bq = QueryBuilders.boolQuery() ; 
	bq.must(QueryBuilders.termQuery("orgi", orgi)) ;
	boolQueryBuilder.must(bq); 
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(new PageRequest(0, 100000)) ;
	Page<EkmKnowledgeMaster> knowledgeList = null ;
	if(elasticsearchTemplate.indexExists(EkmKnowledgeMaster.class)){
		knowledgeList = elasticsearchTemplate.queryForPage(searchQueryBuilder.build() , EkmKnowledgeMaster.class ) ;
    }
	
	return knowledgeList.getContent();
}
 
Example #3
Source File: FuzzySearchTest.java    From Spring-Boot-Book with Apache License 2.0 6 votes vote down vote up
@Test
/**
 * Description: 前缀查询prefixQuery。
 */
public void prefixQuery() {
    // 查询条件
    NativeSearchQueryBuilder nativeSearchQueryBuilderQueryBuilder = new NativeSearchQueryBuilder();
    //  左右模糊
    nativeSearchQueryBuilderQueryBuilder.withQuery(QueryBuilders.prefixQuery("name","士"));
    // 搜索,获取结果
    Page<Product>  products= productRepository.search(nativeSearchQueryBuilderQueryBuilder.build());
    for (Product product : products) {
        System.out.println(product);
    }

}
 
Example #4
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> searchRecentRunningWorkflows(int lastModifiedHoursAgoFrom,
    int lastModifiedHoursAgoTo) {
    DateTime dateTime = new DateTime();
    QueryBuilder q = QueryBuilders.boolQuery()
        .must(QueryBuilders.rangeQuery("updateTime")
            .gt(dateTime.minusHours(lastModifiedHoursAgoFrom)))
        .must(QueryBuilders.rangeQuery("updateTime")
            .lt(dateTime.minusHours(lastModifiedHoursAgoTo)))
        .must(QueryBuilders.termQuery("status", "RUNNING"));

    SearchRequestBuilder s = elasticSearchClient.prepareSearch(indexName)
        .setTypes("workflow")
        .setQuery(q)
        .setSize(5000)
        .addSort("updateTime", SortOrder.ASC);

    SearchResponse response = s.execute().actionGet();
    return StreamSupport.stream(response.getHits().spliterator(), false)
        .map(SearchHit::getId)
        .collect(Collectors.toCollection(LinkedList::new));
}
 
Example #5
Source File: EsQueryVisitor.java    From occurrence with Apache License 2.0 6 votes vote down vote up
/**
 * handle disjunction predicate
 *
 * @param predicate disjunction predicate
 * @param queryBuilder  root query builder
 */
public void visit(DisjunctionPredicate predicate, BoolQueryBuilder queryBuilder) throws QueryBuildingException {
  Map<OccurrenceSearchParameter, List<EqualsPredicate>> equalsPredicatesReplaceableByIn = groupEquals(predicate);

  predicate.getPredicates().forEach(subPredicate -> {
    try {
      if (!isReplaceableByInPredicate(subPredicate, equalsPredicatesReplaceableByIn)) {
        BoolQueryBuilder shouldQueryBuilder = QueryBuilders.boolQuery();
        visit(subPredicate, shouldQueryBuilder);
        queryBuilder.should(shouldQueryBuilder);
      }
    } catch (QueryBuildingException ex) {
      throw new RuntimeException(ex);
    }
  });
  if (!equalsPredicatesReplaceableByIn.isEmpty()) {
    toInPredicates(equalsPredicatesReplaceableByIn)
      .forEach(ep -> queryBuilder.should().add(QueryBuilders.termsQuery(getElasticField(ep.getKey()),
                                                                        ep.getValues().stream()
                                                                          .map(v -> parseParamValue(v, ep.getKey()))
                                                                          .collect(Collectors.toList()))));
  }
}
 
Example #6
Source File: DateRangeApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void dateRange1() throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
            RangeQueryBuilder matchQueryBuilder = QueryBuilders.rangeQuery("ctm")
                    .from("2018-07-01 07:27:59.733",true)
                    .to("2018-07-01 07:30:00.000",false);
//            matchQueryBuilder.format("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式
//            matchQueryBuilder.timeZone("+08:00");//设置时区

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(matchQueryBuilder);
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(5);

            SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
            searchRequest.types("log");//限定type
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);


        }finally{
            HighLevelClient.close();
        }
    }
 
Example #7
Source File: EsInstanceStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<EsNameMetaData> getMetaDataByName(String field, String name) throws Exception {

  Preconditions.checkNotNull(field);
  Preconditions.checkNotNull(name);

  QueryBuilder queryBuilder = QueryBuilders.boolQuery()
      .must(QueryBuilders.termQuery(field, name))
      .must(QueryBuilders.termQuery("state", "running"));

  ScrollableResponse<List<EsNameMetaData>> response =
      this.retrieveScrollByQuery(queryBuilder,
          EsMapper.getIncludeFields(EsNameMetaData.class, this.getClass()), BATCHSIZE,
          str -> updateMapper.readValue(str, EsNameMetaData.class));

  EsIterator<EsNameMetaData> iterator =
      new EsIterator<>(response, r -> scrollNext(r.getContinousToken(),
          str -> updateMapper.readValue(str, EsNameMetaData.class)));

  return iterator;
}
 
Example #8
Source File: ByPageTest.java    From Spring-Boot-Book with Apache License 2.0 6 votes vote down vote up
@Test
/**
 * Description: 分页查询+排序
 */
public void searchByPageAndSort() {
    // 分页:
    int page = 0;
    int size = 5;//每页文档数

    // 构建查询条件
    NativeSearchQueryBuilder nativeSearchQueryBuilderQueryBuilder = new NativeSearchQueryBuilder();
    // 查询词,只能查询一个汉字,或者一个英文单词
    nativeSearchQueryBuilderQueryBuilder.withQuery(QueryBuilders.termQuery("name", "富"));
    // 搜索,获取结果
    nativeSearchQueryBuilderQueryBuilder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
    nativeSearchQueryBuilderQueryBuilder.withPageable(PageRequest.of(page, size));
    Page<Product> products = productRepository.search(nativeSearchQueryBuilderQueryBuilder.build());
    // 总条数
    for (Product product : products) {
        System.out.println(product);
    }

}
 
Example #9
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<KbsTopic> getTopicByCon(BoolQueryBuilder boolQueryBuilder, final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(KbsTopic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class);
    }
    return pages ; 
}
 
Example #10
Source File: ElasticsearchFieldValueScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example #11
Source File: ClusteringActionIT.java    From elasticsearch-carrot2 with Apache License 2.0 6 votes vote down vote up
public void testAttributes() throws IOException {
    LingoClusteringAlgorithm algorithm = new LingoClusteringAlgorithm();
    algorithm.desiredClusterCount.set(5);

    Map<String, Object> extract = Attrs.extract(algorithm);
    Attrs.populate(algorithm, extract);

    ClusteringActionResponse result = new ClusteringActionRequestBuilder(client)
        .setQueryHint("data mining")
        .addSourceFieldMapping("title", LogicalField.TITLE)
        .addSourceFieldMapping("content", LogicalField.CONTENT)
        .addAttributes(Attrs.extract(algorithm))
        .setSearchRequest(
          client.prepareSearch()
                .setIndices(INDEX_TEST)
                .setSize(100)
                .setQuery(QueryBuilders.matchAllQuery())
                .setFetchSource(new String[] {"title", "content"}, null))
        .execute().actionGet();

    checkValid(result);
    checkJsonSerialization(result);

    Assertions.assertThat(result.getDocumentGroups().length)
        .isBetween(0, 5 + 1);
}
 
Example #12
Source File: ElasticSearchClient.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public SearchHits queryBoostedQueryIndex(String boostedQuery, String field, int resultsSize) throws IOException
{
	if(client==null)
		return null;
	
	QueryBuilder query = QueryBuilders.queryStringQuery(boostedQuery).defaultField(field);
	SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	sourceBuilder.query(query);
	sourceBuilder.size(resultsSize);
	
	SearchRequest searchRequest = new SearchRequest();
	searchRequest.source(sourceBuilder);
	
	SearchResponse searchResponse = client.search(searchRequest);
	return searchResponse.getHits();
}
 
Example #13
Source File: ESSearchTest.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Test
public void rangeQuery() throws IOException {
    queryPair.setFieldNames(new String[] {"productName"});
    QueryBuilder queryBuilder = QueryBuilders.rangeQuery(queryPair.getFieldNames()[0]).from("android").to("服装")
        .includeLower(true).includeUpper(true);
    queryCondition.setQueryBuilder(queryBuilder);

    List<Spu> list = elasticsearchTemplate.analyzeSearchResponse(Spu.class,
        elasticsearchTemplate.executeQuery("es_test", queryCondition, "type"));
    log.info("json string is:{}", mapper.writeValueAsString(list));
    log.info("list size is:{}", list.size());

    assertThat(list.size(), is(3));
    assertThat(list.get(0), equalTo(spu2));
    assertThat(list.get(1), equalTo(spu1));
    assertThat(list.get(2), equalTo(spu3));
}
 
Example #14
Source File: QueryMaker.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void explanWhere(BoolQueryBuilder boolQuery, Where where) throws SqlParseException {

		//zhongshu-comment 暂时只遇到了该sql:select a,b,c as my_c from tbl where a = 1,会走这个分支
		if (where instanceof Condition) {
			addSubQuery(
					boolQuery,
					where,
					(QueryBuilder) make((Condition) where) //zhongshu-comment 重点方法 就是这里解析最细粒度的where条件
			);
		} else {
			/*
			zhongshu-comment select a,b,c as my_c from tbl where a = 1 or b = 2 and (c = 3 or d = 4) or e > 1
			上面这条sql中的“b = 2 and (c = 3 or d = 4)”这部分会走该分支,
			因为“b = 2 and (c = 3 or d = 4)”被封装为Where类型的对象,而不是Condition对象
			对应的具体笔记见:搜索-->es插件开发-->es-sql-->代码阅读-->如何解析where条件
			 */
			BoolQueryBuilder subQuery = QueryBuilders.boolQuery();

			//zhongshu-comment 将subQuery对象纳入到boolQuery中,即boolQuery是上一级,subQuery是下一级
			addSubQuery(boolQuery, where, subQuery);
			for (Where subWhere : where.getWheres()) {
				//zhongshu-comment 然后又将subWhere对象纳入到subQuery对象中,通过递归就能层层解析出这个Where条件了:“b = 2 and (c = 3 or d = 4)”
				explanWhere(subQuery, subWhere);
			}
		}
	}
 
Example #15
Source File: ElasticsearchSearchVertexQuery.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private QueryBuilder getEdgeFilter() {
    switch (direction) {
        case BOTH:
            QueryBuilder inVertexIdFilter = getDirectionInEdgeFilter();
            QueryBuilder outVertexIdFilter = getDirectionOutEdgeFilter();
            return QueryBuilders.boolQuery()
                .should(inVertexIdFilter)
                .should(outVertexIdFilter)
                .minimumShouldMatch(1);
        case OUT:
            return getDirectionOutEdgeFilter();
        case IN:
            return getDirectionInEdgeFilter();
        default:
            throw new VertexiumException("unexpected direction: " + direction);
    }
}
 
Example #16
Source File: OccurrenceSearchEsImpl.java    From occurrence with Apache License 2.0 6 votes vote down vote up
private <T> T searchByKey(Long key, Function<SearchHit, T> mapper) {
  //This should be changed to use GetRequest once ElasticSearch stores id correctly
  SearchRequest searchRequest = new SearchRequest();
  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  searchSourceBuilder.size(1);
  searchRequest.indices(esIndex);
  searchSourceBuilder.query(QueryBuilders.termQuery(OccurrenceEsField.GBIF_ID.getFieldName(), key));
  searchRequest.source(searchSourceBuilder);
  try {
    SearchHits hits = esClient.search(searchRequest, HEADERS.get()).getHits();
    if (hits != null && hits.totalHits > 0) {
      return mapper.apply(hits.getAt(0));
    }
    return null;
  } catch (IOException ex) {
    throw new SearchException(ex);
  }
}
 
Example #17
Source File: ElasticSearchUtilImpl.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<String> getTableIdsByCatalogs(final String type, final List<QualifiedName> qualifiedNames,
                                          final List<QualifiedName> excludeQualifiedNames) {
    List<String> ids = Lists.newArrayList();
    final QueryBuilder queryBuilder = QueryBuilders.boolQuery()
        .must(QueryBuilders.termsQuery("name.qualifiedName.tree", qualifiedNames))
        .must(QueryBuilders.termQuery("deleted_", false))
        .mustNot(QueryBuilders.termsQuery("name.qualifiedName.tree", excludeQualifiedNames));

    // Run the query and get the response.
    final SearchRequestBuilder request = client.prepareSearch(esIndex)
        .setTypes(type)
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(queryBuilder)
        .setSize(Integer.MAX_VALUE)  // TODO May break if too many tables returned back, change to Scroll
        .setFetchSource(false);
    final SearchResponse response = request.execute().actionGet(esCallTimeout);
    if (response.getHits().getHits().length != 0) {
        ids = getIds(response);
    }
    return ids;
}
 
Example #18
Source File: SearchHelper.java    From fess with Apache License 2.0 6 votes vote down vote up
public List<Map<String, Object>> getDocumentListByDocIds(final String[] docIds, final String[] fields,
        final OptionalThing<FessUserBean> userBean, final SearchRequestType searchRequestType) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    return ComponentUtil.getFessEsClient().getDocumentList(
            fessConfig.getIndexDocumentSearchIndex(),
            builder -> {
                final BoolQueryBuilder boolQuery =
                        QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(fessConfig.getIndexFieldDocId(), docIds));
                final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
                if (searchRequestType != SearchRequestType.ADMIN_SEARCH) {
                    final Set<String> roleSet = ComponentUtil.getRoleQueryHelper().build(searchRequestType);
                    if (!roleSet.isEmpty()) {
                        queryHelper.buildRoleQuery(roleSet, boolQuery);
                    }
                }
                builder.setQuery(boolQuery);
                builder.setSize(fessConfig.getPagingSearchPageMaxSizeAsInteger());
                builder.setFetchSource(fields, null);
                queryHelper.processSearchPreference(builder, userBean, String.join(StringUtil.EMPTY, docIds));
                return true;
            });
}
 
Example #19
Source File: ElasticSearchCleanupComponentBrowseTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private QueryBuilder getLastDownloadQuery(final String value)
{
  BoolQueryBuilder neverDownloadDownloadBuilder = QueryBuilders.boolQuery();
  neverDownloadDownloadBuilder.mustNot(existsQuery(LAST_DOWNLOADED_KEY));
  neverDownloadDownloadBuilder.filter(
      rangeQuery(LAST_BLOB_UPDATED_KEY)
          .lte(format(NOW_MINUS_SECONDS, value))
  );

  RangeQueryBuilder lastDownloadRangeBuilder = rangeQuery(LAST_DOWNLOADED_KEY)
      .lte(format(NOW_MINUS_SECONDS, value));

  BoolQueryBuilder lastDownloadShouldBuilder = QueryBuilders.boolQuery();
  lastDownloadShouldBuilder.must(lastDownloadRangeBuilder);

  BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery();
  filterBuilder.should(lastDownloadShouldBuilder);
  filterBuilder.should(neverDownloadDownloadBuilder);

  return filterBuilder;
}
 
Example #20
Source File: DetectorMappingRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Override
public List<DetectorMapping> findLastUpdated(int timeInSeconds) {
    val sourceBuilder = new SearchSourceBuilder();
    val boolQuery = QueryBuilders.boolQuery();
    val fromTime = System.currentTimeMillis() - timeInSeconds * 1000;
    boolQuery.must(new RangeQueryBuilder(LAST_MOD_TIME_KEYWORD).gt(fromTime));
    sourceBuilder.query(boolQuery);
    //FIXME setting default result set size to 500.
    sourceBuilder.size(500);
    val searchRequest =
            new SearchRequest()
                    .source(sourceBuilder)
                    .indices(elasticSearchProperties.getIndexName())
                    .types(elasticSearchProperties.getDocType());
    return getDetectorMappings(searchRequest);
}
 
Example #21
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @return void
 * @Author pancm
 * @Description exist查询
 * @Date 2019/9/17
 * @Param []
 **/
private static void existSearch() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    // 设置查询条件
    sourceBuilder.query(QueryBuilders.existsQuery("msgcode"));
    searchRequest.source(sourceBuilder);
    System.out.println("存在查询的DSL语句:"+sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 结果
    searchResponse.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("存在查询的Map结果:" + map);
        System.out.println("存在查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
}
 
Example #22
Source File: ClusteringActionIT.java    From elasticsearch-carrot2 with Apache License 2.0 6 votes vote down vote up
public void testPropagatingAlgorithmException() {
    // The query should result in an error.
    try {
        // Out of allowed range (should cause an exception).
        Map<String,Object> attrs = new HashMap<>();
        attrs.put("ignoreWordIfInHigherDocsPercent", Double.MAX_VALUE);

        new ClusteringActionRequestBuilder(client)
            .setQueryHint("")
            .addSourceFieldMapping("title", LogicalField.TITLE)
            .addSourceFieldMapping("content", LogicalField.CONTENT)
            .setAlgorithm(STCClusteringAlgorithm.NAME)
            .addAttributes(attrs)
            .setSearchRequest(
              client.prepareSearch()
                    .setIndices(INDEX_TEST)
                    .setSize(100)
                    .setQuery(QueryBuilders.termQuery("content", "data"))
                    .setFetchSource(new String[] {"title", "content"}, null))
            .execute().actionGet();
        throw Preconditions.unreachable();
    } catch (ElasticsearchException e) {
        Assertions.assertThat(e)
            .hasMessageContaining("Clustering error:");
    }
}
 
Example #23
Source File: TestElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private SearchResponse search(String indexName, String structuredQuery, int start,
    int size, String freeTextQuery, String docType) throws ParserException {
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    if (StringUtils.isNotEmpty(structuredQuery)) {
        Expression expression = Expression.fromString(structuredQuery);
        queryBuilder = expression.getFilterBuilder();
    }

    BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
    QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
    BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);
    final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(indexName)
        .setQuery(fq)
        .setTypes(docType)
        .storedFields("_id")
        .setFrom(start)
        .setSize(size);

    return srb.get();
}
 
Example #24
Source File: PercentileRankAggregationMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast");
        PercentileRanksAggregationBuilder aggregationBuilder = AggregationBuilders.percentileRanks("utm_ranks",new double[]{200,500,1000,3000,8000}).field("utm").keyed(false);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

    }finally{
        HighLevelClient.close();
    }
}
 
Example #25
Source File: ElasticSearchIndexerTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void deleteMessage() {
    DocumentId documentId = DocumentId.fromString("1:2");
    String content = "{\"message\": \"trying out Elasticsearch\"}";

    testee.index(documentId, content, useDocumentId(documentId)).block();
    elasticSearch.awaitForElasticSearch();

    testee.delete(ImmutableList.of(documentId), useDocumentId(documentId)).block();
    elasticSearch.awaitForElasticSearch();
    
    SearchResponse searchResponse = client.search(
        new SearchRequest(INDEX_NAME.getValue())
            .source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())),
        RequestOptions.DEFAULT)
        .block();
    assertThat(searchResponse.getHits().getTotalHits()).isEqualTo(0);
}
 
Example #26
Source File: ElasticsearchHighRestFactory.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(StringUtil.isEmpty(indexs))indexs="_all";
		if(xclient==null){
			init();
		}
		SearchSourceBuilder search = new SearchSourceBuilder();
		search.query(QueryBuilders.queryStringQuery(condition)); 
		search.explain(false);
		SearchRequest request = new SearchRequest();
		request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.source(search);
		request.indices(indexs.split(","));
		request.types(types.split(","));
		SearchResponse response = xclient.search(request);
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #27
Source File: JestExample.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
/**
 * 查询全部
 * @throws Exception
 */
private static void searchAll() throws Exception {
	JestClient jestClient = JestExample.getJestClient();
	SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	searchSourceBuilder.query(QueryBuilders.matchAllQuery());
	Search search = new Search.Builder(searchSourceBuilder.toString())
			.addIndex("article")
			.build();
	SearchResult result = jestClient.execute(search);
	System.out.println("本次查询共查到:"+result.getTotal()+"篇文章!");
	List<Hit<Article,Void>> hits = result.getHits(Article.class);
	for (Hit<Article, Void> hit : hits) {
		Article source = hit.source;
		System.out.println("标题:"+source.getTitle());
		System.out.println("内容:"+source.getContent());
		System.out.println("url:"+source.getUrl());
		System.out.println("来源:"+source.getSource());
		System.out.println("作者:"+source.getAuthor());
	}
}
 
Example #28
Source File: NestedQueryDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    NestedQueryBuilder nestedQueryBuilder = QueryBuilders
            .nestedQuery(
                    "keywords",
                    QueryBuilders.termQuery("keywords.keyword","北京"), ScoreMode.None);
}
 
Example #29
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 5 votes vote down vote up
public QueryBuilder getQuery() {
    if (queryBuilderList == null) {
        return null;
    } else if (queryBuilderList.size() == 1) {
        return queryBuilderList.get(0);
    }
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    queryBuilderList.forEach(query -> {
        boolQuery.must(query);
    });
    return boolQuery;
}
 
Example #30
Source File: ElasticsearchAnySearchDAO.java    From syncope with Apache License 2.0 5 votes vote down vote up
private Pair<DisMaxQueryBuilder, Set<String>> adminRealmsFilter(final Set<String> adminRealms) {
    DisMaxQueryBuilder builder = QueryBuilders.disMaxQuery();

    Set<String> dynRealmKeys = new HashSet<>();
    RealmUtils.normalize(adminRealms).forEach(realmPath -> {
        if (realmPath.startsWith("/")) {
            Realm realm = realmDAO.findByFullPath(realmPath);
            if (realm == null) {
                SyncopeClientException noRealm = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
                noRealm.getElements().add("Invalid realm specified: " + realmPath);
                throw noRealm;
            } else {
                realmDAO.findDescendants(realm).forEach(
                        descendant -> builder.add(QueryBuilders.termQuery("realm", descendant.getFullPath())));
            }
        } else {
            DynRealm dynRealm = dynRealmDAO.find(realmPath);
            if (dynRealm == null) {
                LOG.warn("Ignoring invalid dynamic realm {}", realmPath);
            } else {
                dynRealmKeys.add(dynRealm.getKey());
                builder.add(QueryBuilders.termQuery("dynRealm", dynRealm.getKey()));
            }
        }
    });
    if (!dynRealmKeys.isEmpty()) {
        realmDAO.findAll().forEach(
                descendant -> builder.add(QueryBuilders.termQuery("realm", descendant.getFullPath())));
    }

    return Pair.of(builder, dynRealmKeys);
}