我想先为我的英语不好道歉 我是新手放松,我并不真正理解查询文档,我索引了一些文档并制作了此查询代码,但它不起作用
Term t = new Term("description", "history");
Query q = new FuzzyQuery(t, 2);
int hitsPerPage = 100;
Path indexPath = Paths.get("C:\Users\Win 7\Desktop\projet_ri\index");
Directory directory = FSDirectory.open(indexPath);
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher iSearcher = new IndexSearcher(reader);
TopDocs topdocs = iSearcher.search(q, hitsPerPage);
ScoreDoc[] resultsList = topdocs.scoreDocs;
System.out.println("Tab size: "+resultsList.length); // This prints Tab size: 0
for(int i = 0; i<resultsList.length; i++){
Document book = iSearcher.doc(resultsList[i].doc);
String description = book.getField("description").stringValue();
System.out.println(description);
}
该程序甚至没有进入循环,我尝试检查结果列表选项卡,它打印出大小为零 有人可以帮助我更正我的代码或给我一个查询示例代码吗?
您实际上错过了使用QueryParser进行查询。 此查询分析器需要与用于索引的相同分析器。这非常重要,否则结果集可能与您预期的结果不同。您的序列应该是这样的:
- 打开索引
- 创建索引搜索器 使用
- 索引使用的分析器创建查询解析器
- 使用给定的搜索词创建查询
- 使用 QueryParser 解析查询
- 搜索
- 关闭一切!
请参阅基本的 lucene 教程:https://www.tutorialspoint.com/lucene/lucene_search_operation.htm