1尽量减少不必要的存储。
基本的办法是在添加特定的文档时,使用不存储原始内容的Store.NO,或则压缩存储Store.COMPRESS
2不需要检索的内容不要建立索引 ,
3非格式化的文本需要提前转化 ,比如时间货浮点数字。
4需要整体存方的内容不要分词 ,比如readme.txt
5注意对索引参数的优化
主要用下面几个函数:
setMergeFactor(),设置合并参数。
setMaxbufferedDocs(); 设置最大文档个数。
setMaxFieldLength();限制域索引的个数。
setMaxBufferedDeleteTerms();设置最大内存删除项数。
6合理选择使用磁盘索引还是内存索引。 下面是一个使用内存索引的例子
package chapter5; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.RAMDirectory; public class LuceneIndexCombine { private static String dest_Index_Path = "D:\\workshop\\TextIndex"; private static String text_File_Path = "D:\\largeData\\xx.txt"; public static void main(String[] args) throws IOException{ Date start=new Date(); try { File file=new File(text_File_Path); FileReader reader=new FileReader(file); Directory dir=FSDirectory.getDirectory(dest_Index_Path); Directory ramdir=new RAMDirectory(); Analyzer analyzer=new SimpleAnalyzer(); IndexWriter index=new IndexWriter(dir,analyzer,true); IndexWriter ramIndex=new IndexWriter(ramdir,analyzer,true); Document document=new Document(); Field name=new Field("path",file.getName(),Field.Store.YES,Field.Index.UN_TOKENIZED); document.add(name); Field content=new Field("content",reader); document.add(content); ramIndex.addDocument(document); ramIndex.close(); index.addIndexes(new Directory[]{ramdir}); index.optimize(); index.close(); Date end=new Date(); long tm_index=end.getTime()-start.getTime(); System.out.println("Total Time:(ms)"); System.out.println(tm_index); IndexReader ramIndexReader=IndexReader.open(ramdir); System.out.println("Ram Index Reader doc nums:"+ramIndexReader.maxDoc()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 相关资源:敏捷开发V1.0.pptx