use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class FilterIT method testEmptyFilterDeclarations.
// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
QueryBuilder emptyFilter = new BoolQueryBuilder();
SearchResponse response = client().prepareSearch("idx").addAggregation(filter("tag1", emptyFilter)).execute().actionGet();
assertSearchResponse(response);
Filter filter = response.getAggregations().get("tag1");
assertThat(filter, notNullValue());
assertThat(filter.getDocCount(), equalTo((long) numDocs));
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class ChildQuerySearchIT method testHighlightersIgnoreParentChild.
public void testHighlightersIgnoreParentChild() {
assertAcked(prepareCreate("test").addMapping("parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets").addMapping("child-type", "_parent", "type=parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets"));
client().prepareIndex("test", "parent-type", "parent-id").setSource("searchText", "quick brown fox").get();
client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("searchText", "quick brown fox").get();
refresh();
String[] highlightTypes = new String[] { "plain", "fvh", "postings" };
for (String highlightType : highlightTypes) {
logger.info("Testing with highlight type [{}]", highlightType);
SearchResponse searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("parent-id"));
HighlightField highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("child-id"));
highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
}
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class TemplateQueryBuilder method doRewrite.
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
BytesReference querySource = queryRewriteContext.getTemplateBytes(template);
try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(queryRewriteContext.getXContentRegistry(), querySource)) {
final QueryParseContext queryParseContext = queryRewriteContext.newParseContext(qSourceParser);
final QueryBuilder queryBuilder = queryParseContext.parseInnerQueryBuilder();
if (boost() != DEFAULT_BOOST || queryName() != null) {
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(queryBuilder);
return boolQueryBuilder;
}
return queryBuilder;
}
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class TemplateQueryBuilderTests method testRewriteWithInnerName.
public void testRewriteWithInnerName() throws IOException {
final String query = "{ \"match_all\" : {\"_name\" : \"foobar\"}}";
QueryBuilder builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query, Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap()));
assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(createShardContext()));
builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query, Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap())).queryName("outer");
assertEquals(new BoolQueryBuilder().must(new MatchAllQueryBuilder().queryName("foobar")).queryName("outer"), builder.rewrite(createShardContext()));
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class TemplateQueryBuilderTests method testRewriteWithInnerBoost.
public void testRewriteWithInnerBoost() throws IOException {
final TermQueryBuilder query = new TermQueryBuilder("foo", "bar").boost(2);
QueryBuilder builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query.toString(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap()));
assertEquals(query, builder.rewrite(createShardContext()));
builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query.toString(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap())).boost(3);
assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(createShardContext()));
}
Aggregations