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

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

Introduction

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

Prototype

public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException 

Source Link

Document

Expert: Constructs an appropriate Weight implementation for this query.

Usage

From source file:org.tallison.lucene.search.concordance.util.SimpleTargetCounter.java

License:Apache License

/**
 * Simple utility class to perform basic term frequency/document frequency
 * counts on the individual terms within a query.  This relies on
 * IndexReader and does not perform any concordance search/retrieval;
 * it is, therefore, very fast./*  w  ww .  j  av  a  2s . c  o m*/
 * <p>
 * If you want to visit more than basic terms (e.g. SpanNear),
 * see {@link TargetVisitor}
 *
 * @param query query
 * @param searcher searcher
 * @return target term results
 * @throws java.io.IOException if there is an IOException from the searcher
 */
public SimpleTargetTermResults searchSingleTerms(Query query, IndexSearcher searcher) throws IOException {
    Query tmpQ = query.rewrite(searcher.getIndexReader());
    Set<Term> terms = new HashSet<>();
    Weight weight = tmpQ.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
    weight.extractTerms(terms);

    Map<String, Integer> dfs = new HashMap<>();
    Map<String, Integer> tfs = new HashMap<>();

    for (Term t : terms) {
        String targ = t.text();
        int docFreq = searcher.getIndexReader().docFreq(t);
        if (docFreq == 0) {
            continue;
        }
        Integer i = new Integer(docFreq);
        dfs.put(targ, i);

        long tf = searcher.getIndexReader().totalTermFreq(t);
        tfs.put(targ, (int) tf);
    }

    SimpleTargetTermResults results = new SimpleTargetTermResults(dfs, tfs);

    return results;
}