0

我有这个简单的 Lucene 搜索代码(从http://www.lucenetutorial.com/lucene-in-5-minutes.html修改)

 class Program
    {
        static void Main(string[] args)
        {

            StandardAnalyzer analyzer = new StandardAnalyzer();
            Directory index = new RAMDirectory();
            IndexWriter w = new IndexWriter(index, analyzer, true,
                    IndexWriter.MaxFieldLength.UNLIMITED);
            addDoc(w, "Table 1 <table> content </table>");
            addDoc(w, "Table 2");
            addDoc(w, "<table> content </table>");
            addDoc(w, "The Art of Computer Science");
            w.Close();


            String querystr = "table";


            Query q = new QueryParser("title", analyzer).Parse(querystr);
            Lucene.Net.Search.IndexSearcher searcher = new
            Lucene.Net.Search.IndexSearcher(index);
            Hits hitsFound = searcher.Search(q);

            SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("*", "*");

            Highlighter highlighter = null;
            highlighter = new Highlighter(formatter, new QueryScorer(searcher.Rewrite(q)));

            for (int i = 0; i < hitsFound.Length(); i++)
            {
                Console.WriteLine(highlighter.GetBestFragment(analyzer, "title", hitsFound.Doc(i).Get("title")));
             //   Console.WriteLine(hitsFound.Doc(i).Get("title"));
            }
            Console.ReadKey();



        }
        private static void addDoc(IndexWriter w, String value)
        {
            Document doc = new Document();
            doc.Add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
            w.AddDocument(doc);
        }
    }

突出显示的结果似乎总是跳过我最后一个表格标签的结束“>”。有什么建议么?

4

2 回答 2

1

Lucene 的荧光笔开箱即用,适用于处理纯文本。如果您尝试突出显示 HTML 或任何标记文本,它将无法正常工作。

我最近遇到了同样的问题,并在 Solr 的 HTMLStripReader 中找到了一个解决方案,它跳过了标签中的内容。该解决方案在我的博客上的以下 URL 中进行了概述。

http://sigabrt.blogspot.com/2010/04/highlighting-query-in-entire-html.html

我本可以在此处发布代码,但我的解决方案适用于 Lucene Java。对于 .Net,您必须找到 HTMLStripReader 的等价物。

于 2010-05-06T05:33:18.357 回答
0

解决了。显然我的 Highlighter.Net 版本过时了。升级到 2.3.2.1 解决了问题

于 2010-05-06T05:43:20.957 回答