lucene3.0 CRUD实例(三)
package com.wj.lucene;import java.io.File;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.SimpleFSDirectory;import org.apache.lucene.util.Version;/** * Lucene3.0 CRUD操作 * 更能如下: * 追加、查询所有、删除索引 * * @author jcom * @date 2010-9-28 * */public class zjIndex{private static final Log LOGGER = LogFactory.getLog(zjIndex.class);private static String path = "c:/index";private static Directory dir = null;public static void main(String[] args) throws Exception{dir = new SimpleFSDirectory(new File(path)); //追加数据superAdditionIndex();//查询输出所有数据searchAll();//删除deleteIndex();}/** * 追加数据 */public static void superAdditionIndex(){System.out.println("追加开始==============");try{IndexWriter write = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), false,IndexWriter.MaxFieldLength.UNLIMITED);Document doc = new Document();doc.add(new Field("id", "234567", Field.Store.YES,Field.Index.NOT_ANALYZED));doc.add(new Field("userName", "王二", Field.Store.YES,Field.Index.ANALYZED));doc.add(new Field("comefrom", "南京", Field.Store.YES,Field.Index.ANALYZED));Term term = new Term("id", "234567");/** * 调用updateDocument的方法,传给它一个新的doc来更新数据, * Term term = new Term("id","234567"); * 先去索引文件里查找id为234567的Doc,如果有就更新它(如果有多条,最后更新后只有一条)。如果没有就新增. * * 数据库更新的时候,我们可以只针对某个列来更新,而lucene只能针对一行数据更新。 */write.updateDocument(term, doc);write.commit();write.close();} catch (Exception e){LOGGER.info(e.getMessage());}System.out.println("追加王二成功==========");System.out.println("追加结束==============\n");}