Lucene.Net搜索结果突出显示搜索关键字

问题描述:

我使用Lucene.Net为一些文档建立索引.我想向用户展示几行有关为什么该文档出现在结果集中的信息.就像您使用google进行搜索一样,它显示了链接,然后是链接,其中有几行带有突出显示的关键字. 有什么想法吗?

I use Lucene.Net to index some documents. I want to show the user a couple of lines as to why that document is in the result set. just like when you use google to search and it shows the link and followed by the link there are a few lines with the keywords highlighted. any ideas?

获得结果后,您可以通过类似于以下方法的方法将索引文本与查询一起传递给该文本:

When you have a result you can get the indexed text pass it along with your query through a method similar to this:

public string GeneratePreviewText(Query q, string text)
{
    QueryScorer scorer = new QueryScorer(q);
    Formatter formatter = new SimpleHTMLFormatter(highlightStartTag, highlightEndTag);
    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.SetTextFragmenter(new SimpleFragmenter(fragmentLength));
    TokenStream stream = new StandardAnalyzer().TokenStream(new StringReader(text));
    return highlighter.GetBestFragments(stream, text, fragmentCount, fragmentSeparator);
}