Search in sources :

Example 16 with Scorer

use of org.apache.lucene.search.Scorer in project lucene-solr by apache.

the class QueryBitSetProducer method getBitSet.

@Override
public BitSet getBitSet(LeafReaderContext context) throws IOException {
    final LeafReader reader = context.reader();
    final IndexReader.CacheHelper cacheHelper = reader.getCoreCacheHelper();
    DocIdSet docIdSet = null;
    if (cacheHelper != null) {
        docIdSet = cache.get(cacheHelper.getKey());
    }
    if (docIdSet == null) {
        final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
        final IndexSearcher searcher = new IndexSearcher(topLevelContext);
        searcher.setQueryCache(null);
        final Weight weight = searcher.createNormalizedWeight(query, false);
        final Scorer s = weight.scorer(context);
        if (s == null) {
            docIdSet = DocIdSet.EMPTY;
        } else {
            docIdSet = new BitDocIdSet(BitSet.of(s.iterator(), context.reader().maxDoc()));
        }
        if (cacheHelper != null) {
            cache.put(cacheHelper.getKey(), docIdSet);
        }
    }
    return docIdSet == DocIdSet.EMPTY ? null : ((BitDocIdSet) docIdSet).bits();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) LeafReader(org.apache.lucene.index.LeafReader) IndexReader(org.apache.lucene.index.IndexReader) DocIdSet(org.apache.lucene.search.DocIdSet) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) Scorer(org.apache.lucene.search.Scorer) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) Weight(org.apache.lucene.search.Weight)

Example 17 with Scorer

use of org.apache.lucene.search.Scorer in project lucene-solr by apache.

the class TestBlockJoinValidation method testAdvanceValidationForToChildBjq.

public void testAdvanceValidationForToChildBjq() throws Exception {
    Query parentQuery = new MatchAllDocsQuery();
    ToChildBlockJoinQuery blockJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);
    final LeafReaderContext context = indexSearcher.getIndexReader().leaves().get(0);
    Weight weight = indexSearcher.createNormalizedWeight(blockJoinQuery, true);
    Scorer scorer = weight.scorer(context);
    final Bits parentDocs = parentsFilter.getBitSet(context);
    int target;
    do {
        // make the parent scorer advance to a doc ID which is not a parent
        target = TestUtil.nextInt(random(), 0, context.reader().maxDoc() - 2);
    } while (parentDocs.get(target + 1));
    final int illegalTarget = target;
    IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
        scorer.iterator().advance(illegalTarget);
    });
    assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Scorer(org.apache.lucene.search.Scorer) Bits(org.apache.lucene.util.Bits) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Weight(org.apache.lucene.search.Weight)

Example 18 with Scorer

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

the class TopHitsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(final LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    for (LongObjectPagedHashMap.Cursor<TopDocsAndLeafCollector> cursor : topDocsCollectors) {
        cursor.value.leafCollector = cursor.value.topLevelCollector.getLeafCollector(ctx);
    }
    return new LeafBucketCollectorBase(sub, null) {

        Scorer scorer;

        @Override
        public void setScorer(Scorer scorer) throws IOException {
            this.scorer = scorer;
            for (LongObjectPagedHashMap.Cursor<TopDocsAndLeafCollector> cursor : topDocsCollectors) {
                cursor.value.leafCollector.setScorer(scorer);
            }
            super.setScorer(scorer);
        }

        @Override
        public void collect(int docId, long bucket) throws IOException {
            TopDocsAndLeafCollector collectors = topDocsCollectors.get(bucket);
            if (collectors == null) {
                SortAndFormats sort = subSearchContext.sort();
                int topN = subSearchContext.from() + subSearchContext.size();
                if (sort == null) {
                    for (RescoreSearchContext rescoreContext : context.rescore()) {
                        topN = Math.max(rescoreContext.window(), topN);
                    }
                }
                // In the QueryPhase we don't need this protection, because it is build into the IndexSearcher,
                // but here we create collectors ourselves and we need prevent OOM because of crazy an offset and size.
                topN = Math.min(topN, subSearchContext.searcher().getIndexReader().maxDoc());
                TopDocsCollector<?> topLevelCollector;
                if (sort == null) {
                    topLevelCollector = TopScoreDocCollector.create(topN);
                } else {
                    topLevelCollector = TopFieldCollector.create(sort.sort, topN, true, subSearchContext.trackScores(), subSearchContext.trackScores());
                }
                collectors = new TopDocsAndLeafCollector(topLevelCollector);
                collectors.leafCollector = collectors.topLevelCollector.getLeafCollector(ctx);
                collectors.leafCollector.setScorer(scorer);
                topDocsCollectors.put(bucket, collectors);
            }
            collectors.leafCollector.collect(docId);
        }
    };
}
Also used : RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) LongObjectPagedHashMap(org.elasticsearch.common.util.LongObjectPagedHashMap) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) Scorer(org.apache.lucene.search.Scorer) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats)

