LuceneIndexList.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:2k
- package chapter5;
- import java.io.IOException;
- import java.io.File;
- import java.io.FileReader;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.RAMDirectory;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.analysis.SimpleAnalyzer;
- /*******************************************************************
- * 本代码完成已经建立的索引的合并和优化,用来把不同时间不同线程创建的索引进行合并,优化性能,
- * 提高检索速度。
- *******************************************************************/
- public class LuceneIndexList {
- private static String Dest_Index_Path = "D:\workshop\TextIndex";
- private static String Text_File_Path_Work = "D:\workshop\TextIndexstore";
- /*========================================================
- * 主函数,执行 Lucene 索引合并操作
- *========================================================*/
- public static void main(String[] args) {
-
- File indexpath = new File(Dest_Index_Path);
- File localPath = new File(Text_File_Path_Work);
-
- try {
- int nums = indexMerge(indexpath,localPath);
- System.out.println("Index Finished " + nums + " docs");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static int indexMerge( File indexPath , File localPath )
- throws IOException{
- IndexWriter fswriter = null ;
- IndexWriter ramwriter = null ;
- Directory ramDir = new RAMDirectory(); // 内存高速目录
- fswriter = new IndexWriter(indexPath, new SimpleAnalyzer(),false); // 追加模式添加索引
- ramwriter = new IndexWriter(ramDir, new SimpleAnalyzer(),true); // 构建内存RAM的索引
- FSDirectory[] fs = { FSDirectory.getDirectory(localPath , false) }; // 生成目录文件
- ramwriter.addIndexes(fs); // 添加目录中的索引到内存索引
- ramwriter.optimize(); // 内存索引优化
- ramwriter.close(); //关闭内存索引
-
- fswriter.addIndexes(new Directory[]{ramDir}); // 合并内存索引到目标地址指定的文件索引
- fswriter.close(); // 关闭文件索引
- return 0;
- }
- }