Example usage for org.apache.lucene.search TwoPhaseIterator asDocIdSetIterator

List of usage examples for org.apache.lucene.search TwoPhaseIterator asDocIdSetIterator

Introduction

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

Prototype

public static DocIdSetIterator asDocIdSetIterator(TwoPhaseIterator twoPhaseIterator) 

Source Link

Document

Return a DocIdSetIterator view of the provided TwoPhaseIterator .

Usage

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

License:Apache License

@Override
public DocIdSetIterator iterator() {
    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
}

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;
            }//  w w w. j  a v  a  2 s  . co  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.index.query.PercolatorQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight innerWeight = percolatorQueriesQuery.createWeight(searcher, needsScores);
    return new Weight(this) {
        @Override/*from w  ww.ja  v a2s .co  m*/
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                int result = scorer.iterator().advance(docId);
                if (result == docId) {
                    return Explanation.match(scorer.score(), "PercolatorQuery");
                }
            }
            return Explanation.noMatch("PercolatorQuery");
        }

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

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

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = innerWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }

            final QueryRegistry.Leaf percolatorQueries = queryRegistry.getQueries(leafReaderContext);
            return new Scorer(this) {

                @Override
                public DocIdSetIterator iterator() {
                    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
                }

                @Override
                public TwoPhaseIterator twoPhaseIterator() {
                    return new TwoPhaseIterator(approximation.iterator()) {
                        @Override
                        public boolean matches() throws IOException {
                            return matchDocId(approximation.docID());
                        }

                        @Override
                        public float matchCost() {
                            return MATCH_COST;
                        }
                    };
                }

                @Override
                public float score() throws IOException {
                    return approximation.score();
                }

                @Override
                public int freq() throws IOException {
                    return approximation.freq();
                }

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

                boolean matchDocId(int docId) throws IOException {
                    Query query = percolatorQueries.getQuery(docId);
                    if (query != null) {
                        return Lucene.exists(percolatorIndexSearcher, query);
                    } else {
                        return false;
                    }
                }
            };
        }
    };
}

From source file:org.elasticsearch.percolator.PercolatorQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight innerWeight = percolatorQueriesQuery.createWeight(searcher, needsScores);
    return new Weight(this) {
        @Override/*from  ww  w.  j  av  a  2 s.  c o  m*/
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                int result = scorer.iterator().advance(docId);
                if (result == docId) {
                    return Explanation.match(scorer.score(), "PercolatorQuery");
                }
            }
            return Explanation.noMatch("PercolatorQuery");
        }

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

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

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = innerWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }

            final LeafReader leafReader = leafReaderContext.reader();
            return new Scorer(this) {

                @Override
                public DocIdSetIterator iterator() {
                    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
                }

                @Override
                public TwoPhaseIterator twoPhaseIterator() {
                    return new TwoPhaseIterator(approximation.iterator()) {
                        @Override
                        public boolean matches() throws IOException {
                            return matchDocId(approximation.docID(), leafReader);
                        }

                        @Override
                        public float matchCost() {
                            return MATCH_COST;
                        }
                    };
                }

                @Override
                public float score() throws IOException {
                    return approximation.score();
                }

                @Override
                public int freq() throws IOException {
                    return approximation.freq();
                }

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

                boolean matchDocId(int docId, LeafReader leafReader) throws IOException {
                    SingleFieldsVisitor singleFieldsVisitor = new SingleFieldsVisitor(UidFieldMapper.NAME);
                    leafReader.document(docId, singleFieldsVisitor);
                    BytesRef percolatorQueryId = new BytesRef(singleFieldsVisitor.uid().id());
                    return matchQuery(percolatorQueryId);
                }
            };
        }
    };
}

From source file:org.hibernate.search.spatial.impl.ConstantScoreScorer.java

License:LGPL

/**
 * Constructor based on a {@link TwoPhaseIterator}. In that case the {@link Scorer} will support two-phase
 * iteration.//from w  w  w  . j  a v a  2  s. c o m
 *
 * @param weight the parent weight
 * @param score the score to return on each document
 * @param twoPhaseIterator the iterator that defines matching documents
 */
public ConstantScoreScorer(Weight weight, float score, TwoPhaseIterator twoPhaseIterator) {
    super(weight);
    this.score = score;
    this.twoPhaseIterator = twoPhaseIterator;
    this.disi = TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator);
}

From source file:org.vootoo.search.function.ValueSourceCollectorFilter.java

License:Apache License

@Override
public DocIdSet getDocIdSet(@SuppressWarnings("rawtypes") final Map context,
        final LeafReaderContext readerContext, Bits acceptDocs) throws IOException {
    collectorFilterable.doSetNextReader(context, readerContext);
    //TODO  check getDocIdSet use
    return BitsFilteredDocIdSet.wrap(new DocIdSet() {
        @Override/*from   www.j ava 2s.  co  m*/
        public long ramBytesUsed() {
            return 0;
        }

        @Override
        public DocIdSetIterator iterator() throws IOException {
            final DocIdSetIterator approximation = DocIdSetIterator.all(readerContext.reader().maxDoc()); // no approximation!
            TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approximation) {
                @Override
                public boolean matches() throws IOException {
                    return collectorFilterable.matches(approximation.docID());
                }

                @Override
                public float matchCost() {
                    return 100; // TODO: use cost of ValueSourceScorer.this.matches()
                }
            };
            return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator);
        }

        @Override
        public Bits bits() {
            return null; // don't use random access
        }
    }, acceptDocs);
}