2013-02-07 72 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的分词器比标准分析器的标记生成器。