//合并两个索引目录
package directory;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
public class Merge {
try{
//初始化一个RAMDirectory对象
RAMDirectory ramDir = new RAMDirectory();
//初始化一个FSDirectory对象
FSDirectory fsDir = FSDirectory.getDirectory("", true);
//构建一个索引,并以文件系统的目录作为其目录路标
IndexWriter fsWriter= new IndexWriter(ramDir, new StandardAnalyzer(), true);
//构建一个索引器,并以内存作为目标路径
IndexWriter ramWriter = new IndexWriter(ramDir, new StandardAnalyzer(), true);
//创建第一个文档
Document doc1 = new Document();
Field bookNo1 = new Field("booknumber", "BOOKNUM1", Field.Store.YES, Field.Index.TOKENIZED);
doc1.add(bookNo1);
//创建第2个文档
Document doc2 = new Document();
Field bookNo2 = new Field("booknumber", "BOOKNUM2",Field.Store.YES, Field.Index.TOKENIZED);
doc2.add(bookNo2);
//将第一个文档加入内存目录中
ramWriter.addDocument(doc1);
//关闭内存索引器
ramWriter.close();
//将第二个文档加入磁盘目录中
fsWriter.addDocument(doc2);
//合并索引
fsWriter.addIndexes(new Directory[]{ramDir});
//关闭磁盘索引器
fsWriter.close();
}catch(Exception e)
{
e.printStackTrace();
}
}