我正在尝试使用java程序合并lucene索引。 我的要求是替换已经拥有的文件并为其合并新文件我使用以下代码。
try{
File INDEX_DIR= new File("path of first directory");
INDEX_DIR.mkdir();
File indexDir = new File("path of first directory");
Directory indexes[] = new Directory[indexDir.list().length];
IndexWriter writer = new IndexWriter(
FSDirectory.open(INDEX_DIR),
new SimpleAnalyzer(),true,
IndexWriter.MaxFieldLength.LIMITED);
indexes[0] = FSDirectory.open(indexDir);
writer.addIndexes(indexes[0]);
File indexDir1 = new File("path of second directory");
Directory indexes1[] = new Directory[indexDir1.list().length];
indexes1[0] = FSDirectory.open(indexDir1);
writer.addIndexes(indexes1[0]);
writer.optimize();
writer.close();
System.out.println("index merged");
}
catch(Exception ex) {
ex.printStackTrace();
}