Lucene.net突出显示了文本中的搜索词

时间:2015-10-23 23:17:54

标签: lucene lucene.net

我正在使用Lucene.net搜索给定的文档。要求是一旦搜索完成,它应突出显示文档中搜索的术语。我见过返回最佳片段的例子。但我需要的是在主要内容中突出显示。

     using (StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_30, stopWords))
        {
            QueryParser parser = new QueryParser(Version.LUCENE_30, "Content", standardAnalyzer);
            parser.AllowLeadingWildcard = true;
            Query qry = parser.Parse(searchText);
            Directory indexDir = CreateRAMDirectory(htmlContent);

            IndexReader reader = IndexReader.Open(indexDir, true);
            IndexSearcher searcher = new IndexSearcher(reader);
            searcher.SetDefaultFieldSortScoring(true, true);

            IFormatter formatter = new SimpleHTMLFormatter("<span style=\"font-weight:bold; background-color:yellow;\">", "</span>");
            SimpleFragmenter fragmenter = new SimpleFragmenter(1000);
            QueryScorer scorer = null;
            scorer = new QueryScorer(qry);

            ScoreDoc[] hits = searcher.Search(qry, null, 10000, Sort.RELEVANCE).ScoreDocs;

            Highlighter highlighter = new Highlighter(formatter, scorer);
            highlighter.TextFragmenter = fragmenter;



            foreach (var result in hits)
            {
                int docId = result.Doc;
                float score = result.Score;
                Document doc = searcher.Doc(docId);
                Lucene.Net.Analysis.TokenStream stream = standardAnalyzer.TokenStream("Content", new IO.StringReader(searchText));
        String highlighterData = highlighter.GetBestFragments(stream, searchText, 1000, "");

            }

        }

我是Lucene.net的新手,如何才能获得整个文档,其中搜索的术语内容突出显示而不是片段?

1 个答案:

答案 0 :(得分:0)

分段符控制返回的文本块的大小。要使用整个字段内容,只需使用NullFragmenter,而不是SimpleFragmenter

Fragmenter fragmenter = new NullFragmenter();
.....
highlighter.TextFragmenter = fragmenter;