Lucene.Net搜索结果突出显示搜索关键字 [英] Lucene.Net Search result to highlight search keywords

查看:61
本文介绍了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);
}

这篇关于Lucene.Net搜索结果突出显示搜索关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