Example usage for org.apache.lucene.search IndexSearcher createWeight

List of usage examples for org.apache.lucene.search IndexSearcher createWeight

Introduction

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

Prototype

public Weight createWeight(Query query, ScoreMode scoreMode, float boost) throws IOException 

Source Link

Document

Creates a Weight for the given query, potentially adding caching if possible and configured.

Usage

From source file:com.o19s.es.explore.ExplorerQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    if (!needsScores) {
        return searcher.createWeight(query, false, boost);
    }//from   w  w  w  .j a v  a  2s. c om
    final Weight subWeight = searcher.createWeight(query, true, boost);
    Set<Term> terms = new HashSet<>();
    subWeight.extractTerms(terms);
    if (isCollectionScoped()) {
        ClassicSimilarity sim = new ClassicSimilarity();
        StatisticsHelper df_stats = new StatisticsHelper();
        StatisticsHelper idf_stats = new StatisticsHelper();
        StatisticsHelper ttf_stats = new StatisticsHelper();

        for (Term term : terms) {
            TermContext ctx = TermContext.build(searcher.getTopReaderContext(), term);
            TermStatistics tStats = searcher.termStatistics(term, ctx);
            df_stats.add(tStats.docFreq());
            idf_stats.add(sim.idf(tStats.docFreq(), searcher.getIndexReader().numDocs()));
            ttf_stats.add(tStats.totalTermFreq());
        }

        /*
        If no terms are parsed in the query we opt for returning 0
        instead of throwing an exception that could break various
        pipelines.
         */
        float constantScore;

        if (terms.size() > 0) {
            switch (type) {
            case ("sum_classic_idf"):
                constantScore = idf_stats.getSum();
                break;
            case ("mean_classic_idf"):
                constantScore = idf_stats.getMean();
                break;
            case ("max_classic_idf"):
                constantScore = idf_stats.getMax();
                break;
            case ("min_classic_idf"):
                constantScore = idf_stats.getMin();
                break;
            case ("stddev_classic_idf"):
                constantScore = idf_stats.getStdDev();
                break;
            case "sum_raw_df":
                constantScore = df_stats.getSum();
                break;
            case "min_raw_df":
                constantScore = df_stats.getMin();
                break;
            case "max_raw_df":
                constantScore = df_stats.getMax();
                break;
            case "mean_raw_df":
                constantScore = df_stats.getMean();
                break;
            case "stddev_raw_df":
                constantScore = df_stats.getStdDev();
                break;
            case "sum_raw_ttf":
                constantScore = ttf_stats.getSum();
                break;
            case "min_raw_ttf":
                constantScore = ttf_stats.getMin();
                break;
            case "max_raw_ttf":
                constantScore = ttf_stats.getMax();
                break;
            case "mean_raw_ttf":
                constantScore = ttf_stats.getMean();
                break;
            case "stddev_raw_ttf":
                constantScore = ttf_stats.getStdDev();
                break;
            case "unique_terms_count":
                constantScore = terms.size();
                break;

            default:
                throw new RuntimeException("Invalid stat type specified.");
            }
        } else {
            constantScore = 0.0f;
        }

        return new ConstantScoreWeight(ExplorerQuery.this, constantScore) {

            @Override
            public Explanation explain(LeafReaderContext context, int doc) throws IOException {
                Scorer scorer = scorer(context);
                int newDoc = scorer.iterator().advance(doc);
                assert newDoc == doc; // this is a DocIdSetIterator.all
                return Explanation.match(scorer.score(), "Stat Score: " + type);
            }

            @Override
            public Scorer scorer(LeafReaderContext context) throws IOException {
                return new ConstantScoreScorer(this, constantScore,
                        DocIdSetIterator.all(context.reader().maxDoc()));
            }

            @Override
            public boolean isCacheable(LeafReaderContext ctx) {
                return true;
            }

        };
    } else if (type.endsWith("_raw_tf")) {
        // Rewrite this into a boolean query where we can inject our PostingsExplorerQuery
        BooleanQuery.Builder qb = new BooleanQuery.Builder();
        for (Term t : terms) {
            qb.add(new BooleanClause(new PostingsExplorerQuery(t, PostingsExplorerQuery.Type.TF),
                    BooleanClause.Occur.SHOULD));
        }
        // FIXME: completely refactor this class and stop accepting a random query but a list of terms directly
        // rewriting at this point is wrong, additionally we certainly build the TermContext twice for every terms
        // problem is that we rely on extractTerms which happen too late in the process
        Query q = qb.build().rewrite(searcher.getIndexReader());
        return new ExplorerQuery.ExplorerWeight(this, searcher.createWeight(q, true, boost), type);
    }
    throw new IllegalArgumentException("Unknown ExplorerQuery type [" + type + "]");
}

