Dmytro Titov Dmytro Titov - 6 months ago 48
Apache Configuration Question

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<>();

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 at

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

Answer Source

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 class.

parsedQuery = parsedQuery.rewrite(reader);
// Here parsedQuery is an instance of the class.

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download