Example usage for org.apache.lucene.queries CommonTermsQuery createWeight

List of usage examples for org.apache.lucene.queries CommonTermsQuery createWeight

Introduction

In this page you can find the example usage for org.apache.lucene.queries CommonTermsQuery 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.queries.SpanQueryConverter.java

License:Apache License

@Override
protected SpanQuery convertUnknownQuery(String field, Query query) {
    if (query instanceof CommonTermsQuery) {

        // specialized since rewriting would change the result query
        // this query is TermContext sensitive.
        CommonTermsQuery ctq = (CommonTermsQuery) query;

        Set<Term> terms = new HashSet<>();
        try {//ww  w.  j a va 2  s  .c o m
            Weight w = ctq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
            w.extractTerms(terms);
        } catch (IOException e) {
            throw new RuntimeException("IOException on searcher!!!", e);
        }
        List<SpanQuery> spanQs = new LinkedList<SpanQuery>();

        for (Term term : terms) {
            if (term.field().equals(field)) {
                spanQs.add(new SpanTermQuery(term));
            }
        }
        if (spanQs.size() == 0) {
            return getEmptySpanQuery();
        } else if (spanQs.size() == 1) {
            return spanQs.get(0);
        } else {
            return new SpanOrQuery(spanQs.toArray(new SpanQuery[spanQs.size()]));
        }
    }
    super.convertUnknownQuery(field, query);
    return null;
}