我正在使用 apache lucene 来索引 html 文件。我将 html 文件的路径存储在 lucene 索引中。它存储索引,并且我已经在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();
}
它显示找到的文档数作为文档总数,而搜索到的路径文件仅存在一次
最佳答案
您正在分析路径,这会将其拆分为单独的术语。根路径术语(例如 /catalog/products/versions 中的 catalog)可能会出现在所有文档中,因此任何包含 catalog 的搜索都不会强制所有强制性条款将返回所有文件。
您需要一个搜索查询,例如(使用上面的示例):
+catalog +products +versions
强制所有术语都存在。
请注意,如果同一组术语可以以不同的顺序出现,情况会变得更加复杂,例如:
/catalog/products/versions
/versions/catalog/products/SKUs
在这种情况下,您需要使用与标准分析器中的标记生成器不同的 Lucene 标记生成器。
关于java - apache lucene 在文件路径上索引和搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14745149/