Ashish
Ashish

Reputation:

Lucene.net, want to search a word and display nearest words

i am using the document creation like..

Lucene.Net.Documents.Field fldContent =
    new Lucene.Net.Documents.Field("content", content,
        Lucene.Net.Documents.Field.Store.YES,
        Lucene.Net.Documents.Field.Index.TOKENIZED,
        Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS);

want to perform search by a word and want to display before 5 words and after 5 words.

please send me as soon as possible..

Ashish

Upvotes: 0

Views: 992

Answers (1)

Mikos
Mikos

Reputation: 8553

You should be looking at Highlighter. Highlighter lets you get snippets from the source documents. Here is some code to help you along:

Query qry= QueryParser.Parse(query,new StandardAnalyzer());
Highlighter highlighter = new Highlighter(new QueryScorer(qry));

Hits hits = searcher.Search(qry);

// Iterate through the results:
for (int i = 0; i < lHits; i++)
{
    Document hitDoc = hits.Doc(i);
    String desc = hitDoc.Get("Contents");
    TokenStream tokenStream = analyzer.TokenStream("Contents", new System.IO.StringReader(desc));

    highlights[i] = highlighter.GetBestFragment(tokenStream, desc);
}

Upvotes: 2

Related Questions