我试图对那些经常打错字的用户(比如我自己)好一点。
我试着为一些数据创建一个简单的搜索页面。我在BooleanQuery
中构建FuzzyQuery
s,因为我希望用户可以输入错别字,例如:
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();
搜索按预期工作,但是我从Lucene 8.5 API文档中获得的用于构建高亮显示的代码失败了:
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()));
}
}
}
有错误:
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)
代码使用了一个过时的方法,但我直接从文档中获得了它。
有人能解释一下 * 为什么 * 我会得到这个错误吗?我如何创建一个可以使用这个查询结构的荧光笔?或者我需要一个不同的Query
吗?
暂无答案!
目前还没有任何答案,快来回答吧!