lucene+nutch学习笔记六:lucene使用需要注意的地方

 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();
		}
		
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值