Search in sources :

Example 1 with Bits

use of org.apache.lucene.util.Bits in project elasticsearch by elastic.

the class PercolateQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight verifiedMatchesWeight = verifiedMatchesQuery.createWeight(searcher, false);
    final Weight candidateMatchesWeight = candidateMatchesQuery.createWeight(searcher, false);
    return new Weight(this) {

        @Override
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                TwoPhaseIterator twoPhaseIterator = scorer.twoPhaseIterator();
                int result = twoPhaseIterator.approximation().advance(docId);
                if (result == docId) {
                    if (twoPhaseIterator.matches()) {
                        if (needsScores) {
                            CheckedFunction<Integer, Query, IOException> percolatorQueries = queryStore.getQueries(leafReaderContext);
                            Query query = percolatorQueries.apply(docId);
                            Explanation detail = percolatorIndexSearcher.explain(query, 0);
                            return Explanation.match(scorer.score(), "PercolateQuery", detail);
                        } else {
                            return Explanation.match(scorer.score(), "PercolateQuery");
                        }
                    }
                }
            }
            return Explanation.noMatch("PercolateQuery");
        }

        @Override
        public float getValueForNormalization() throws IOException {
            return candidateMatchesWeight.getValueForNormalization();
        }

        @Override
        public void normalize(float v, float v1) {
            candidateMatchesWeight.normalize(v, v1);
        }

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = candidateMatchesWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }
            final CheckedFunction<Integer, Query, IOException> queries = queryStore.getQueries(leafReaderContext);
            if (needsScores) {
                return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {

                    float score;

                    @Override
                    boolean matchDocId(int docId) throws IOException {
                        Query query = percolatorQueries.apply(docId);
                        if (query != null) {
                            TopDocs topDocs = percolatorIndexSearcher.search(query, 1);
                            if (topDocs.totalHits > 0) {
                                score = topDocs.scoreDocs[0].score;
                                return true;
                            } else {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }

                    @Override
                    public float score() throws IOException {
                        return score;
                    }
                };
            } else {
                Scorer verifiedDocsScorer = verifiedMatchesWeight.scorer(leafReaderContext);
                Bits verifiedDocsBits = Lucene.asSequentialAccessBits(leafReaderContext.reader().maxDoc(), verifiedDocsScorer);
                return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {

                    @Override
                    public float score() throws IOException {
                        return 0f;
                    }

                    boolean matchDocId(int docId) throws IOException {
                        // the MemoryIndex verification.
                        if (verifiedDocsBits.get(docId)) {
                            return true;
                        }
                        Query query = percolatorQueries.apply(docId);
                        return query != null && Lucene.exists(percolatorIndexSearcher, query);
                    }
                };
            }
        }
    };
}
Also used : Set(java.util.Set) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) Query(org.apache.lucene.search.Query) Explanation(org.apache.lucene.search.Explanation) Scorer(org.apache.lucene.search.Scorer) IOException(java.io.IOException) Weight(org.apache.lucene.search.Weight) TopDocs(org.apache.lucene.search.TopDocs) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits)

Example 2 with Bits

use of org.apache.lucene.util.Bits in project elasticsearch by elastic.

the class IndexShardTestCase method getShardDocUIDs.

protected Set<Uid> getShardDocUIDs(final IndexShard shard) throws IOException {
    shard.refresh("get_uids");
    try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
        Set<Uid> ids = new HashSet<>();
        for (LeafReaderContext leafContext : searcher.reader().leaves()) {
            LeafReader reader = leafContext.reader();
            Bits liveDocs = reader.getLiveDocs();
            for (int i = 0; i < reader.maxDoc(); i++) {
                if (liveDocs == null || liveDocs.get(i)) {
                    Document uuid = reader.document(i, Collections.singleton(UidFieldMapper.NAME));
                    ids.add(Uid.createUid(uuid.get(UidFieldMapper.NAME)));
                }
            }
        }
        return ids;
    }
}
Also used : Uid(org.elasticsearch.index.mapper.Uid) LeafReader(org.apache.lucene.index.LeafReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) Document(org.apache.lucene.document.Document) Engine(org.elasticsearch.index.engine.Engine) HashSet(java.util.HashSet)

Example 3 with Bits

use of org.apache.lucene.util.Bits in project elasticsearch by elastic.

the class InternalEngineTests method getSeqNosSet.

