Search in sources :

Example 6 with ObjectMapper

use of org.elasticsearch.index.mapper.ObjectMapper in project elasticsearch by elastic.

the class FetchPhase method getInternalNestedIdentity.

private SearchHit.NestedIdentity getInternalNestedIdentity(SearchContext context, int nestedSubDocId, LeafReaderContext subReaderContext, DocumentMapper documentMapper, ObjectMapper nestedObjectMapper) throws IOException {
    int currentParent = nestedSubDocId;
    ObjectMapper nestedParentObjectMapper;
    ObjectMapper current = nestedObjectMapper;
    String originalName = nestedObjectMapper.name();
    SearchHit.NestedIdentity nestedIdentity = null;
    do {
        Query parentFilter;
        nestedParentObjectMapper = documentMapper.findParentObjectMapper(current);
        if (nestedParentObjectMapper != null) {
            if (nestedParentObjectMapper.nested().isNested() == false) {
                current = nestedParentObjectMapper;
                continue;
            }
            parentFilter = nestedParentObjectMapper.nestedTypeFilter();
        } else {
            parentFilter = Queries.newNonNestedFilter();
        }
        Query childFilter = nestedObjectMapper.nestedTypeFilter();
        if (childFilter == null) {
            current = nestedParentObjectMapper;
            continue;
        }
        final Weight childWeight = context.searcher().createNormalizedWeight(childFilter, false);
        Scorer childScorer = childWeight.scorer(subReaderContext);
        if (childScorer == null) {
            current = nestedParentObjectMapper;
            continue;
        }
        DocIdSetIterator childIter = childScorer.iterator();
        BitSet parentBits = context.bitsetFilterCache().getBitSetProducer(parentFilter).getBitSet(subReaderContext);
        int offset = 0;
        int nextParent = parentBits.nextSetBit(currentParent);
        for (int docId = childIter.advance(currentParent + 1); docId < nextParent && docId != DocIdSetIterator.NO_MORE_DOCS; docId = childIter.nextDoc()) {
            offset++;
        }
        currentParent = nextParent;
        current = nestedObjectMapper = nestedParentObjectMapper;
        int currentPrefix = current == null ? 0 : current.name().length() + 1;
        nestedIdentity = new SearchHit.NestedIdentity(originalName.substring(currentPrefix), offset, nestedIdentity);
        if (current != null) {
            originalName = current.name();
        }
    } while (current != null);
    return nestedIdentity;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Query(org.apache.lucene.search.Query) BitSet(org.apache.lucene.util.BitSet) Scorer(org.apache.lucene.search.Scorer) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper) Weight(org.apache.lucene.search.Weight)

Example 7 with ObjectMapper

use of org.elasticsearch.index.mapper.ObjectMapper in project elasticsearch by elastic.

the class ReverseNestedAggregationBuilder method doBuild.

@Override
protected AggregatorFactory<?> doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException {
    if (findNestedAggregatorFactory(parent) == null) {
        throw new SearchParseException(context, "Reverse nested aggregation [" + name + "] can only be used inside a [nested] aggregation", null);
    }
    ObjectMapper parentObjectMapper = null;
    if (path != null) {
        parentObjectMapper = context.getObjectMapper(path);
        if (parentObjectMapper == null) {
            return new ReverseNestedAggregatorFactory(name, true, null, context, parent, subFactoriesBuilder, metaData);
        }
        if (parentObjectMapper.nested().isNested() == false) {
            throw new AggregationExecutionException("[reverse_nested] nested path [" + path + "] is not nested");
        }
    }
    NestedScope nestedScope = context.getQueryShardContext().nestedScope();
    try {
        nestedScope.nextLevel(parentObjectMapper);
        return new ReverseNestedAggregatorFactory(name, false, parentObjectMapper, context, parent, subFactoriesBuilder, metaData);
    } finally {
        nestedScope.previousLevel();
    }
}
Also used : SearchParseException(org.elasticsearch.search.SearchParseException) NestedScope(org.elasticsearch.index.query.support.NestedScope) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Aggregations

ObjectMapper (org.elasticsearch.index.mapper.ObjectMapper)7 Query (org.apache.lucene.search.Query)3 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)2 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)2 SearchHit (org.elasticsearch.search.SearchHit)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Scorer (org.apache.lucene.search.Scorer)1 Weight (org.apache.lucene.search.Weight)1 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)1 BitSet (org.apache.lucene.util.BitSet)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 Nested (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)1 CustomFieldsVisitor (org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor)1 FieldsVisitor (org.elasticsearch.index.fieldvisitor.FieldsVisitor)1