From source file:com.o19s.es.ltr.query.RankerQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    if (!needsScores) {
        // If scores are not needed simply return a constant score on all docs
        return new ConstantScoreWeight(this, boost) {
            @Override/*from  w  w  w.  jav  a2 s . com*/
            public Scorer scorer(LeafReaderContext context) throws IOException {
                return new ConstantScoreScorer(this, score(), DocIdSetIterator.all(context.reader().maxDoc()));
            }

            @Override
            public boolean isCacheable(LeafReaderContext ctx) {
                return true;
            }
        };
    }
    List<Weight> weights = new ArrayList<>(queries.size());
    for (Query q : queries) {
        weights.add(searcher.createWeight(q, needsScores, boost));
    }
    return new RankerWeight(weights);
}

From source file:org.tallison.lucene.search.concordance.charoffsets.SpansCrawler.java

License:Apache License

public static void crawl(SpanQuery query, Query filter, IndexSearcher searcher, DocTokenOffsetsVisitor visitor)
        throws IOException, TargetTokenNotFoundException {

    query = (SpanQuery) query.rewrite(searcher.getIndexReader());

    SpanWeight w = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
    if (filter == null) {
        for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {

            Spans spans = w.getSpans(ctx, SpanWeight.Postings.POSITIONS);
            if (spans == null) {
                continue;
            }//from www.j av a2 s . c o m
            boolean cont = visitLeafReader(ctx, spans, visitor);
            if (!cont) {
                break;
            }
        }
    } else {
        filter = searcher.rewrite(filter);
        Weight searcherWeight = searcher.createWeight(filter, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
        for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
            Scorer leafReaderContextScorer = searcherWeight.scorer(ctx);
            if (leafReaderContextScorer == null) {
                continue;
            }
            //Can we tell from the scorer that there were no hits?
            //in <= 5.x we could stop here if the filter query had no hits.

            Spans spans = w.getSpans(ctx, SpanWeight.Postings.POSITIONS);
            if (spans == null) {
                continue;
            }
            DocIdSetIterator filterItr = leafReaderContextScorer.iterator();

            if (filterItr == null || filterItr.equals(DocIdSetIterator.empty())) {
                continue;
            }
            boolean cont = visitLeafReader(ctx, spans, filterItr, visitor);
            if (!cont) {
                break;
            }
        }
    }
}

From source file:tech.beshu.ror.es.security.DocumentFilterReader.java

License:Open Source License

private DocumentFilterReader(LeafReader reader, Query query) throws IOException {
    super(reader);
    final IndexSearcher searcher = new IndexSearcher(this);
    searcher.setQueryCache(null);//w w  w.  j a v  a 2 s . c  o m
    final boolean needsScores = false;
    final Weight preserveWeight = searcher.createWeight(query, needsScores, 0);

    final int maxDoc = this.in.maxDoc();
    final FixedBitSet bits = new FixedBitSet(maxDoc);
    final Scorer preserveScorer = preserveWeight.scorer(this.getContext());
    if (preserveScorer != null) {
        bits.or(preserveScorer.iterator());
    }

    if (in.hasDeletions()) {
        final Bits oldLiveDocs = in.getLiveDocs();
        assert oldLiveDocs != null;
        final DocIdSetIterator it = new BitSetIterator(bits, 0L);
        for (int i = it.nextDoc(); i != DocIdSetIterator.NO_MORE_DOCS; i = it.nextDoc()) {
            if (!oldLiveDocs.get(i)) {
                bits.clear(i);
            }
        }
    }

    this.liveDocs = bits;
    this.numDocs = bits.cardinality();
}