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;
}
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);
}
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);
}
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");
}
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();
}
Aggregations