private static FixedBitSet getSeqNosSet(final IndexReader reader, final long highestSeqNo) throws IOException {
    // _seq_no are stored as doc values for the time being, so this is how we get them
    // (as opposed to using an IndexSearcher or IndexReader)
    final FixedBitSet bitSet = new FixedBitSet((int) highestSeqNo + 1);
    final List<LeafReaderContext> leaves = reader.leaves();
    if (leaves.isEmpty()) {
        return bitSet;
    }
    for (int i = 0; i < leaves.size(); i++) {
        final LeafReader leaf = leaves.get(i).reader();
        final NumericDocValues values = leaf.getNumericDocValues(SeqNoFieldMapper.NAME);
        if (values == null) {
            continue;
        }
        final Bits bits = leaf.getLiveDocs();
        for (int docID = 0; docID < leaf.maxDoc(); docID++) {
            if (bits == null || bits.get(docID)) {
                final long seqNo = values.get(docID);
                assertFalse("should not have more than one document with the same seq_no[" + seqNo + "]", bitSet.get((int) seqNo));
                bitSet.set((int) seqNo);
            }
        }
    }
    return bitSet;
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) LongPoint(org.apache.lucene.document.LongPoint)

Example 4 with Bits

use of org.apache.lucene.util.Bits in project elasticsearch by elastic.

the class Versions method loadPrimaryTerm.

/**
     * Returns the primary term for the given uid term, returning {@code 0} if none is found.
     */
public static long loadPrimaryTerm(IndexReader reader, Term term) throws IOException {
    assert term.field().equals(UidFieldMapper.NAME) : "can only load _primary_term by uid";
    List<LeafReaderContext> leaves = reader.leaves();
    if (leaves.isEmpty()) {
        return 0;
    }
    // which are likely to be in the last segments
    for (int i = leaves.size() - 1; i >= 0; i--) {
        LeafReader leaf = leaves.get(i).reader();
        Bits liveDocs = leaf.getLiveDocs();
        TermsEnum termsEnum = null;
        NumericDocValues dvField = null;
        PostingsEnum docsEnum = null;
        final Fields fields = leaf.fields();
        if (fields != null) {
            Terms terms = fields.terms(UidFieldMapper.NAME);
            if (terms != null) {
                termsEnum = terms.iterator();
                assert termsEnum != null;
                dvField = leaf.getNumericDocValues(SeqNoFieldMapper.PRIMARY_TERM_NAME);
                assert dvField != null;
                final BytesRef id = term.bytes();
                if (termsEnum.seekExact(id)) {
                    // there may be more than one matching docID, in the
                    // case of nested docs, so we want the last one:
                    docsEnum = termsEnum.postings(docsEnum, 0);
                    int docID = DocIdSetIterator.NO_MORE_DOCS;
                    for (int d = docsEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = docsEnum.nextDoc()) {
                        if (liveDocs != null && liveDocs.get(d) == false) {
                            continue;
                        }
                        docID = d;
                    }
                    if (docID != DocIdSetIterator.NO_MORE_DOCS) {
                        return dvField.get(docID);
                    }
                }
            }
        }
    }
    return 0;
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) Fields(org.apache.lucene.index.Fields) LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 5 with Bits

use of org.apache.lucene.util.Bits in project elasticsearch by elastic.

the class Lucene method exists.

/**
     * Check whether there is one or more documents matching the provided query.
     */
public static boolean exists(IndexSearcher searcher, Query query) throws IOException {
    final Weight weight = searcher.createNormalizedWeight(query, false);
    // match than the bulk scorer API
    for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
        final Scorer scorer = weight.scorer(context);
        if (scorer == null) {
            continue;
        }
        final Bits liveDocs = context.reader().getLiveDocs();
        final DocIdSetIterator iterator = scorer.iterator();
        for (int doc = iterator.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iterator.nextDoc()) {
            if (liveDocs == null || liveDocs.get(doc)) {
                return true;
            }
        }
    }
    return false;
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Scorer(org.apache.lucene.search.Scorer) Bits(org.apache.lucene.util.Bits) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) Weight(org.apache.lucene.search.Weight)

Aggregations

Bits (org.apache.lucene.util.Bits)121 Document (org.apache.lucene.document.Document)37 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)35 Directory (org.apache.lucene.store.Directory)32 IOException (java.io.IOException)26 BytesRef (org.apache.lucene.util.BytesRef)23 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)19 LeafReader (org.apache.lucene.index.LeafReader)18 ArrayList (java.util.ArrayList)16 StringField (org.apache.lucene.document.StringField)16 NumericDocValues (org.apache.lucene.index.NumericDocValues)16 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 IndexReader (org.apache.lucene.index.IndexReader)12 HashSet (java.util.HashSet)11 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)11 TermsEnum (org.apache.lucene.index.TermsEnum)10 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)10 FixedBitSet (org.apache.lucene.util.FixedBitSet)10 Terms (org.apache.lucene.index.Terms)9