Lucene HTMLFormatter跳过最后一个字符

时间:2010-05-05 15:45:33

标签: lucene lucene.net highlighter.net

我有这个简单的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);
        }
    }

突出显示的结果似乎总是跳过结束'&gt;'我最后一个表标签。有什么建议?

2 个答案:

答案 0 :(得分:1)

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

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

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

我本可以在这里发布代码,但我的解决方案适用于Lucene Java。对于.Net,您必须找到与HTMLStripReader等效的内容。

答案 1 :(得分:0)

解决。显然我的Highlighter.Net版本很古老。升级到2.3.2.1解决了问题

相关问题