As the previous answerer said, you should use either Highlighter or FastVectorHighlighter from contrib.
Here's an example of using Highlighter lib to get highlighted fragments:
Formatter formatter = new SimpleHTMLFormatter("<span><b>", "</b></span>");
Lucene.Net.Highlight.Scorer scorer = new QueryScorer(query, field);
Lucene.Net.Highlight.Encoder encoder = new SimpleHTMLEncoder();
var highlighter = new Highlighter(formatter, encoder, scorer);
highlighter.SetTextFragmenter(new SimpleFragmenter(100));
string[] fragments =
highlighter.GetBestFragments(DefaultAnalyzer, field, doc.Get(field), 3);
Some Highlighter-related gotchas:
To highlight a field, it should be added to index with Field.Store.YES
option
Your query should be rewritten before passing it to highlighter
- The analyzer you pass to highlighter should be the same you use for indexing and searching