当前位置: 首页>>代码示例>>Java>>正文


Java BoolQueryBuilder类代码示例

本文整理汇总了Java中org.elasticsearch.index.query.BoolQueryBuilder的典型用法代码示例。如果您正苦于以下问题:Java BoolQueryBuilder类的具体用法?Java BoolQueryBuilder怎么用?Java BoolQueryBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


BoolQueryBuilder类属于org.elasticsearch.index.query包,在下文中一共展示了BoolQueryBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
/**
 * Here to establish QueryBuilder patterns
 */
void test() {
	SearchSourceBuilder b;
	BoolQueryBuilder qb = QueryBuilders.boolQuery();
	QueryBuilder qbmm = QueryBuilders.multiMatchQuery("name", "fieldNames");
	QueryBuilder qbm = QueryBuilders.matchQuery("name", "text");
	QueryBuilder qbmp = QueryBuilders.matchPhraseQuery("name", "text");
	QueryBuilder qb1 = QueryBuilders.termQuery(ITQCoreOntology.TUPLE_SUBJECT_PROPERTY, "");
	QueryBuilder qb2 = QueryBuilders.termQuery(ITQCoreOntology.TUPLE_OBJECT_PROPERTY, "");
	QueryBuilder qb3 = QueryBuilders.termQuery(ITQCoreOntology.INSTANCE_OF_PROPERTY_TYPE, "");
	qb.must(qb3);
	qb.should(qb1);
	qb.should(qb2);
	QueryBuilder partial = QueryBuilders.regexpQuery("name", "regexp");
	QueryBuilder wildcard = QueryBuilders.wildcardQuery("name", "query");
}
 
开发者ID:topicquests,项目名称:backside-servlet-ks,代码行数:19,代码来源:ElasticQueryDSL.java

示例2: testSelectSort

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
/**
 * 分词 查询 商品名称 and 描述 价格排序
 */
@Test
public void testSelectSort() {
    //组装查询
    BoolQueryBuilder builder = boolQuery();
    builder.must(matchQuery("goodsName", "百事")).must(matchQuery("description", "百事"));

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    searchQuery.addSort(new Sort(Sort.Direction.DESC, new String[]{"price"}));

    Page<GoodsModel> page = elasticsearchTemplate.queryForPage(searchQuery, GoodsModel.class);
    System.out.println(page.getSize());

    List<GoodsModel> GoodsESDocs = page.getContent();

    System.out.println(JSON.toJSONString(GoodsESDocs));

    Assert.assertThat(page.getTotalElements(), is(2L));
}
 
开发者ID:aillamsun,项目名称:spring-boot-elastcsearch-example,代码行数:22,代码来源:GoodsESTest.java

示例3: testUnsupportedQueries

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public void testUnsupportedQueries() {
    RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D");
    RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now");
    PercolatorFieldMapper.verifyQuery(rangeQuery1);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(rangeQuery2));
    PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery1));
    expectThrows(IllegalArgumentException.class, () ->
            PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery2)));
    PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
    expectThrows(IllegalArgumentException.class, () ->
            PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(rangeQuery2)));
    PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
    expectThrows(IllegalArgumentException.class, () ->
            PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery2, new MatchAllQueryBuilder())));
    PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
    expectThrows(IllegalArgumentException.class, () ->
            PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery2, new RandomScoreFunctionBuilder())));

    HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));

    HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery)));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:27,代码来源:PercolatorFieldMapperTests.java

示例4: calculateStats

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public List<DateHistogramValue> calculateStats(String sourceUrl) {
    BoolQueryBuilder filter = QueryBuilders.boolQuery()
            .must(QueryBuilders.rangeQuery("created").gte("now-1M"))
            .must(QueryBuilders.termQuery("source", sourceUrl));

    SearchResponse response = getConnection().getClient()
            .prepareSearch(getIndex())
            .setTypes(getType())
            .setSearchType(SearchType.DEFAULT)
            .setQuery(filter)
            .addAggregation(AggregationBuilders
                    .dateHistogram("urls_over_time")
                    .field("created")
                    .format("yyyy-MM-dd")
                    .dateHistogramInterval(DateHistogramInterval.DAY))
            .setSize(0)
            .setFetchSource(true)
            .setExplain(false)
            .execute()
            .actionGet();

    InternalDateHistogram hits = response.getAggregations().get("urls_over_time");
    return hits.getBuckets().stream()
            .map(b -> new DateHistogramValue(b.getKeyAsString(), b.getDocCount()))
            .collect(Collectors.toList());
}
 
开发者ID:tokenmill,项目名称:crawling-framework,代码行数:27,代码来源:EsDocumentOperations.java

