⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 luceneindexlist.java

📁 《lucene+nutch搜索引擎开发》源代码
💻 JAVA
字号:
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;
	}
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -