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?
Asked
Active
Viewed 8,367 times
1 Answers
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);
}

Cristian Libardo
- 9,260
- 3
- 35
- 41
-
Sorted me out, but had to pass an arbitrary string value as a first parameter of TokenStream. – ctrlplusb Oct 19 '12 at 08:51
-
2I had to add the Lucene.Net.Contrib nuget package to find those objects. – Alex C Mar 08 '17 at 18:33
-
Hi Cristian Libardo, I am also looking for it but I am using Lucene.Net v 2.9.4g with sensenet but don't have Lucene.Net.Highlight library . Is there any way to get source code or dll of Lucene.Net.Highlight library? Thanks – Ankitkumar Tandel Apr 16 '18 at 13:02