示例5: DfsOnlyRequest

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public DfsOnlyRequest(Fields termVectorsFields, String[] indices, String[] types, Set<String> selectedFields) throws IOException {
    super(indices);

    // build a search request with a query of all the terms
    final BoolQueryBuilder boolBuilder = boolQuery();
    for (String fieldName : termVectorsFields) {
        if ((selectedFields != null) && (!selectedFields.contains(fieldName))) {
            continue;
        }
        Terms terms = termVectorsFields.terms(fieldName);
        TermsEnum iterator = terms.iterator();
        while (iterator.next() != null) {
            String text = iterator.term().utf8ToString();
            boolBuilder.should(QueryBuilders.termQuery(fieldName, text));
        }
    }
    // wrap a search request object
    this.searchRequest = new SearchRequest(indices).types(types).source(new SearchSourceBuilder().query(boolBuilder));
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:20,代码来源:DfsOnlyRequest.java

示例6: fullTextQueryMultipleFields

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public String fullTextQueryMultipleFields(String textQuery, boolean isPhraseQuery,
		List<String> fieldNames, int start, int count) {
	BoolQueryBuilder qb = QueryBuilders.boolQuery();
	String fn;
	Iterator<String>itr = fieldNames.iterator();
	while (itr.hasNext()) {
		fn = itr.next();
		if (!isPhraseQuery)
			qb.should(QueryBuilders.matchQuery(fn, textQuery));
		else
			qb.should(QueryBuilders.matchPhraseQuery(fn, textQuery));
	}
	SearchSourceBuilder ssb = new SearchSourceBuilder();
	ssb.query(qb);
	ssb.from(start);
	if (count > -1)
		ssb.size(count);
	return ssb.toString();
}
 
开发者ID:topicquests,项目名称:backside-servlet-ks,代码行数:20,代码来源:ElasticQueryDSL.java

示例7: preparse

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
private QueryBuilder preparse(String q, int timezoneOffset) {
    // detect usage of OR connector usage.
    q = fixQueryMistakes(q);
    List<String> terms = splitIntoORGroups(q); // OR binds stronger than AND
    if (terms.size() == 0) return QueryBuilders.constantScoreQuery(QueryBuilders.matchAllQuery());
    
    // special handling
    if (terms.size() == 1) return parse(terms.get(0), timezoneOffset);

    // generic handling
    BoolQueryBuilder aquery = QueryBuilders.boolQuery();
    for (String t: terms) {
        QueryBuilder partial = parse(t, timezoneOffset);
        aquery.filter(partial);
    }
    return aquery;
}
 
开发者ID:yacy,项目名称:yacy_grid_mcp,代码行数:18,代码来源:LoklakQuery.java

示例8: orFilters

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
private QueryBuilder orFilters(List<QueryBuilder> filters) {
    if (filters.size() == 1) {
        return filters.get(0);
    } else {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        for (QueryBuilder filter : filters) {
            boolQuery.should(filter);
        }
        boolQuery.minimumShouldMatch(1);
        return boolQuery;
    }
}
 
开发者ID:mware-solutions,项目名称:memory-graph,代码行数:13,代码来源:ElasticsearchSearchVertexQuery.java

示例9: testThatTermsLookupGetRequestContainsContextAndHeaders

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public void testThatTermsLookupGetRequestContainsContextAndHeaders() throws Exception {
    transportClient().prepareIndex(lookupIndex, "type", "1")
        .setSource(jsonBuilder().startObject().array("followers", "foo", "bar", "baz").endObject()).get();
    transportClient().prepareIndex(queryIndex, "type", "1")
        .setSource(jsonBuilder().startObject().field("username", "foo").endObject()).get();
    transportClient().admin().indices().prepareRefresh(queryIndex, lookupIndex).get();

    TermsLookup termsLookup = new TermsLookup(lookupIndex, "type", "1", "followers");
    TermsQueryBuilder termsLookupFilterBuilder = QueryBuilders.termsLookupQuery("username", termsLookup);
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(termsLookupFilterBuilder);

    SearchResponse searchResponse = transportClient()
        .prepareSearch(queryIndex)
        .setQuery(queryBuilder)
        .get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1);

    assertGetRequestsContainHeaders();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:ContextAndHeaderTransportIT.java

示例10: testMatchQueryZeroTermsQuery

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public void testMatchQueryZeroTermsQuery() {
    assertAcked(prepareCreate("test")
            .addMapping("type1", "field1", "type=text,analyzer=classic", "field2", "type=text,analyzer=classic"));
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
    client().prepareIndex("test", "type1", "2").setSource("field1", "value2").get();
    refresh();

    BoolQueryBuilder boolQuery = boolQuery()
            .must(matchQuery("field1", "a").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE))
            .must(matchQuery("field1", "value1").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE));
    SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);

    boolQuery = boolQuery()
            .must(matchQuery("field1", "a").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL))
            .must(matchQuery("field1", "value1").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);

    boolQuery = boolQuery().must(matchQuery("field1", "a").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 2L);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:24,代码来源:SearchQueryIT.java

