我即将编写一个带有分布式索引的近实时搜索应用程序。现在我想知道在多个索引上实现搜索的正确方法是什么:
我已经阅读过关于MultiSearcher的内容,因此有一个approch:
IndexSearcher[] indexSearchers = new IndexSearcher[indexCount];
for (int i = 0; i < indexCount; i++) {
File directory = new File(indexdir, String.valueOf(i));
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(directory), analyzer, IndexWriter.MaxFieldLength.LIMITED);
IndexReader indexReader = indexWriter.getReader();
indexSearchers[i] = new IndexSearcher(indexReader);
}
MultiSearcher searcher = new MultiSearcher(indexSearchers);
但我认为这也是可能的:
IndexReader[] indexReader = new IndexReader[indexCount];
for (int i = 0; i < indexCount; i++) {
File directory = new File(indexdir, String.valueOf(i));
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(directory), analyzer, IndexWriter.MaxFieldLength.LIMITED);
indexReader[i] = indexWriter.getReader();
}
IndexSearcher searcher = new IndexSearcher(new MultiReader(indexReader));
这两个方法之间有什么显着差异吗?如果读者没有数据,第二个将更容易处理,因为我可以调用MultiReader.reopen()而不是遍历所有IndexReader,重新打开它们而不是创建新的IndexSearchers ......
答案 0 :(得分:3)