A
A
Alf1622012-12-05 19:09:13
ASP.NET
Alf162, 2012-12-05 19:09:13

Word highlighting lucene.net?

I'm doing a full text search for the web using lucene on asp.net. Everything works great, except for keyword highlighting. More precisely, it works and highlights for all queries except wildcardQuery, i.e. requests like: "by * sk", etc. I do everything by default:

Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("text", analyzer);
Query query = parser.Parse(src_txt);
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = searcher.Search(query);
Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter(
                    "<span class='find'>", "</span>");
Lucene.Net.Highlight.SimpleFragmenter fragmenter = new Lucene.Net.Highlight.SimpleFragmenter(300);
Lucene.Net.Highlight.QueryScorer scorer = new Lucene.Net.Highlight.QueryScorer(query);
Lucene.Net.Highlight.Highlighter highlighter = new Lucene.Net.Highlight.Highlighter(formatter, scorer);
highlighter.SetTextFragmenter(fragmenter);
Document doc = hits.Doc(i);
string tr = Server.HtmlEncode(doc.Get("text"));
TokenStream ts = analyzer.TokenStream("", new System.IO.StringReader(tr));
string result = highlighter.GetBestFragment(ts, tr);

Please tell me what could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alf162, 2012-12-06
@Alf162

The problem was resolved by adding two lines:

parser.SetMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
.....
query = query.Rewrite(searcher.GetIndexReader());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question