Lucene 荧光笔不匹配前缀
我正在使用 Lucene 的荧光笔来突出显示字符串的某些部分。下面的代码似乎可以很好地查找词干单词,但不能用于前缀匹配。
EnglishAnalyzer analyzer = new EnglishAnalyzer(Version.LUCENE_34);
QueryParser parser = new QueryParser(Version.LUCENE_30, "", analyzer);
Query query = parser.parse(pQuery);
QueryScorer scorer = new QueryScorer(query);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, 40);
Highlighter highlighter = new Highlighter(scorer);
highlighter.setTextFragmenter(fragmenter);
String[] frags = highlighter.getBestFragments(analyzer, "", pText, 4);
我在几个不同的地方读过,我需要调用 Query.rewrite 才能使前缀匹配正常工作。该方法需要 IndexReader 争论,但我不知道如何获得它。我发现所有示例的调用 Query.rereite 都没有显示 IndexReader 的来源。我要补充一点,这是我使用的唯一 Lucene 代码。我没有使用 Lucene 本身进行搜索,只是为了突出显示。
如何创建 IndexReader?如果我像现在这样使用 Lucene,是否可以创建一个 IndexReader。或者也许有不同的方法让它突出显示前缀匹配?我对 Lucene 很陌生,我确定所有这些部分的作用或者它们是否都是必需的。我刚刚从网上找到的各种示例中复制了它们。因此,如果我做错了什么,请告诉我。谢谢。
I'm using Lucene's Highlighter to highlight parts of a string. The code below seems to work fine for finding the stemmed words but not for prefix matching.
EnglishAnalyzer analyzer = new EnglishAnalyzer(Version.LUCENE_34);
QueryParser parser = new QueryParser(Version.LUCENE_30, "", analyzer);
Query query = parser.parse(pQuery);
QueryScorer scorer = new QueryScorer(query);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, 40);
Highlighter highlighter = new Highlighter(scorer);
highlighter.setTextFragmenter(fragmenter);
String[] frags = highlighter.getBestFragments(analyzer, "", pText, 4);
I've read in a few different places I need to call Query.rewrite to get the prefix matching to work. That method takes an IndexReader arguement though and I'm not sure how to get it. All of the example's I've found that call Query.rewreite don't show where the IndexReader came from. I'll add that that this is the only Lucene code I'm using. I'm not using Lucene to do the searching itself, just for the highlighting.
How do I create an IndexReader and is it possible to create one if I'm using Lucene the way that I am. Or perhaps there's a different way to get it to highlight the prefix matches? I'm very new to Lucene and I'm sure what all of these pieces do or if they're all necessary. I've just copied them from various example's I've found online. So if I've doing anything else wrong please let me know. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您有一个查询
field:abc*
。query.rewrite
的基本作用是:它读取索引(这就是为什么您需要 IndexReader)查找以abc
开头的所有术语,并将您的查询更改为 ,例如,字段:abc1 字段:abc2 字段:abc3
。如果您知道索引的位置,则可以使用 IndexReader.Open 来获取 IndexReader。如果您根本没有索引,则应该搜索pText
,查找以abc
开头的所有单词,并相应地更新您的查询。Suppose you have a query
field:abc*
. Whatquery.rewrite
basically does is: it reads the index(this why you need an IndexReader) finds all terms that start withabc
and changes your query as ,for ex.,field:abc1 field:abc2 field:abc3
. If you know the location of the index, you can useIndexReader.Open
to get an IndexReader. If you don't have an index at all, you should search yourpText
, find all words that start withabc
and update your query accordingly.