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

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

Introduction

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

Prototype

protected Weight(Query query) 

Source Link

Document

Sole constructor, typically invoked by sub-classes.

Usage

From source file:io.crate.lucene.GenericFunctionQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    return new Weight(this) {
        @Override//from  w w  w. j  ava2s.c om
        public void extractTerms(Set<Term> terms) {
        }

        @Override
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            final Scorer s = scorer(context);
            final boolean match;
            final TwoPhaseIterator twoPhase = s.twoPhaseIterator();
            if (twoPhase == null) {
                match = s.iterator().advance(doc) == doc;
            } else {
                match = twoPhase.approximation().advance(doc) == doc && twoPhase.matches();
            }
            if (match) {
                assert s.score() == 0f : "score must be 0";
                return Explanation.match(0f, "Match on id " + doc);
            } else {
                return Explanation.match(0f, "No match on id " + doc);
            }
        }

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

        @Override
        public void normalize(float norm, float boost) {
        }

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            return new ConstantScoreScorer(this, 0f, getTwoPhaseIterator(context));
        }
    };
}

From source file:org.apache.solr.search.Filter.java

License:Apache License

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

        @Override//w w w .  j a v  a  2s  .  co  m
        public void extractTerms(Set<Term> terms) {
        }

        @Override
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            final Scorer scorer = scorer(context);
            final boolean match = (scorer != null && scorer.iterator().advance(doc) == doc);
            if (match) {
                assert scorer.score() == 0f;
                return Explanation.match(0f, "Match on id " + doc);
            } else {
                return Explanation.match(0f, "No match on id " + doc);
            }
        }

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final DocIdSet set = getDocIdSet(context, null);
            if (set == null) {
                return null;
            }
            if (applyLazily && set.bits() != null) {
                final Bits bits = set.bits();
                final DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
                final TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
                    @Override
                    public boolean matches() throws IOException {
                        return bits.get(approximation.docID());
                    }

                    @Override
                    public float matchCost() {
                        return 10; // TODO use cost of bits.get()
                    }
                };
                return new ConstantScoreScorer(this, 0f, twoPhase);
            }
            final DocIdSetIterator iterator = set.iterator();
            if (iterator == null) {
                return null;
            }
            return new ConstantScoreScorer(this, 0f, iterator);
        }

    };
}

From source file:org.apache.solr.update.DeleteByQueryWrapper.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final LeafReader wrapped = wrap((LeafReader) searcher.getIndexReader());
    final IndexSearcher privateContext = new IndexSearcher(wrapped);
    final Weight inner = in.createWeight(privateContext, needsScores);
    return new Weight(DeleteByQueryWrapper.this) {
        @Override//from  w ww  .j a va 2 s .  com
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            throw new UnsupportedOperationException();
        }

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

        @Override
        public void normalize(float norm, float topLevelBoost) {
            inner.normalize(norm, topLevelBoost);
        }

        @Override
        public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
            return inner.scorer(privateContext.getIndexReader().leaves().get(0), acceptDocs);
        }
    };
}

From source file:org.codelibs.elasticsearch.common.lucene.all.AllTermQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    if (needsScores == false) {
        return new TermQuery(term).createWeight(searcher, needsScores);
    }/*from   www  . ja  va2 s.  c  om*/
    final TermContext termStates = TermContext.build(searcher.getTopReaderContext(), term);
    final CollectionStatistics collectionStats = searcher.collectionStatistics(term.field());
    final TermStatistics termStats = searcher.termStatistics(term, termStates);
    final Similarity similarity = searcher.getSimilarity(needsScores);
    final SimWeight stats = similarity.computeWeight(collectionStats, termStats);
    return new Weight(this) {

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

        @Override
        public void normalize(float norm, float topLevelBoost) {
            stats.normalize(norm, topLevelBoost);
        }

        @Override
        public void extractTerms(Set<Term> terms) {
            terms.add(term);
        }

        @Override
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            AllTermScorer scorer = scorer(context);
            if (scorer != null) {
                int newDoc = scorer.iterator().advance(doc);
                if (newDoc == doc) {
                    float score = scorer.score();
                    float freq = scorer.freq();
                    SimScorer docScorer = similarity.simScorer(stats, context);
                    Explanation freqExplanation = Explanation.match(freq, "termFreq=" + freq);
                    Explanation termScoreExplanation = docScorer.explain(doc, freqExplanation);
                    Explanation payloadBoostExplanation = Explanation.match(scorer.payloadBoost(),
                            "payloadBoost=" + scorer.payloadBoost());
                    return Explanation.match(score,
                            "weight(" + getQuery() + " in " + doc + ") ["
                                    + similarity.getClass().getSimpleName() + "], product of:",
                            termScoreExplanation, payloadBoostExplanation);
                }
            }
            return Explanation.noMatch("no matching term");
        }

        @Override
        public AllTermScorer scorer(LeafReaderContext context) throws IOException {
            final Terms terms = context.reader().terms(term.field());
            if (terms == null) {
                return null;
            }
            final TermsEnum termsEnum = terms.iterator();
            if (termsEnum == null) {
                return null;
            }
            final TermState state = termStates.get(context.ord);
            if (state == null) {
                // Term does not exist in this segment
                return null;
            }
            termsEnum.seekExact(term.bytes(), state);
            PostingsEnum docs = termsEnum.postings(null, PostingsEnum.PAYLOADS);
            assert docs != null;
            return new AllTermScorer(this, docs, similarity.simScorer(stats, context));
        }

    };
}

