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

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

Introduction

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

Prototype

public abstract int docID();

Source Link

Document

Returns the following:
  • -1 if #nextDoc() or #advance(int) were not called yet.

    Usage

    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//  w  ww  .  j  a v  a 2s.co 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  ww  w .  jav  a2s  .c  o  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.function.MinScoreScorerTests.java

    License:Apache License

    private static Scorer scorer(int maxDoc, final int[] docs, final float[] scores, final boolean twoPhase) {
        final DocIdSetIterator iterator = twoPhase ? DocIdSetIterator.all(maxDoc) : iterator(docs);
        return new Scorer(null) {
            public DocIdSetIterator iterator() {
                if (twoPhase) {
                    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
                } else {
                    return iterator;
                }//ww  w. j  ava2  s  . c  o m
            }
    
            public TwoPhaseIterator twoPhaseIterator() {
                if (twoPhase) {
                    return new TwoPhaseIterator(iterator) {
    
                        @Override
                        public boolean matches() throws IOException {
                            return Arrays.binarySearch(docs, iterator.docID()) >= 0;
                        }
    
                        @Override
                        public float matchCost() {
                            return 10;
                        }
                    };
                } else {
                    return null;
                }
            }
    
            @Override
            public int docID() {
                return iterator.docID();
            }
    
            @Override
            public float score() throws IOException {
                final int idx = Arrays.binarySearch(docs, docID());
                return scores[idx];
            }
    
            @Override
            public int freq() throws IOException {
                return 1;
            }
        };
    }
    

    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  w  w.  j  av  a 2s .  c  om*/
            if (current == target) {
                return true;
            } else {
                return docIdSetIterator.advance(target) == target;
            }
        }
    }
    

    From source file:org.elasticsearch.index.search.geo.GeoDistanceRangeQuery.java

    License:Apache License

    @Override
    public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
        final Weight boundingBoxWeight;
        if (boundingBoxFilter != null) {
            boundingBoxWeight = searcher.createNormalizedWeight(boundingBoxFilter, false);
        } else {// w w w .  j a  va  2  s.  co m
            boundingBoxWeight = null;
        }
        return new ConstantScoreWeight(this) {
            @Override
            public Scorer scorer(LeafReaderContext context) throws IOException {
                final DocIdSetIterator approximation;
                if (boundingBoxWeight != null) {
                    Scorer s = boundingBoxWeight.scorer(context);
                    if (s == null) {
                        // if the approximation does not match anything, we're done
                        return null;
                    }
                    approximation = s.iterator();
                } else {
                    approximation = DocIdSetIterator.all(context.reader().maxDoc());
                }
                final MultiGeoPointValues values = indexFieldData.load(context).getGeoPointValues();
                final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approximation) {
                    @Override
                    public boolean matches() throws IOException {
                        final int doc = approximation.docID();
                        values.setDocument(doc);
                        final int length = values.count();
                        for (int i = 0; i < length; i++) {
                            GeoPoint point = values.valueAt(i);
                            if (distanceBoundingCheck.isWithin(point.lat(), point.lon())) {
                                double d = fixedSourceDistance.calculate(point.lat(), point.lon());
                                if (d >= inclusiveLowerPoint && d <= inclusiveUpperPoint) {
                                    return true;
                                }
                            }
                        }
                        return false;
                    }
    
                    @Override
                    public float matchCost() {
                        if (distanceBoundingCheck == GeoDistance.ALWAYS_INSTANCE) {
                            return 0.0f;
                        } else {
                            // TODO: is this right (up to 4 comparisons from GeoDistance.SimpleDistanceBoundingCheck)?
                            return 4.0f;
                        }
                    }
                };
                return new ConstantScoreScorer(this, score(), twoPhaseIterator);
            }
        };
    }
    

    From source file:org.elasticsearch.index.search.geo.LegacyGeoDistanceRangeQuery.java

    License:Apache License

    @Override
    public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
        final Weight boundingBoxWeight;
        if (boundingBoxFilter != null) {
            boundingBoxWeight = searcher.createNormalizedWeight(boundingBoxFilter, false);
        } else {/* w ww .j  ava2  s . c om*/
            boundingBoxWeight = null;
        }
        return new ConstantScoreWeight(this) {
            @Override
            public Scorer scorer(LeafReaderContext context) throws IOException {
                final DocIdSetIterator approximation;
                if (boundingBoxWeight != null) {
                    Scorer s = boundingBoxWeight.scorer(context);
                    if (s == null) {
                        // if the approximation does not match anything, we're done
                        return null;
                    }
                    approximation = s.iterator();
                } else {
                    approximation = DocIdSetIterator.all(context.reader().maxDoc());
                }
                final MultiGeoPointValues values = indexFieldData.load(context).getGeoPointValues();
                final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approximation) {
                    @Override
                    public boolean matches() throws IOException {
                        final int doc = approximation.docID();
                        values.setDocument(doc);
                        final int length = values.count();
                        for (int i = 0; i < length; i++) {
                            GeoPoint point = values.valueAt(i);
                            if (bbox == null || GeoUtils.rectangleContainsPoint(bbox, point.lat(), point.lon())) {
                                double d = geoDistance.calculate(lat, lon, point.lat(), point.lon(),
                                        DistanceUnit.DEFAULT);
                                if (d >= inclusiveLowerPoint && d <= inclusiveUpperPoint) {
                                    return true;
                                }
                            }
                        }
                        return false;
                    }
    
                    @Override
                    public float matchCost() {
                        if (bbox != null) {
                            // always within bounds so we're going to compute distance for every point
                            return values.count();
                        } else {
                            // TODO: come up with better estimate of boundary points
                            return 4.0f;
                        }
                    }
                };
                return new ConstantScoreScorer(this, score(), twoPhaseIterator);
            }
        };
    }
    

    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}./*from ww w . ja v 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.composite.SortedDocsProducer.java

    License:Apache License

    /**
     * Visits all non-deleted documents in <code>iterator</code> and fills the provided <code>queue</code>
     * with the top composite buckets extracted from the collection.
     * Documents that contain a top composite bucket are added in the provided <code>builder</code> if it is not null.
     *
     * Returns true if the queue is full and the current <code>leadSourceBucket</code> did not produce any competitive
     * composite buckets./*from  w w w  .j  av a  2s  . c  om*/
     */
    protected boolean processBucket(CompositeValuesCollectorQueue queue, LeafReaderContext context,
            DocIdSetIterator iterator, Comparable<?> leadSourceBucket, @Nullable DocIdSetBuilder builder)
            throws IOException {
        final int[] topCompositeCollected = new int[1];
        final boolean[] hasCollected = new boolean[1];
        final LeafBucketCollector queueCollector = new LeafBucketCollector() {
            int lastDoc = -1;
    
            // we need to add the matching document in the builder
            // so we build a bulk adder from the approximate cost of the iterator
            // and rebuild the adder during the collection if needed
            int remainingBits = (int) Math.min(iterator.cost(), Integer.MAX_VALUE);
            DocIdSetBuilder.BulkAdder adder = builder == null ? null : builder.grow(remainingBits);
    
            @Override
            public void collect(int doc, long bucket) throws IOException {
                hasCollected[0] = true;
                int slot = queue.addIfCompetitive();
                if (slot != -1) {
                    topCompositeCollected[0]++;
                    if (adder != null && doc != lastDoc) {
                        if (remainingBits == 0) {
                            // the cost approximation was lower than the real size, we need to grow the adder
                            // by some numbers (128) to ensure that we can add the extra documents
                            adder = builder.grow(128);
                            remainingBits = 128;
                        }
                        adder.add(doc);
                        remainingBits--;
                        lastDoc = doc;
                    }
                }
            }
        };
        final Bits liveDocs = context.reader().getLiveDocs();
        final LeafBucketCollector collector = queue.getLeafCollector(leadSourceBucket, context, queueCollector);
        while (iterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
            if (liveDocs == null || liveDocs.get(iterator.docID())) {
                collector.collect(iterator.docID());
            }
        }
        if (queue.isFull() && hasCollected[0] && topCompositeCollected[0] == 0) {
            return true;
        }
        return false;
    }
    

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

    License:Apache License

    /**
     * Replay the wrapped collector, but only on a selection of buckets.
     *///  w w w.  j  a va2s  .  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.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  va2s  .  c o 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();
            }
        };
    }