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

时间:2021-01-09 03:08:37

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?

我使用Lucene.Net来索引一些文档。我想向用户显示关于该文档在结果集中的原因的几行。就像当你使用谷歌搜索它并显示链接,然后链接后,有几行突出显示关键字。有任何想法吗?

1 个解决方案

#1


23  

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);
}

#1


23  

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);
}