Lucene.Net Search result to highlight search keywords

后端 未结 1 1944
北恋
北恋 2020-12-07 16:14

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

相关标签:
1条回答
  • 2020-12-07 16:35

    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);
    }
    
    0 讨论(0)
提交回复
热议问题