views:

1375

answers:

2

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?

+5  A: 

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
You are a gem mate. thanks.
Ali Shafai
A: 

Hi, It does not highlight a word like cert* If matching texts like certain, certainity, certificate etc.

Does it have any limitation. Let me know what other steps I need to implement to make this * works.

It works fine with Full word, However as soon as I give *, It does not look for those matching word to highlight.

Malay