rainyzz's blog

Lucene索引合并

在项目中需要将许多个小索引合并成一个大的索引,INDEX_PATH是许多索引所在的文件夹,INDEX_BASE_PATH是最终要合并生成大索引所在的文件夹。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexUpgrader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
public class MergeIndex {
private final String INDEX_PATH = "C:/Users/rainystars/Desktop/allindex/";
private final String INDEX_BASE_PATH = "C:/base";
public static void main(String[] args) {
MergeIndex m = new MergeIndex();
m.merge();
}
public void merge(){
File baseDir = new File(INDEX_BASE_PATH);
File dir = new File(INDEX_PATH);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
String path = "";
String[] dirList = dir.list();
try {
IndexWriter writer = new IndexWriter(FSDirectory.open(baseDir), iwc);
for (String d:dirList){
path = INDEX_PATH + "/" + d;
writer.addIndexes(FSDirectory.open(new File(path)));
System.out.println(path+" Done.");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("All Done.");
}
}