仅使用lucene荧光笔(3.0)将查询术语作为突出显示的结果

时间:2011-03-24 16:41:50

标签: c# search lucene lucene.net

我正在使用当前代码来获取片段以突出显示lucene输出,但结果始终只是搜索到的字符串。

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, ".");

如果它有用,这里是用于查询的代码:

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);
}

无论我搜索什么,都与为突出显示的片段返回的字符串完全相同。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。生成的工作代码是结果的变化:

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, ".");

我在查询中从多个字段更改为单个字段,因为页面名称在将用于格式化程序并更改为SimpleFormatter的摘要中永远不会有用