2013-02-07 70 views
0

我正在使用apache lucene索引html文件。我將這些html文件的路徑存儲在lucene索引中。它存儲的索引,我已經檢查了所有盧克。 但是當我搜索文件的路徑時,它返回非常高的文檔數量。我希望它應該搜索它存儲在lucene索引中的確切路徑。 我使用下面的代碼apache lucene索引和搜索文件路徑

for index creation 


    try{ 
     File indexDir=new File("d:/abc/") 
     IndexWriter indexWriter = new IndexWriter(
      FSDirectory.open(indexDir), 
      new SimpleAnalyzer(), 
      true, 
      IndexWriter.MaxFieldLength.LIMITED); 
      indexWriter.setUseCompoundFile(false); 
     Document doc= new Document(); 
     String path=f.getCanonicalPath(); 
      doc.add(new Field("fpath",path, 
     Field.Store.YES,Field.Index.ANALYZED)); 
     indexWriter.addDocument(doc); 
     indexWriter.optimize(); 
     indexWriter.close(); 
    } 
    catch(Exception ex) 
    { 
    ex.printStackTrace(); 
    } 



    Following the code for searching the filepath 

     File indexDir = new File("d:/abc/"); 
      int maxhits = 10000000; 
        int len = 0; 
       try { 
        Directory directory = FSDirectory.open(indexDir); 
        IndexSearcher searcher = new IndexSearcher(directory, true); 
        QueryParser parser = new QueryParser(Version.LUCENE_36,"fpath", new SimpleAnalyzer()); 
        Query query = parser.parse(path); 
        query.setBoost((float) 1.5); 
        TopDocs topDocs = searcher.search(query, maxhits); 
        ScoreDoc[] hits = topDocs.scoreDocs; 
        len = hits.length; 
        JOptionPane.showMessageDialog(null,"items found"+len); 

       } 
       catch(Exception ex) 
       { 
       ex.printStackTrace(); 
       } 

其展示,而搜索路徑文件只存在一次沒有發現因爲總沒有文檔文件

回答

0

您所分析的路徑,將其分割成獨立條款。根路徑術語(如目錄/目錄/產品/版本)可能會出現在所有文檔中,因此任何包含目錄的搜索都不會強制所有術語都是強制性的,將返回所有文檔。

你需要像一個搜索查詢(使用上面的例子):

+catalog +products +versions 

強制所有條件存在。

注意,這個問題變得更爲複雜,如果同一組條件的可以以不同的順序,如:

/catalog/products/versions 
/versions/catalog/products/SKUs 

在這種情況下,你需要使用不同的Lucene的分詞器比標準分析器的標記生成器。