Search in sources :

Example 1 with DocIdSetIterator

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

the class MinDocQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    return new ConstantScoreWeight(this) {

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final int maxDoc = context.reader().maxDoc();
            if (context.docBase + maxDoc <= minDoc) {
                return null;
            }
            final int segmentMinDoc = Math.max(0, minDoc - context.docBase);
            final DocIdSetIterator disi = new DocIdSetIterator() {

                int doc = -1;

                @Override
                public int docID() {
                    return doc;
                }

                @Override
                public int nextDoc() throws IOException {
                    return advance(doc + 1);
                }

                @Override
                public int advance(int target) throws IOException {
                    assert target > doc;
                    if (doc == -1) {
                        // skip directly to minDoc
                        doc = Math.max(target, segmentMinDoc);
                    } else {
                        doc = target;
                    }
                    if (doc >= maxDoc) {
                        doc = NO_MORE_DOCS;
                    }
                    return doc;
                }

                @Override
                public long cost() {
                    return maxDoc - segmentMinDoc;
                }
            };
            return new ConstantScoreScorer(this, score(), disi);
        }
    };
}
Also used : ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 2 with DocIdSetIterator

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

the class MinScoreScorer method twoPhaseIterator.

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator inTwoPhase = this.in.twoPhaseIterator();
    final DocIdSetIterator approximation = inTwoPhase == null ? in.iterator() : inTwoPhase.approximation();
    return new TwoPhaseIterator(approximation) {

        @Override
        public boolean matches() throws IOException {
            // otherwise calling score() is illegal
            if (inTwoPhase != null && inTwoPhase.matches() == false) {
                return false;
            }
            return in.score() >= minScore;
        }

        @Override
        public float matchCost() {
            return // random constant for the score computation
            1000f + (inTwoPhase == null ? 0 : inTwoPhase.matchCost());
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 3 with DocIdSetIterator

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

Example 4 with DocIdSetIterator

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

the class Lucene method asSequentialAccessBits.

/**
     * Given a {@link Scorer}, return a {@link Bits} instance that will match
     * all documents contained in the set. Note that the returned {@link Bits}
     * instance MUST be consumed in order.
     */
public static Bits asSequentialAccessBits(final int maxDoc, @Nullable Scorer scorer) throws IOException {
    if (scorer == null) {
        return new Bits.MatchNoBits(maxDoc);
    }
    final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
    final DocIdSetIterator iterator;
    if (twoPhase == null) {
        iterator = scorer.iterator();
    } else {
        iterator = twoPhase.approximation();
    }
    return new Bits() {

        int previous = -1;

        boolean previousMatched = false;

        @Override
        public boolean get(int index) {
            if (index < 0 || index >= maxDoc) {
                throw new IndexOutOfBoundsException(index + " is out of bounds: [" + 0 + "-" + maxDoc + "[");
            }
            if (index < previous) {
                throw new IllegalArgumentException("This Bits instance can only be consumed in order. " + "Got called on [" + index + "] while previously called on [" + previous + "]");
            }
            if (index == previous) {
                // twoPhase.matches() twice
                return previousMatched;
            }
            previous = index;
            int doc = iterator.docID();
            if (doc < index) {
                try {
                    doc = iterator.advance(index);
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot advance iterator", e);
                }
            }
            if (index == doc) {
                try {
                    return previousMatched = twoPhase == null || twoPhase.matches();
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot validate match", e);
                }
            }
            return previousMatched = false;
        }

        @Override
        public int length() {
            return maxDoc;
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) Bits(org.apache.lucene.util.Bits) IOException(java.io.IOException) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 5 with DocIdSetIterator

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

the class LongValuesComparatorSource method newComparator.

@Override
public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
    assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName());
    final Long dMissingValue = (Long) missingObject(missingValue, reversed);
    // the comparator doesn't check docsWithField since we replace missing values in select()
    return new FieldComparator.LongComparator(numHits, null, null) {

        @Override
        protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
            final SortedNumericDocValues values = indexFieldData.load(context).getLongValues();
            final NumericDocValues selectedValues;
            if (nested == null) {
                selectedValues = sortMode.select(values, dMissingValue);
            } else {
                final BitSet rootDocs = nested.rootDocs(context);
                final DocIdSetIterator innerDocs = nested.innerDocs(context);
                selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc());
            }
            return selectedValues;
        }
    };
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) BitSet(org.apache.lucene.util.BitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Aggregations

DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)68 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)28 Scorer (org.apache.lucene.search.Scorer)15 DocIdSet (org.apache.lucene.search.DocIdSet)14 Weight (org.apache.lucene.search.Weight)12 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)10 BitSet (org.apache.lucene.util.BitSet)10 Bits (org.apache.lucene.util.Bits)10 BytesRef (org.apache.lucene.util.BytesRef)10 IOException (java.io.IOException)9 MatchingDocs (org.apache.lucene.facet.FacetsCollector.MatchingDocs)8 IndexSearcher (org.apache.lucene.search.IndexSearcher)8 SortedDocValues (org.apache.lucene.index.SortedDocValues)7 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)7 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)7 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)6 Query (org.apache.lucene.search.Query)6 Document (org.apache.lucene.document.Document)5 IndexReader (org.apache.lucene.index.IndexReader)5 LeafReader (org.apache.lucene.index.LeafReader)5