I'm trying to be nice for users that make a lot of typos (like myself).
I try to create a simple search page for some data. I build FuzzyQuery
s in a BooleanQuery
because I would like the user to make typos, for example this:
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(new FuzzyQuery(new Term("body", "pzza")), BooleanClause.Occur.SHOULD);
builder.add(new FuzzyQuery(new Term("body", "tcyoon")), BooleanClause.Occur.SHOULD);
BooleanQuery query = builder.build();
Searching works as expected, but the code I got from the Lucene 8.5 API docs to build the highlighting fails:
SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
for (int i = 0; i < hits.length; i++) {
int id = hits[i].doc;
Document doc = searcher.doc(id);
System.out.println("HIT:" + doc.get("url"));
String text = doc.get("body");
TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "body", analyzer);
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);//highlighter.getBestFragments(tokenStream, text, 3, "...");
for (int j = 0; j < frag.length; j++) {
if ((frag[j] != null) && (frag[j].getScore() > 0)) {
System.out.println((frag[j].toString()));
}
}
}
With error:
java.lang.IllegalArgumentException: boost must be a positive float, got -1.0
at org.apache.lucene.search.BoostQuery.<init>(BoostQuery.java:44)
at org.apache.lucene.search.ScoringRewrite$1.addClause(ScoringRewrite.java:69)
at org.apache.lucene.search.ScoringRewrite$1.addClause(ScoringRewrite.java:54)
at org.apache.lucene.search.ScoringRewrite.rewrite(ScoringRewrite.java:117)
at org.apache.lucene.search.highlight.WeightedSpanTermExtractor.extract(WeightedSpanTermExtractor.java:246)
at org.apache.lucene.search.highlight.WeightedSpanTermExtractor.extract(WeightedSpanTermExtractor.java:135)
at org.apache.lucene.search.highlight.WeightedSpanTermExtractor.getWeightedSpanTerms(WeightedSpanTermExtractor.java:530)
at org.apache.lucene.search.highlight.QueryScorer.initExtractor(QueryScorer.java:218)
at org.apache.lucene.search.highlight.QueryScorer.init(QueryScorer.java:186)
at org.apache.lucene.search.highlight.Highlighter.getBestTextFragments(Highlighter.java:201)
The code uses a deprecated method, but I took it straight from the documentation.
Can somebody explain why I get this error? How can I create a highlighter that works with this query construction? Or do I need a different Query
?
The following approach to highlighting uses Lucene v8.5.0 with the question's fuzzy boolean example.
The results look like this, in my stripped-down demo (but you can refine how the highlighted fragments are displayed, of course):
The highlighting code is as follows:
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.TokenSources;
import org.apache.lucene.search.highlight.TextFragment;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
public class CustomHighlighter {
private static final String PRE_TAG = "<span class=\"hilite\">";
private static final String POST_TAG = "</span>";
public static String[] highlight(Query query, IndexSearcher searcher,
Analyzer analyzer, ScoreDoc hit, String fieldName)
throws IOException, InvalidTokenOffsetsException {
SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(PRE_TAG, POST_TAG);
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
int id = hit.doc;
Document doc = searcher.doc(id);
String text = doc.get(fieldName);
TokenStream tokenStream = TokenSources.getTokenStream(fieldName,
searcher.getIndexReader().getTermVectors(id), text, analyzer, -1);
int maxNumFragments = 10;
boolean mergeContiguousFragments = Boolean.TRUE;
TextFragment[] frags = highlighter.getBestTextFragments(tokenStream,
text, mergeContiguousFragments, maxNumFragments);
String[] highlightedText = new String[frags.length];
for (int i = 0; i < frags.length; i++) {
highlightedText[i] = frags[i].toString();
}
// control how you handle each fragment for display...
//for (TextFragment frag : frags) {
// if ((frag != null) && (frag.getScore() > 0)) {
// highlightedText = frag.toString();
// }
//}
return highlightedText;
}
}
The class is used as follows (where SearchResults
is just one of my classes for collecting results, for later presentation to the user):
for (ScoreDoc hit : hits) {
String[] highlightedText = CustomHighlighter.highlight(query, searcher,
analyzer, hit, field);
String document = searcher.doc(hit.doc).get("path");
SearchResults.Match match = new SearchResults.Match(document, highlightedText, hit.score);
results.getMatches().add(match);
}
And the fuzzy query is this:
private static Query useFuzzyBooleanQuery() {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(new FuzzyQuery(new Term("contents", "pzza")), BooleanClause.Occur.SHOULD);
builder.add(new FuzzyQuery(new Term("contents", "tcyoon")), BooleanClause.Occur.SHOULD);
return builder.build();
}
The above code does not give me any deprecation warnings.
I can't explain why you get that specific "boost" error - I have not seen that myself, and I was not able to recreate it. But I did not try too hard, I confess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.