Search in sources :

Example 1 with NestedHelper

use of org.elasticsearch.index.search.NestedHelper in project elasticsearch by elastic.

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter());
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    // in its child space
    if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
        innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
    }
    return new ESToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) NestedHelper(org.elasticsearch.index.search.NestedHelper) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 2 with NestedHelper

use of org.elasticsearch.index.search.NestedHelper in project elasticsearch by elastic.

the class DefaultSearchContext method buildFilteredQuery.

@Override
public Query buildFilteredQuery(Query query) {
    List<Query> filters = new ArrayList<>();
    Query typeFilter = createTypeFilter(queryShardContext.getTypes());
    if (typeFilter != null) {
        filters.add(typeFilter);
    }
    if (mapperService().hasNested() && // when a _type filter is set, it will automatically exclude nested docs
    typeFilter == null && new NestedHelper(mapperService()).mightMatchNestedDocs(query) && (aliasFilter == null || new NestedHelper(mapperService()).mightMatchNestedDocs(aliasFilter))) {
        filters.add(Queries.newNonNestedFilter());
    }
    if (aliasFilter != null) {
        filters.add(aliasFilter);
    }
    if (sliceBuilder != null) {
        filters.add(sliceBuilder.toFilter(queryShardContext, shardTarget().getShardId().getId(), queryShardContext.getIndexSettings().getNumberOfShards()));
    }
    if (filters.isEmpty()) {
        return query;
    } else {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(query, Occur.MUST);
        for (Query filter : filters) {
            builder.add(filter, Occur.FILTER);
        }
        return builder.build();
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SliceBuilder(org.elasticsearch.search.slice.SliceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) ArrayList(java.util.ArrayList) NestedHelper(org.elasticsearch.index.search.NestedHelper)

Aggregations

Query (org.apache.lucene.search.Query)2 NestedHelper (org.elasticsearch.index.search.NestedHelper)2 ArrayList (java.util.ArrayList)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)1 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)1 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)1 ObjectMapper (org.elasticsearch.index.mapper.ObjectMapper)1 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)1 ParsedQuery (org.elasticsearch.index.query.ParsedQuery)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 ESToParentBlockJoinQuery (org.elasticsearch.index.search.ESToParentBlockJoinQuery)1 SliceBuilder (org.elasticsearch.search.slice.SliceBuilder)1