代码之家  ›  专栏  ›  技术社区  ›  Midhat

Lucene HTMLFormatter跳过最后一个字符

  •  0
  • Midhat  · 技术社区  · 16 年前

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

    2 回复  |  直到 16 年前
        1
  •  1
  •   Shashikant Kore    16 年前

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

    我最近遇到了同样的问题,并在Solr的HTMLStripReader中找到了一个跳过标记中内容的解决方案。这个解决方案在我的博客下面的网址中列出。

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

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

        2
  •  0
  •   Midhat    16 年前

    解决了的。显然我的荧光笔.Net版本是过时的。升级到2.3.2.1解决了这个问题