2017-05-04 134 views
0

我遇到了Lucene熒光筆的問題。我在Stackoverflow和其他代碼上發現了一些代碼,但是這段代碼在我的程序中不起作用。這是一種我嘗試搜索和高興的詞的方法,但是當我搜索某些東西時,程序給了我一個例外。Lucene熒光筆TokenStream異常

方法:

private static void useIndex(String query, String field, String option) 
      throws ParseException, CorruptIndexException, IOException, InvalidTokenOffsetsException { 
     // StandardAnalyzer analyzer = new StandardAnalyzer(); 
     Query q = new QueryParser(field, analyzer).parse(query); 

     int hitsPerPage = 5; 

     IndexReader reader = DirectoryReader.open(index); 
     IndexSearcher searcher = new IndexSearcher(reader); 

     TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage); 

     searcher.search(q, collector); 

     ScoreDoc[] hits = collector.topDocs().scoreDocs; 

     SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(); 
     Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(q)); 

     // display results 
     System.out.println("Found " + hits.length + " hits for " + query); 
     for (int i = 0; i < hits.length; ++i) { 
      int docId = hits[i].doc; 
      Document d = searcher.doc(docId); 
      String docURL = d.get("url"); 
      String docContent = d.get("content"); 

      TokenStream tokenStream = TokenSources.getAnyTokenStream(reader, docId, "content", analyzer); 
      TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, docContent, false, 4); 

      String docFrag=""; 

      if ((frag[0] != null) && (frag[0].getScore() > 0)) { 
       docFrag=frag[0].toString(); 
      } 
      model.addRow(new Object[] { docURL, findSilimar(docId), docFrag }); 
     } 
     reader.close(); 
    } 

例外:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/lucene/index/memory/MemoryIndex 
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.index.memory.MemoryIndex 

我都試過了,但我不知道什麼是錯。

P.S.對不起我的英語不好。

回答

1

A NoClassDefFoundError意味着類不在你的類路徑中,所以你應該弄清楚你需要添加什麼jar來獲取它。 MemoryIndex是在:lucene-memory-x.x.x.jar

順便說一句,它看起來不會在您提供的代碼中拋出此異常。