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

查看:20
本文介绍了Lucene.Net 搜索结果突出显示搜索关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Lucene.Net 来索引一些文档.我想向用户展示几行关于为什么该文档在结果集中的原因.就像您使用谷歌搜索时一样,它会显示链接,然后链接后面有几行突出显示了关键字.有什么想法吗?

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天全站免登陆