Search in sources :

Example 1 with TwoPhaseIterator

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

the class PercolateQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight verifiedMatchesWeight = verifiedMatchesQuery.createWeight(searcher, false);
    final Weight candidateMatchesWeight = candidateMatchesQuery.createWeight(searcher, false);
    return new Weight(this) {

        @Override
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                TwoPhaseIterator twoPhaseIterator = scorer.twoPhaseIterator();
                int result = twoPhaseIterator.approximation().advance(docId);
                if (result == docId) {
                    if (twoPhaseIterator.matches()) {
                        if (needsScores) {
                            CheckedFunction<Integer, Query, IOException> percolatorQueries = queryStore.getQueries(leafReaderContext);
                            Query query = percolatorQueries.apply(docId);
                            Explanation detail = percolatorIndexSearcher.explain(query, 0);
                            return Explanation.match(scorer.score(), "PercolateQuery", detail);
                        } else {
                            return Explanation.match(scorer.score(), "PercolateQuery");
                        }
                    }
                }
            }
            return Explanation.noMatch("PercolateQuery");
        }

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

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

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = candidateMatchesWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }
            final CheckedFunction<Integer, Query, IOException> queries = queryStore.getQueries(leafReaderContext);
            if (needsScores) {
                return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {

                    float score;

                    @Override
                    boolean matchDocId(int docId) throws IOException {
                        Query query = percolatorQueries.apply(docId);
                        if (query != null) {
                            TopDocs topDocs = percolatorIndexSearcher.search(query, 1);
                            if (topDocs.totalHits > 0) {
                                score = topDocs.scoreDocs[0].score;
                                return true;
                            } else {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }

                    @Override
                    public float score() throws IOException {
                        return score;
                    }
                };
            } else {
                Scorer verifiedDocsScorer = verifiedMatchesWeight.scorer(leafReaderContext);
                Bits verifiedDocsBits = Lucene.asSequentialAccessBits(leafReaderContext.reader().maxDoc(), verifiedDocsScorer);
                return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {

                    @Override
                    public float score() throws IOException {
                        return 0f;
                    }

                    boolean matchDocId(int docId) throws IOException {
                        // the MemoryIndex verification.
                        if (verifiedDocsBits.get(docId)) {
                            return true;
                        }
                        Query query = percolatorQueries.apply(docId);
                        return query != null && Lucene.exists(percolatorIndexSearcher, query);
                    }
                };
            }
        }
    };
}
Also used : Set(java.util.Set) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) Query(org.apache.lucene.search.Query) Explanation(org.apache.lucene.search.Explanation) Scorer(org.apache.lucene.search.Scorer) IOException(java.io.IOException) Weight(org.apache.lucene.search.Weight) TopDocs(org.apache.lucene.search.TopDocs) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits)

Example 2 with TwoPhaseIterator

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

use of org.apache.lucene.search.TwoPhaseIterator 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 4 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project lucene-solr by apache.

the class FunctionMatchQuery method createWeight.

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

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            DoubleValues values = source.getValues(context, null);
            DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
            TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {

                @Override
                public boolean matches() throws IOException {
                    return values.advanceExact(approximation.docID()) && filter.test(values.doubleValue());
                }

                @Override
                public float matchCost() {
                    // TODO maybe DoubleValuesSource should have a matchCost?
                    return 100;
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhase);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) DoubleValues(org.apache.lucene.search.DoubleValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 5 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project lucene-solr by apache.

the class LatLonDocValuesBoxQuery method createWeight.

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

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final SortedNumericDocValues values = context.reader().getSortedNumericDocValues(field);
            if (values == null) {
                return null;
            }
            final TwoPhaseIterator iterator = new TwoPhaseIterator(values) {

                @Override
                public boolean matches() throws IOException {
                    for (int i = 0, count = values.docValueCount(); i < count; ++i) {
                        final long value = values.nextValue();
                        final int lat = (int) (value >>> 32);
                        if (lat < minLatitude || lat > maxLatitude) {
                            // not within latitude range
                            continue;
                        }
                        final int lon = (int) (value & 0xFFFFFFFF);
                        if (crossesDateline) {
                            if (lon > maxLongitude && lon < minLongitude) {
                                // not within longitude range
                                continue;
                            }
                        } else {
                            if (lon < minLongitude || lon > maxLongitude) {
                                // not within longitude range
                                continue;
                            }
                        }
                        return true;
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    // 5 comparisons
                    return 5;
                }
            };
            return new ConstantScoreScorer(this, boost, iterator);
        }
    };
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Aggregations

TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)20 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)13 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)11 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)9 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)9 Scorer (org.apache.lucene.search.Scorer)7 Weight (org.apache.lucene.search.Weight)7 IOException (java.io.IOException)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Set (java.util.Set)3 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)3 Query (org.apache.lucene.search.Query)3 Bits (org.apache.lucene.util.Bits)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Document (org.apache.lucene.document.Document)2 IndexReader (org.apache.lucene.index.IndexReader)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 Term (org.apache.lucene.index.Term)2 FunctionValues (org.apache.lucene.queries.function.FunctionValues)2