开发者

Only getting query terms as highlighted results with lucene highlighter (3.0)

开发者 https://www.devze.com 2023-02-19 18:32 出处:网络
I\'m using the current code to get fragments to highlight on lucene output, but the results are always just the searched for string.

I'm using the current code to get fragments to highlight on lucene output, but the results are always just the searched for string.

var parser = new MultiFieldQueryParser(new[] { "contents", "PageName" }, 
    new StandardAnalyzer());                   
Query query = parser.Parse(Query);

QueryScorer scorer = new QueryScorer(query);
Formatter formatter = new SimpleHTMLFormatter(config.HighlightFormatterPrefix,
    config.HighlightFormatterSuffix);
Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.SetTextFragmenter(new SimpleFragmenter(100));
TokenStream stream = new StandardAnalyzer().TokenStream("contents",
    new StringReader(Query));
return highlighter.GetBestFragments(stream, Query, 2, ".");

In case it is helpful, here is the code used for the query:开发者_开发知识库

var parser = new MultiFieldQueryParser(new[]{"contents","PageName"}, 
    new StandardAnalyzer());
Query query = parser.Parse(searchString);

Hits results = searcher.Search(query);
var hits = new List<LuceneSearchResult>();
for (int index = 0; index < results.Length(); index++)
{
    Document document = results.Doc(index);

    var searchResult = new LuceneSearchResult();
    searchResult.Document = document;
    searchResult.Query = searchString;
    searchResult.Id = document.GetField("ID").StringValue();
    searchResult.Score = results.Score(index);
    hits.Add(searchResult);
}

Whatever I search for is the exact same as the string that is returned for the highlighted fragments.


I found the issue. The resulting working code was a change in the results:

var parser = new QueryParser("contents", new StandardAnalyzer());
Query query = parser.Parse(Query);
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(config.HighlightFormatterPrefix, config.HighlightFormatterSuffix);
QueryScorer fragmentScorer = new QueryScorer(query,"contents");
Highlighter highlighter = new Highlighter(formatter, fragmentScorer);
highlighter.SetTextFragmenter(new SimpleFragmenter(100));
TokenStream tokenStream = new SimpleAnalyzer().TokenStream(config.MainContentFieldName, new StringReader(field.StringValue()));

return highlighter.GetBestFragments(tokenStream, field.StringValue(), 2, ".");

I changed from multiple fields to a single field on the query since the pagename would never be useful in the summary this is being used for and changed from a Formatter to a SimpleFormatter

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号