Search in sources :

Example 6 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class RandomQueryGenerator method randomBoolQuery.

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;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FuzzyQueryBuilder(org.elasticsearch.index.query.FuzzyQueryBuilder)

Example 7 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testBoolQueryMinShouldMatchBiggerThanNumberOfShouldClauses.

public void testBoolQueryMinShouldMatchBiggerThanNumberOfShouldClauses() throws IOException {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", new String[] { "value1", "value2", "value3" }).get();
    client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get();
    refresh();
    BoolQueryBuilder boolQuery = boolQuery().must(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3));
    SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    boolQuery = boolQuery().must(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(1)).minimumShouldMatch(2);
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);
    boolQuery = boolQuery().should(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3)).minimumShouldMatch(1);
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    boolQuery = boolQuery().must(termQuery("field1", "value1")).must(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testMultiMatchQueryZeroTermsQuery.

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(// Fields are ORed together
    multiMatchQuery("value1", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE));
    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);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class SimpleQueryStringIT method testLenientFlagBeingTooLenient.

// Issue #7967
public void testLenientFlagBeingTooLenient() throws Exception {
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("num", 1, "body", "foo bar baz"), client().prepareIndex("test", "doc", "2").setSource("num", 2, "body", "eggplant spaghetti lasagna"));
    BoolQueryBuilder q = boolQuery().should(simpleQueryStringQuery("bar").field("num").field("body").lenient(true));
    SearchResponse resp = client().prepareSearch("test").setQuery(q).get();
    assertNoFailures(resp);
    // the bug is that this would be parsed into basically a match_all
    // query and this would match both documents
    assertHitCount(resp, 1);
    assertSearchHits(resp, "1");
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 10 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class ContextAndHeaderTransportIT method testThatTermsLookupGetRequestContainsContextAndHeaders.

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();
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) TermsLookup(org.elasticsearch.indices.TermsLookup) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)98 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)15 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)9 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)9 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)5 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)5 Map (java.util.Map)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)4 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 StickyFacetBuilder (org.sonar.server.es.StickyFacetBuilder)4 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)3 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)3