示例11: testMultiMatchQueryZeroTermsQuery

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public void testMultiMatchQueryZeroTermsQuery() {
    assertAcked(prepareCreate("test")
            .addMapping("type1", "field1", "type=text,analyzer=classic", "field2", "type=text,analyzer=classic"));
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").get();
    refresh();


    BoolQueryBuilder boolQuery = boolQuery()
            .must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE))
            .must(multiMatchQuery("value1", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE)); // Fields are ORed together
    SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);

    boolQuery = boolQuery()
            .must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL))
            .must(multiMatchQuery("value4", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);

    boolQuery = boolQuery().must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 2L);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:SearchQueryIT.java

示例12: randomBoolQuery

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
private static QueryBuilder randomBoolQuery(List<String> stringFields, List<String> numericFields, int numDocs, int depth) {
    QueryBuilder q = QueryBuilders.boolQuery();
    int numClause = randomIntBetween(0,5);
    for (int i = 0; i < numClause; i++) {
        ((BoolQueryBuilder)q).must(randomQueryBuilder(stringFields, numericFields,numDocs, depth -1));
    }

    numClause = randomIntBetween(0,5);
    for (int i = 0; i < numClause; i++) {
        ((BoolQueryBuilder)q).should(randomQueryBuilder(stringFields, numericFields,numDocs, depth -1));
    }

    numClause = randomIntBetween(0,5);
    for (int i = 0; i < numClause; i++) {
        ((BoolQueryBuilder)q).mustNot(randomQueryBuilder(stringFields, numericFields, numDocs, depth -1));
    }

    return q;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:20,代码来源:RandomQueryGenerator.java

示例13: findUrlsByStatusAndSource

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public List<HttpUrl> findUrlsByStatusAndSource(Enum status, String source, int count) {
    BoolQueryBuilder filter = QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("status", String.valueOf(status)))
            .must(QueryBuilders.termQuery("source", source));

    SearchResponse response = getConnection().getClient()
            .prepareSearch(getIndex())
            .setTypes(getType())
            .setSearchType(SearchType.DEFAULT)
            .setPostFilter(filter)
            .addSort("created", SortOrder.DESC)
            .setSize(count)
            .setFetchSource(true)
            .setExplain(false)
            .execute()
            .actionGet();

    SearchHits hits = response.getHits();
    return Arrays.stream(hits.getHits())
            .map(SearchHit::getSource)
            .map(s -> {
                HttpUrl httpUrl = new HttpUrl();
                httpUrl.setUrl(Objects.toString(s.get("url"), null));
                httpUrl.setPublished(Objects.toString(s.get("published"), null));
                httpUrl.setDiscovered(EsDataParser.nullOrDate(s.get("created")));
                httpUrl.setSource(source);
                return httpUrl;
            })
            .collect(Collectors.toList());
}
 
开发者ID:tokenmill,项目名称:crawling-framework,代码行数:31,代码来源:EsHttpUrlOperations.java

示例14: findEnabledSources

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
public List<HttpSource> findEnabledSources() {
    BoolQueryBuilder filter = QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("enabled", true));

    SearchResponse response = getConnection().getClient()
            .prepareSearch(getIndex())
            .setTypes(getType())
            .setSearchType(SearchType.DEFAULT)
            .setPostFilter(filter)
            .addSort("updated", SortOrder.ASC)
            .setSize(10000)
            .setFetchSource(true)
            .setExplain(false)
            .execute()
            .actionGet();

    SearchHits hits = response.getHits();
    return Arrays.stream(hits.getHits())
            .map(SearchHit::sourceAsMap)
            .map(this::mapToHttpSource)
            .collect(Collectors.toList());

}
 
开发者ID:tokenmill,项目名称:crawling-framework,代码行数:24,代码来源:EsHttpSourceOperations.java

示例15: applyFieldStrategy

import org.elasticsearch.index.query.BoolQueryBuilder; //导入依赖的package包/类
private void applyFieldStrategy(BoolQueryBuilder rootQuery, Function<String, QueryBuilder> fieldQueryResolver,
                                Set<? extends FactSearchCriteria.FieldStrategy> fieldStrategies,
                                FactSearchCriteria.MatchStrategy matchStrategy) {
  // Determine all fields to query.
  Set<String> fieldsToQuery = fieldStrategies.stream()
          .flatMap(strategy -> strategy.getFields().stream())
          .collect(Collectors.toSet());

  BoolQueryBuilder strategyQuery = boolQuery();
  for (String field : fieldsToQuery) {
    if (matchStrategy == FactSearchCriteria.MatchStrategy.all) {
      // Field query must match all fields.
      strategyQuery.filter(fieldQueryResolver.apply(field));
    } else {
      // Field query should match at least one field.
      strategyQuery.should(fieldQueryResolver.apply(field));
    }
  }

  rootQuery.filter(strategyQuery);
}
 
开发者ID:mnemonic-no,项目名称:act-platform,代码行数:22,代码来源:FactSearchManager.java


注:本文中的org.elasticsearch.index.query.BoolQueryBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。