Search in sources :

Example 1 with NestedQueryBuilder

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

the class NestedHelperTests method testNested.

public void testNested() throws IOException {
    QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
    NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.Avg);
    ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("_type", "__nested1")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested1", new TermQueryBuilder("nested1.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // this time we do not add a filter since the inner query only matches inner docs
    expectedChildQuery = new TermQuery(new Term("nested1.foo", "bar"));
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested2", new TermQueryBuilder("nested2.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // we need to add the filter again because of include_in_parent
    expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested2")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested3", new TermQueryBuilder("nested3.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // we need to add the filter again because of include_in_root
    expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested3")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiReader(org.apache.lucene.index.MultiReader) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Term(org.apache.lucene.index.Term) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Aggregations

MultiReader (org.apache.lucene.index.MultiReader)1 Term (org.apache.lucene.index.Term)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Query (org.apache.lucene.search.Query)1 TermQuery (org.apache.lucene.search.TermQuery)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 NestedQueryBuilder (org.elasticsearch.index.query.NestedQueryBuilder)1 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)1 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)1