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

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

Introduction

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

Prototype

DocIdSetIterator

Source Link

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/* ww w.ja  v a  2  s.c om*/
        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;
    }/*w w  w .ja v a  2s  .  c om*/
    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.docset.GetDocSet.java

License:Apache License

@Override
public DocIdSetIterator iterator() throws IOException {
    return new DocIdSetIterator() {
        private int doc = -1;

        @Override/*from   w w  w. j  a v  a2 s.  co  m*/
        public int docID() {
            return doc;
        }

        @Override
        public int nextDoc() throws IOException {
            do {
                doc++;
                if (doc >= maxDoc) {
                    return doc = NO_MORE_DOCS;
                }
            } while (!get(doc));
            return doc;
        }

        @Override
        public int advance(int target) throws IOException {
            if (target >= maxDoc) {
                return doc = NO_MORE_DOCS;
            }
            doc = target;
            while (!get(doc)) {
                doc++;
                if (doc >= maxDoc) {
                    return doc = NO_MORE_DOCS;
                }
            }
            return doc;
        }
    };
}

From source file:org.elasticsearch.common.lucene.search.function.MinScoreScorerTests.java

License:Apache License

private static DocIdSetIterator iterator(final int... docs) {
    return new DocIdSetIterator() {

        int i = -1;

        @Override/*from  w  ww  .j a  va  2 s . c om*/
        public int nextDoc() throws IOException {
            if (i + 1 == docs.length) {
                return NO_MORE_DOCS;
            } else {
                return docs[++i];
            }
        }

        @Override
        public int docID() {
            return i < 0 ? -1 : i == docs.length ? NO_MORE_DOCS : docs[i];
        }

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

        @Override
        public int advance(int target) throws IOException {
            return slowAdvance(target);
        }
    };
}

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  ww  .j  ava  2s  .  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();
        }
    };
}

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

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }/* w w  w  . j  av  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(ProfileBreakdown.TimingType.ADVANCE);
            try {
                return inApproximation.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.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(ProfileBreakdown.TimingType.MATCH);
            try {
                return in.matches();
            } finally {
                profile.stopAndRecordTime();
            }
        }

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

From source file:org.hippoecm.repository.query.lucene.util.MultiDocIdSet.java

License:Apache License

@Override
public DocIdSetIterator iterator() throws IOException {
    return new DocIdSetIterator() {

        int docID = -1;
        int docOffset = 0;
        int docIdSetIndex = 0;
        DocIdSetIterator currentDocIdSetIterator = (docIdSets.length == 0) ? null : docIdSets[0].iterator();

        @Override// w  w w . jav  a2s. c om
        public int docID() {
            return docID;
        }

        @Override
        public int nextDoc() throws IOException {
            while (currentDocIdSetIterator != null) {
                int currentDocIdSetDocId = currentDocIdSetIterator.nextDoc();
                if (currentDocIdSetDocId != NO_MORE_DOCS) {
                    docID = docOffset + currentDocIdSetDocId;
                    return docID;
                }
                pointCurrentToNextIterator();
            }
            docID = NO_MORE_DOCS;
            return docID;
        }

        /**
         * if there is no next iterator, currentDocIdSetIterator becomes null
         */
        private void pointCurrentToNextIterator() throws IOException {
            currentDocIdSetIterator = null;
            while (currentDocIdSetIterator == null && docIdSetIndex + 1 < docIdSets.length) {
                docOffset += maxDocs[docIdSetIndex];
                docIdSetIndex++;
                currentDocIdSetIterator = docIdSets[docIdSetIndex].iterator();
            }
        }

        @Override
        public int advance(final int target) throws IOException {
            while (currentDocIdSetIterator != null) {
                int relative = target - docOffset;
                if (relative < 0) {
                    relative = 0;
                }
                int currentDocIdSetDocId = currentDocIdSetIterator.advance(relative);
                if (currentDocIdSetDocId != NO_MORE_DOCS) {
                    docID = docOffset + currentDocIdSetDocId;
                    return docID;
                }
                pointCurrentToNextIterator();
            }
            docID = NO_MORE_DOCS;
            return docID;
        }
    };
}

From source file:org.meresco.lucene.queries.KeyFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(final AtomicReaderContext context, Bits acceptDocs) throws IOException {
    return BitsFilteredDocIdSet.wrap(new DocIdSet() {
        @Override//  ww  w . ja  v a  2 s  .c om
        public DocIdSetIterator iterator() throws IOException {
            return new DocIdSetIterator() {
                private int[] keyValuesArray = KeyValuesCache.get(context, keyName);
                private int maxDoc = context.reader().maxDoc();
                int docId;

                @Override
                public int docID() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public int nextDoc() throws IOException {
                    if (keyValuesArray != null) {
                        try {
                            while (this.docId < this.maxDoc) {
                                int key = this.keyValuesArray[this.docId];
                                if (keySet.get(key)) {
                                    return this.docId++;
                                }
                                docId++;
                            }
                        } catch (IndexOutOfBoundsException e) {
                        }
                    }
                    this.docId = DocIdSetIterator.NO_MORE_DOCS;
                    return this.docId;
                }

                @Override
                public int advance(int target) throws IOException {
                    this.docId = target;
                    return nextDoc();
                }

                @Override
                public long cost() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, acceptDocs);
}

From source file:org.meresco.lucene.suggestion.SuggestionNGramKeysFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(final AtomicReaderContext context, Bits acceptDocs) throws IOException {
    return BitsFilteredDocIdSet.wrap(new DocIdSet() {
        @Override//from ww  w  . ja  va 2 s .c o  m
        public DocIdSetIterator iterator() throws IOException {
            return new DocIdSetIterator() {
                private BinaryDocValues keysDocValues = FieldCache.DEFAULT.getTerms(context.reader(), keyName,
                        false);
                private int maxDoc = context.reader().maxDoc();
                int docId;

                @Override
                public int docID() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public int nextDoc() throws IOException {
                    while (this.docId < this.maxDoc) {
                        String keys = this.keysDocValues.get(this.docId).utf8ToString();
                        for (String key : keys.split("\\|")) {
                            if (keySet.get(Integer.parseInt(key))) {
                                return this.docId++;
                            }
                        }
                        docId++;
                    }
                    this.docId = DocIdSetIterator.NO_MORE_DOCS;
                    return this.docId;
                }

                @Override
                public int advance(int target) throws IOException {
                    this.docId = target;
                    return nextDoc();
                }

                @Override
                public long cost() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, acceptDocs);
}

From source file:perf.RandomQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {
        @Override//from   w  ww  .j  a v  a 2s  . c o  m
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final int maxDoc = context.reader().maxDoc();
            final int interval = (int) (1 / fractionKeep);
            final DocIdSetIterator iterator = 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 {
                    if (target >= maxDoc) {
                        return doc = NO_MORE_DOCS;
                    }
                    int intervalId = target / interval;
                    int addend = (31 * intervalId) % interval;
                    doc = intervalId * interval + addend;
                    if (doc < target) {
                        intervalId++;
                        addend = (31 * intervalId) % interval;
                        doc = intervalId * interval + addend;
                    }
                    assert doc >= target;
                    if (doc >= maxDoc) {
                        return doc = NO_MORE_DOCS;
                    }
                    return doc;
                }

                @Override
                public long cost() {
                    return maxDoc / interval;
                }

            };
            return new ConstantScoreScorer(this, score(), iterator);
        }
    };
}