package org.sam.demo.lucene; import java.io.IOException; import java.sql.SQLException; import java.util.List; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.LockObtainFailedException; import org.sam.demo.mysql.Authors; import org.sam.demo.mysql.DBOperation; /** * @author Sam * @copyright_code 123456789987654321 * @Date 2008-09-16 * */ public class IndexCreate { /* 索引存放的路径,如果找不到会有IOException */ private final String PATH="E:/Lucene/Index6"; public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException, SQLException, ClassNotFoundException { IndexCreate one = new IndexCreate(); // one.createIndexOne(new DBOperation().getAuthors("000e5f51-f671-47f0-a6bd-0c46a969f162")); one.createIndexList(new DBOperation().getAuthorses()); } /** * 建立一个对象的索引 * @throws IOException * @throws LockObtainFailedException * @throws CorruptIndexException * */ public void createIndexOne(Authors s) throws CorruptIndexException, LockObtainFailedException, IOException{ /* Field(String name, String value, Field.Store store, Field.Index index) 个人习惯选择这个构造方法 */ Field f1 = new Field("id",s.getAu_id(),Field.Store.YES,Field.Index.UN_TOKENIZED); Field f2 = new Field("lname",s.getAu_lname(),Field.Store.YES,Field.Index.TOKENIZED); Field f3 = new Field("fname",s.getAu_fname(),Field.Store.YES,Field.Index.TOKENIZED); Field f4 = new Field("phone",s.getPhone(),Field.Store.YES,Field.Index.TOKENIZED); Field f5 = new Field("address",s.getAddress(),Field.Store.YES,Field.Index.TOKENIZED); Field f6 = new Field("city",s.getCity(),Field.Store.YES,Field.Index.TOKENIZED); Field f7 = new Field("zip",s.getZip(),Field.Store.YES,Field.Index.TOKENIZED); Field f8 = new Field("state",s.getState(),Field.Store.YES,Field.Index.TOKENIZED); Field f9 = new Field("contract",s.getContract()+"",Field.Store.YES,Field.Index.TOKENIZED); Document doc = new Document(); doc.add(f1); doc.add(f2); doc.add(f3); doc.add(f4); doc.add(f5); doc.add(f6); doc.add(f7); doc.add(f8); doc.add(f9); IndexWriter writer = null; try { writer = new IndexWriter(PATH,new StandardAnalyzer(),true); writer.addDocument(doc); } finally{ writer.optimize(); writer.flush(); writer.close(); } } /** * 建立一个List对象的索引 * @throws IOException * @throws LockObtainFailedException * @throws CorruptIndexException * */ public void createIndexList(List<Authors> list) throws CorruptIndexException, LockObtainFailedException, IOException{ IndexWriter writer = null; writer = new IndexWriter(PATH,new StandardAnalyzer(),true); try { for(Authors s : list){ Field f1 = new Field("id",s.getAu_id(),Field.Store.YES,Field.Index.UN_TOKENIZED); Field f2 = new Field("lname",s.getAu_lname(),Field.Store.YES,Field.Index.TOKENIZED); Field f3 = new Field("fname",s.getAu_fname(),Field.Store.YES,Field.Index.TOKENIZED); Field f4 = new Field("phone",s.getPhone(),Field.Store.YES,Field.Index.TOKENIZED); Field f5 = new Field("address",s.getAddress(),Field.Store.YES,Field.Index.TOKENIZED); Field f6 = new Field("city",s.getCity(),Field.Store.YES,Field.Index.TOKENIZED); Field f7 = new Field("zip",s.getZip(),Field.Store.YES,Field.Index.TOKENIZED); Field f8 = new Field("state",s.getState(),Field.Store.YES,Field.Index.TOKENIZED); Field f9 = new Field("contract",s.getContract()+"",Field.Store.YES,Field.Index.TOKENIZED); Document doc = new Document(); doc.add(f1); doc.add(f2); doc.add(f3); doc.add(f4); doc.add(f5); doc.add(f6); doc.add(f7); doc.add(f8); doc.add(f9); writer.addDocument(doc); } } finally{ writer.optimize(); writer.flush(); writer.close(); } } }