Lucene 索引创建

import java.io.File;
import java.util.ArrayList;

import org.apache.lucene.analysis.Analyzer;
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.dom4j.DocumentException;
import org.wltea.analyzer.lucene.IKAnalyzer;


/**
 * @category 创建所有XML索引
 *
 */
public class CreatIndex {
	private String INDEX_STORE_PATH ;
	
	//创建索引
	@SuppressWarnings("deprecation")
	public void creatIndex(){
		try{
			GetPath path = new GetPath();
			INDEX_STORE_PATH = path.getIndexPath();
			File file = new File(INDEX_STORE_PATH);
			Analyzer analyzer = new IKAnalyzer();
			XmlReader xml = new XmlReader();
			FSDirectory directory = FSDirectory.open(file);
			IndexWriter writer = new IndexWriter(directory, analyzer, true,IndexWriter.MaxFieldLength.LIMITED);
			ArrayList<String> lisId = xml.getId();
			ArrayList<String> lisTitle = xml.getTitle();
			ArrayList<String> lisKeyWords = xml.getKeyWords();
			ArrayList<String> lisKind = xml.getKind();
			ArrayList<String> lisDescribe = xml.getDescribe();
			ArrayList<String> lisDate = xml.getDate();
			ArrayList<String> lisUrl = xml.getUrl();
			ArrayList<String> lisAuthor = xml.getAuthor();
			ArrayList<String> lisPublisher = xml.getPublisher();
		
			//System.out.println(lisUrl.get(5));
			for (int i = 0; i < xml.getCount();i++){
				Document doc = new Document();
				//为ID创建Field
				
				Field field = new Field("id",lisId.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED );
				doc.add(field);
				//为title创建索引
				
			    field = new Field("title",lisTitle.get(i),Field.Store.YES,Field.Index.ANALYZED);
				doc.add(field);
				//为keywords创建索引
				
			    field = new Field("keywords",lisKeyWords.get(i),Field.Store.YES,Field.Index.ANALYZED);
				doc.add(field);
				//为kind创建索引
				
				field = new Field("kind",lisKind.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
				doc.add(field);
				//为describe创建索引
				
			     field = new Field("describe",lisDescribe.get(i),Field.Store.YES,Field.Index.ANALYZED);
				doc.add(field);
				//为data创建索引
				
			    field = new Field("date",lisDate.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
				doc.add(field);
				//为URL创建索引
				
				field = new Field("url",lisUrl.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
				doc.add(field);
				//为author创建索引
				
				field = new Field("author",lisAuthor.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
				doc.add(field);
				//为publisher创建索引
				
				field = new Field("publisher",lisPublisher.get(i),Field.Store.YES,Field.Index.NOT_ANALYZED);
				doc.add(field);
				
								}
				
				writer.addDocument(doc);
			}   
			
			
			
			
			 writer.close();
			 //directory.close();
			System.out.println("索引创建完毕");
			
			
		}  catch (Exception e){
			e.printStackTrace();
		
		}
		
	}
  
	public static void main(String [] args) throws DocumentException{
		CreatIndex index = new CreatIndex();
		index.creatIndex();
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值