Dmytro Titov
Dmytro Titov

Reputation: 3201

Apache Lucene createWeight() for wildcard query

I'm using Apache Lucene 6.6.0 and I'm trying to extract terms from the search query. Current version of code looks like this:

Query parsedQuery = new AnalyzingQueryParser("", analyzer).parse(query);
Weight weight = parsedQuery.createWeight(searcher, false);
Set<Term> terms = new HashSet<>();
weight.extractTerms(terms);

It works pretty much fine, but recently I noticed that it doesn't support queries with wildcards (i.e. * sign). If the query contains wildcard(s), then I get an exception:

java.lang.UnsupportedOperationException: Query id:123*456 does not implement createWeight at org.apache.lucene.search.Query.createWeight(Query.java:66) at org.apache.lucene.search.IndexSearcher.createWeight(IndexSearcher.java:751) at org.apache.lucene.search.BooleanWeight.(BooleanWeight.java:60) at org.apache.lucene.search.BooleanQuery.createWeight(BooleanQuery.java:225)

So is there a way to use createWeight() with wildcarded queries? Or maybe there's another way to extract search terms from query without createWeight()?

Upvotes: 0

Views: 1053

Answers (1)

Long story short, it is necessary to rewrite the query, for example, as follows:

final AnalyzingQueryParser analyzingQueryParser = new AnalyzingQueryParser("", analyzer);

// TODO: The rewrite method can be overridden.
// analyzingQueryParser.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE);

Query parsedQuery = analyzingQueryParser.parse(query);
// Here parsedQuery is an instance of the org.apache.lucene.search.WildcardQuery class.

parsedQuery = parsedQuery.rewrite(reader);
// Here parsedQuery is an instance of the org.apache.lucene.search.MultiTermQueryConstantScoreWrapper class.

final Weight weight = parsedQuery.createWeight(searcher, false);
final Set<Term> terms = new HashSet<>();
weight.extractTerms(terms);

Please refer to the thread:

for further details.

It seems the mentioned Stack Overflow question is this one: How to get matches from a wildcard Query in Lucene 6.2.

Upvotes: 2

Related Questions