From source file:org.eclipse.dltk.internal.core.index.lucene.BitFlagsQuery.java

License:Open Source License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    return new Weight(this) {
        @Override//from   w  w  w.ja  v a  2 s  .  c o m
        public void extractTerms(Set<Term> terms) {
            // Ignore
        }

        @Override
        public void normalize(float norm, float topLevelBoost) {
            // Ignore
        }

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

        @Override
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            final Scorer scorer = scorer(context, context.reader().getLiveDocs());
            final boolean match = (scorer != null && scorer.advance(doc) == doc);
            if (match) {
                assert scorer.score() == 0;
                return Explanation.match(0, "Match on id" + doc); //$NON-NLS-1$
            } else {
                return Explanation.match(0, "No match on id" + doc); //$NON-NLS-1$
            }
        }

        @Override
        public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
            final DocIdSet set = getDocIdSet(context, acceptDocs);
            if (set == null) {
                return null;
            }
            final DocIdSetIterator iterator = set.iterator();
            if (iterator == null) {
                return null;
            }
            return new ConstantScoreScorer(this, 0, iterator);
        }
    };

}

From source file:org.elasticsearch.index.query.PercolateQuery.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// w  w w . j a v 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) {
                TwoPhaseIterator twoPhaseIterator = scorer.twoPhaseIterator();
                int result = twoPhaseIterator.approximation().advance(docId);
                if (result == docId) {
                    if (twoPhaseIterator.matches()) {
                        if (needsScores) {
                            QueryRegistry.Leaf percolatorQueries = queryRegistry.getQueries(leafReaderContext);
                            Query query = percolatorQueries.getQuery(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 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);
            if (needsScores) {
                return new BaseScorer(this, approximation, percolatorQueries, percolatorIndexSearcher) {

                    float score;

                    @Override
                    boolean matchDocId(int docId) throws IOException {
                        Query query = percolatorQueries.getQuery(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 {
                return new BaseScorer(this, approximation, percolatorQueries, percolatorIndexSearcher) {

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

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

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//w w w  . jav a2s  . 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 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.PercolateQuery.java

License:Apache License

@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//from  ww  w  .j av a  2 s . c om
        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) {
                            QueryStore.Leaf percolatorQueries = queryStore.getQueries(leafReaderContext);
                            Query query = percolatorQueries.getQuery(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 QueryStore.Leaf 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.getQuery(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 {
                        // We use the verifiedDocsBits to skip the expensive MemoryIndex verification.
                        // If docId also appears in the verifiedDocsBits then that means during indexing
                        // we were able to extract all query terms and for this candidate match
                        // and we determined based on the nature of the query that it is safe to skip
                        // the MemoryIndex verification.
                        if (verifiedDocsBits.get(docId)) {
                            return true;
                        }
                        Query query = percolatorQueries.getQuery(docId);
                        return query != null && Lucene.exists(percolatorIndexSearcher, query);
                    }
                };
            }
        }
    };
}

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/* w w 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:uk.co.flax.luwak.util.ForceNoBulkScoringQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {

    final Weight innerWeight = inner.createWeight(searcher, needsScores);

    return new Weight(ForceNoBulkScoringQuery.this) {
        @Override/*from w  w  w.j a  va  2  s .co m*/
        public void extractTerms(Set<Term> set) {
            innerWeight.extractTerms(set);
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int i) throws IOException {
            return innerWeight.explain(leafReaderContext, i);
        }

        @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 {
            return innerWeight.scorer(leafReaderContext);
        }
    };
}