www.pudn.com > lucene+mysql+eclipe.rar > IndexerFile.java


package com.lucene; 
 
import java.io.IOException; 
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.index.IndexWriter; 
import org.apache.lucene.document.Field; 
import jeasy.analysis.MMAnalyzer;; 
 
/* 
 * @CopyRigth(R) 城市通 
 * @author 申华锋 E-mail:leonshine@qq.com 
 * @version 创建时间:Mar 9, 2008 12:12:45 AM 
 * @description lucene查询索引 
 */ 
public class IndexerFile { 
	/* 
	 * 把数据库查处的Photo列表建立索引 
	 */ 
	public static int indexFile(String indexDir, Product[] list) 
			throws IOException { 
		//IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),true); 
		IndexWriter writer = new IndexWriter(indexDir, new MMAnalyzer(),true); 
		writer.setUseCompoundFile(false); 
		for (int i = 0; i < list.length; i++) { 
			Document doc = new Document(); 
			doc.add(new Field("productId", String.valueOf(list[i].getProductId()), 
					Field.Store.YES, Field.Index.NO)); 
			if (list[i].getTitle() != null) 
				doc.add(new Field("title", list[i].getTitle(), Field.Store.YES, 
						Field.Index.TOKENIZED)); 
			/*if (list[i].getDescription() != null) 
				doc.add(new Field("description", list[i].getDescription(), 
						Field.Store.YES, Field.Index.TOKENIZED)); 
			doc.add(new Field("address", list[i].getAddress(), Field.Store.YES, 
					Field.Index.NO)); 
			doc.add(new Field("userName", list[i].getUserName(), 
					Field.Store.YES, Field.Index.TOKENIZED)); 
			doc.add(new Field("userId", String.valueOf(list[i].getUserId()), 
					Field.Store.YES, Field.Index.NO)); 
			if (list[i].getTag().length() > 0) 
				doc.add(new Field("tag", list[i].getTag(), Field.Store.YES, 
						Field.Index.TOKENIZED)); 
			doc.add(new Field("uploadTime", "2006-10-26", Field.Store.YES, 
					Field.Index.TOKENIZED));*/ 
			writer.addDocument(doc); 
		} 
 
		int numIndexed = writer.docCount(); 
		writer.optimize(); 
		writer.close(); 
		return numIndexed; 
	} 
}