【问题标题】:apache lucene indexing and searching on the filepathapache lucene 索引和搜索文件路径
【发布时间】:2013-02-07 06:40:34
【问题描述】:

我正在使用 apache lucene 来索引 html 文件。我将 html 文件的路径存储在 lucene index 中。它存储索引,并且我已经在 luke all 中检查了它。 但是当我搜索文件的路径时,它返回的文档数量非常高。我希望它应该搜索存储在 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();
              }

它显示找到的文档数为总文档数,而搜索的路径文件仅存在一次

【问题讨论】:

    标签: java lucene


    【解决方案1】:

    您正在分析路径,这会将其拆分为单独的术语。根路径项(如 /catalog/products/versions 中的 catalog)可能出现在所有文档中,因此任何包含 catalog 的搜索都不会强制所有强制性条款将返回所有文件。

    您需要一个类似的搜索查询(使用上面的示例):

    +catalog +products +versions
    

    强制所有术语都存在。

    请注意,如果同一组术语可以以不同的顺序出现,这会变得更加复杂,例如:

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

    在这种情况下,您需要使用与标准分析器中的分词器不同的 Lucene 分词器。

    【讨论】:

      猜你喜欢
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      相关资源
      最近更新 更多