Example 19 with Scorer

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

the class MatchedQueriesFetchSubPhase method hitsExecute.

@Override
public void hitsExecute(SearchContext context, SearchHit[] hits) {
    if (hits.length == 0 || // in case the request has only suggest, parsed query is null
    context.parsedQuery() == null) {
        return;
    }
    // don't modify the incoming hits
    hits = hits.clone();
    Arrays.sort(hits, (a, b) -> Integer.compare(a.docId(), b.docId()));
    @SuppressWarnings("unchecked") List<String>[] matchedQueries = new List[hits.length];
    for (int i = 0; i < matchedQueries.length; ++i) {
        matchedQueries[i] = new ArrayList<>();
    }
    Map<String, Query> namedQueries = new HashMap<>(context.parsedQuery().namedFilters());
    if (context.parsedPostFilter() != null) {
        namedQueries.putAll(context.parsedPostFilter().namedFilters());
    }
    try {
        for (Map.Entry<String, Query> entry : namedQueries.entrySet()) {
            String name = entry.getKey();
            Query query = entry.getValue();
            int readerIndex = -1;
            int docBase = -1;
            Weight weight = context.searcher().createNormalizedWeight(query, false);
            Bits matchingDocs = null;
            final IndexReader indexReader = context.searcher().getIndexReader();
            for (int i = 0; i < hits.length; ++i) {
                SearchHit hit = hits[i];
                int hitReaderIndex = ReaderUtil.subIndex(hit.docId(), indexReader.leaves());
                if (readerIndex != hitReaderIndex) {
                    readerIndex = hitReaderIndex;
                    LeafReaderContext ctx = indexReader.leaves().get(readerIndex);
                    docBase = ctx.docBase;
                    // scorers can be costly to create, so reuse them across docs of the same segment
                    Scorer scorer = weight.scorer(ctx);
                    matchingDocs = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), scorer);
                }
                if (matchingDocs.get(hit.docId() - docBase)) {
                    matchedQueries[i].add(name);
                }
            }
        }
        for (int i = 0; i < hits.length; ++i) {
            hits[i].matchedQueries(matchedQueries[i].toArray(new String[matchedQueries[i].size()]));
        }
    } catch (IOException e) {
        throw ExceptionsHelper.convertToElastic(e);
    } finally {
        context.clearReleasables(Lifetime.COLLECTION);
    }
}
Also used : Query(org.apache.lucene.search.Query) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) Scorer(org.apache.lucene.search.Scorer) IOException(java.io.IOException) Weight(org.apache.lucene.search.Weight) IndexReader(org.apache.lucene.index.IndexReader) Bits(org.apache.lucene.util.Bits) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with Scorer

use of org.apache.lucene.search.Scorer 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)

Aggregations

Scorer (org.apache.lucene.search.Scorer)46 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)26 Weight (org.apache.lucene.search.Weight)26 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)15 Query (org.apache.lucene.search.Query)13 IOException (java.io.IOException)12 Bits (org.apache.lucene.util.Bits)9 HashSet (java.util.HashSet)7 Set (java.util.Set)7 Document (org.apache.lucene.document.Document)7 IndexReader (org.apache.lucene.index.IndexReader)7 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)7 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)7 TermQuery (org.apache.lucene.search.TermQuery)7 Directory (org.apache.lucene.store.Directory)6 BitSet (org.apache.lucene.util.BitSet)6 BytesRef (org.apache.lucene.util.BytesRef)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SortedDocValues (org.apache.lucene.index.SortedDocValues)5