Example usage for org.apache.lucene.search DocIdSetIterator advance

List of usage examples for org.apache.lucene.search DocIdSetIterator advance

Introduction

In this page you can find the example usage for org.apache.lucene.search DocIdSetIterator advance.

Prototype

public abstract int advance(int target) throws IOException;

Source Link

Document

Advances to the first beyond the current whose document number is greater than or equal to target, and returns the document number itself.

Usage

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {NumericDoubleValues} instance that can be used to sort root documents
 * with this mode, the provided values and filters for root/inner documents.
 *
 * For every root document, the values of its inner documents will be aggregated.
 * If none of the inner documents has a value, then <code>missingValue</code> is returned.
 *
 * Allowed Modes: SUM, AVG, MIN, MAX//from w  ww .ja v  a2  s .c om
 *
 * NOTE: Calling the returned instance on docs that are not root docs is illegal
 *       The returned instance can only be evaluate the current and upcoming docs
 */
public NumericDoubleValues select(final SortedNumericDoubleValues values, final double missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(FieldData.emptySortedNumericDoubles(maxDoc), missingValue);
    }

    return new NumericDoubleValues() {

        int lastSeenRootDoc = 0;
        double lastEmittedValue = missingValue;

        @Override
        public double get(int rootDoc) {
            assert rootDocs.get(rootDoc) : "can only sort root documents";
            assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs";
            if (rootDoc == lastSeenRootDoc) {
                return lastEmittedValue;
            }
            try {
                final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1);
                final int firstNestedDoc;
                if (innerDocs.docID() > prevRootDoc) {
                    firstNestedDoc = innerDocs.docID();
                } else {
                    firstNestedDoc = innerDocs.advance(prevRootDoc + 1);
                }

                lastSeenRootDoc = rootDoc;
                lastEmittedValue = pick(values, missingValue, innerDocs, firstNestedDoc, rootDoc);
                return lastEmittedValue;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {BinaryDocValues} instance that can be used to sort root documents
 * with this mode, the provided values and filters for root/inner documents.
 *
 * For every root document, the values of its inner documents will be aggregated.
 * If none of the inner documents has a value, then <code>missingValue</code> is returned.
 *
 * Allowed Modes: MIN, MAX/*from   w w  w. j a  v  a 2  s  .  c  o  m*/
 *
 * NOTE: Calling the returned instance on docs that are not root docs is illegal
 *       The returned instance can only be evaluate the current and upcoming docs
 */
public BinaryDocValues select(final SortedBinaryDocValues values, final BytesRef missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(FieldData.emptySortedBinary(maxDoc), missingValue);
    }
    final BinaryDocValues selectedValues = select(values, null);

    return new BinaryDocValues() {

        final BytesRefBuilder builder = new BytesRefBuilder();

        int lastSeenRootDoc = 0;
        BytesRef lastEmittedValue = missingValue;

        @Override
        public BytesRef get(int rootDoc) {
            assert rootDocs.get(rootDoc) : "can only sort root documents";
            assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs";
            if (rootDoc == lastSeenRootDoc) {
                return lastEmittedValue;
            }

            try {
                final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1);
                final int firstNestedDoc;
                if (innerDocs.docID() > prevRootDoc) {
                    firstNestedDoc = innerDocs.docID();
                } else {
                    firstNestedDoc = innerDocs.advance(prevRootDoc + 1);
                }

                lastSeenRootDoc = rootDoc;
                lastEmittedValue = pick(selectedValues, builder, innerDocs, firstNestedDoc, rootDoc);
                if (lastEmittedValue == null) {
                    lastEmittedValue = missingValue;
                }
                return lastEmittedValue;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {SortedDocValues} instance that can be used to sort root documents
 * with this mode, the provided values and filters for root/inner documents.
 *
 * For every root document, the values of its inner documents will be aggregated.
 *
 * Allowed Modes: MIN, MAX//from w  w w .  ja va  2 s  .c  om
 *
 * NOTE: Calling the returned instance on docs that are not root docs is illegal
 *       The returned instance can only be evaluate the current and upcoming docs
 */
public SortedDocValues select(final RandomAccessOrds values, final BitSet rootDocs,
        final DocIdSetIterator innerDocs) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(DocValues.emptySortedSet());
    }
    final SortedDocValues selectedValues = select(values);

    return new SortedDocValues() {

        int lastSeenRootDoc = 0;
        int lastEmittedOrd = -1;

        @Override
        public BytesRef lookupOrd(int ord) {
            return selectedValues.lookupOrd(ord);
        }

        @Override
        public int getValueCount() {
            return selectedValues.getValueCount();
        }

        @Override
        public int getOrd(int rootDoc) {
            assert rootDocs.get(rootDoc) : "can only sort root documents";
            assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs";
            if (rootDoc == lastSeenRootDoc) {
                return lastEmittedOrd;
            }

            try {
                final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1);
                final int firstNestedDoc;
                if (innerDocs.docID() > prevRootDoc) {
                    firstNestedDoc = innerDocs.docID();
                } else {
                    firstNestedDoc = innerDocs.advance(prevRootDoc + 1);
                }

                lastSeenRootDoc = rootDoc;
                return lastEmittedOrd = pick(selectedValues, innerDocs, firstNestedDoc, rootDoc);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.codelibs.elasticsearch.search.profile.query.ProfileScorer.java

License:Apache License

@Override
public DocIdSetIterator iterator() {
    final DocIdSetIterator in = scorer.iterator();
    return new DocIdSetIterator() {

        @Override/*from w ww . j ava2s.c o m*/
        public int advance(int target) throws IOException {
            profile.startTime(QueryTimingType.ADVANCE);
            try {
                return in.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(QueryTimingType.NEXT_DOC);
            try {
                return in.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return in.docID();
        }

        @Override
        public long cost() {
            return in.cost();
        }
    };
}

From source file:org.codelibs.elasticsearch.search.profile.query.ProfileScorer.java

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }//from  w ww. ja  v a 2  s  . co m
    final DocIdSetIterator inApproximation = in.approximation();
    final DocIdSetIterator approximation = new DocIdSetIterator() {

        @Override
        public int advance(int target) throws IOException {
            profile.startTime(QueryTimingType.ADVANCE);
            try {
                return inApproximation.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(QueryTimingType.NEXT_DOC);
            try {
                return inApproximation.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return inApproximation.docID();
        }

        @Override
        public long cost() {
            return inApproximation.cost();
        }
    };
    return new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
            profile.startTime(QueryTimingType.MATCH);
            try {
                return in.matches();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public float matchCost() {
            return in.matchCost();
        }
    };
}

From source file:org.elasticsearch.common.lucene.search.XBooleanFilter.java

License:Apache License

static boolean iteratorMatch(DocIdSetIterator docIdSetIterator, int target) throws IOException {
    assert docIdSetIterator != null;
    int current = docIdSetIterator.docID();
    if (current == DocIdSetIterator.NO_MORE_DOCS || target < current) {
        return false;
    } else {/*from  w ww .jav a  2  s  . c om*/
        if (current == target) {
            return true;
        } else {
            return docIdSetIterator.advance(target) == target;
        }
    }
}

From source file:org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregator.java

License:Apache License

/**
 * Replay the documents that might contain a top bucket and pass top buckets to
 * the {@link this#deferredCollectors}.//w ww .j  av a2  s.  co  m
 */
private void runDeferredCollections() throws IOException {
    final boolean needsScores = needsScores();
    Weight weight = null;
    if (needsScores) {
        Query query = context.query();
        weight = context.searcher().createNormalizedWeight(query, true);
    }
    deferredCollectors.preCollection();
    for (Entry entry : entries) {
        DocIdSetIterator docIdSetIterator = entry.docIdSet.iterator();
        if (docIdSetIterator == null) {
            continue;
        }
        final LeafBucketCollector subCollector = deferredCollectors.getLeafCollector(entry.context);
        final LeafBucketCollector collector = queue.getLeafCollector(entry.context,
                getSecondPassCollector(subCollector));
        DocIdSetIterator scorerIt = null;
        if (needsScores) {
            Scorer scorer = weight.scorer(entry.context);
            if (scorer != null) {
                scorerIt = scorer.iterator();
                subCollector.setScorer(scorer);
            }
        }
        int docID;
        while ((docID = docIdSetIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (needsScores) {
                assert scorerIt != null && scorerIt.docID() < docID;
                scorerIt.advance(docID);
                // aggregations should only be replayed on matching documents
                assert scorerIt.docID() == docID;
            }
            collector.collect(docID);
        }
    }
    deferredCollectors.postCollection();
}

From source file:org.elasticsearch.search.aggregations.bucket.MergingBucketsDeferringCollector.java

License:Apache License

/**
 * Replay the wrapped collector, but only on a selection of buckets.
 *//*from   w w w. j  a  va  2 s.  c  o  m*/
@Override
public void prepareSelectedBuckets(long... selectedBuckets) throws IOException {
    if (finished == false) {
        throw new IllegalStateException(
                "Cannot replay yet, collection is not finished: postCollect() has not been called");
    }
    if (this.selectedBuckets != null) {
        throw new IllegalStateException("Already been replayed");
    }

    final LongHash hash = new LongHash(selectedBuckets.length, BigArrays.NON_RECYCLING_INSTANCE);
    for (long bucket : selectedBuckets) {
        hash.add(bucket);
    }
    this.selectedBuckets = hash;

    boolean needsScores = collector.needsScores();
    Weight weight = null;
    if (needsScores) {
        weight = searchContext.searcher().createNormalizedWeight(searchContext.query(), true);
    }
    for (Entry entry : entries) {
        final LeafBucketCollector leafCollector = collector.getLeafCollector(entry.context);
        DocIdSetIterator docIt = null;
        if (needsScores && entry.docDeltas.size() > 0) {
            Scorer scorer = weight.scorer(entry.context);
            // We don't need to check if the scorer is null
            // since we are sure that there are documents to replay
            // (entry.docDeltas it not empty).
            docIt = scorer.iterator();
            leafCollector.setScorer(scorer);
        }
        final PackedLongValues.Iterator docDeltaIterator = entry.docDeltas.iterator();
        final PackedLongValues.Iterator buckets = entry.buckets.iterator();
        int doc = 0;
        for (long i = 0, end = entry.docDeltas.size(); i < end; ++i) {
            doc += docDeltaIterator.next();
            final long bucket = buckets.next();
            final long rebasedBucket = hash.find(bucket);
            if (rebasedBucket != -1) {
                if (needsScores) {
                    if (docIt.docID() < doc) {
                        docIt.advance(doc);
                    }
                    // aggregations should only be replayed on matching
                    // documents
                    assert docIt.docID() == doc;
                }
                leafCollector.collect(doc, rebasedBucket);
            }
        }
    }

    collector.postCollection();
}

From source file:org.elasticsearch.search.fetch.matchedqueries.MatchedQueriesFetchSubPhase.java

License:Apache License

private void addMatchedQueries(HitContext hitContext, ImmutableMap<String, Filter> namedFiltersAndQueries,
        List<String> matchedQueries) {
    for (Map.Entry<String, Filter> entry : namedFiltersAndQueries.entrySet()) {
        String name = entry.getKey();
        Filter filter = entry.getValue();
        try {/*from   ww w .  j  a  v  a 2s.  c  o  m*/
            DocIdSet docIdSet = filter.getDocIdSet(hitContext.readerContext(), null); // null is fine, since we filter by hitContext.docId()
            if (!DocIdSets.isEmpty(docIdSet)) {
                Bits bits = docIdSet.bits();
                if (bits != null) {
                    if (bits.get(hitContext.docId())) {
                        matchedQueries.add(name);
                    }
                } else {
                    DocIdSetIterator iterator = docIdSet.iterator();
                    if (iterator != null) {
                        if (iterator.advance(hitContext.docId()) == hitContext.docId()) {
                            matchedQueries.add(name);
                        }
                    }
                }
            }
        } catch (IOException e) {
            // ignore
        } finally {
            SearchContext.current().clearReleasables();
        }
    }
}

From source file:org.elasticsearch.search.profile.ProfileScorer.java

License:Apache License

@Override
public DocIdSetIterator iterator() {
    final DocIdSetIterator in = scorer.iterator();
    return new DocIdSetIterator() {

        @Override/*from   w  w w. j a  v a 2 s .co m*/
        public int advance(int target) throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.ADVANCE);
            try {
                return in.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.NEXT_DOC);
            try {
                return in.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return in.docID();
        }

        @Override
        public long cost() {
            return in.cost();
        }
    };
}