Search in sources :

Example 1 with DocValuesTermsQuery

use of org.apache.lucene.search.DocValuesTermsQuery in project elasticsearch by elastic.

the class ParentIdQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    DocumentMapper childDocMapper = context.getMapperService().documentMapper(type);
    if (childDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]");
        }
    }
    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
    }
    String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type());
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST);
    // Need to take child type into account, otherwise a child doc of different type with the same id could match
    query.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER);
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) DocValuesTermsQuery(org.apache.lucene.search.DocValuesTermsQuery) Term(org.apache.lucene.index.Term)

Example 2 with DocValuesTermsQuery

use of org.apache.lucene.search.DocValuesTermsQuery in project elasticsearch by elastic.

the class ParentIdQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(ParentIdQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, Matchers.instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.clauses().size(), Matchers.equalTo(2));
    DocValuesTermsQuery idQuery = (DocValuesTermsQuery) booleanQuery.clauses().get(0).getQuery();
    // there are no getters to get the field and terms on DocValuesTermsQuery, so lets validate by creating a
    // new query based on the builder:
    assertThat(idQuery, Matchers.equalTo(new DocValuesTermsQuery("_parent#" + PARENT_TYPE, queryBuilder.getId())));
    TermQuery typeQuery = (TermQuery) booleanQuery.clauses().get(1).getQuery();
    assertThat(typeQuery.getTerm().field(), Matchers.equalTo(TypeFieldMapper.NAME));
    assertThat(typeQuery.getTerm().text(), Matchers.equalTo(queryBuilder.getType()));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) DocValuesTermsQuery(org.apache.lucene.search.DocValuesTermsQuery)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)2 DocValuesTermsQuery (org.apache.lucene.search.DocValuesTermsQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 Term (org.apache.lucene.index.Term)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 ParentFieldMapper (org.elasticsearch.index.mapper.ParentFieldMapper)1