1. 程式人生 > >lucene3.1.0+luke3.5.0開發中遇到的問題解決辦法

lucene3.1.0+luke3.5.0開發中遇到的問題解決辦法

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class IndexFiles {

    /**
     * @param args
     * @throws IOException
     * @throws LockObtainFailedException
     * @throws CorruptIndexException
     */
    public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException {
        File path=new File("e:/tts");
        Directory dir=FSDirectory.open(path);
        Analyzer analyzer=new IKAnalyzer();
        IndexWriterConfig conf=new IndexWriterConfig(Version.LUCENE_31, analyzer);
        //建立索引模式:create(覆蓋模式);append(追加模式)
        conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        IndexWriter indexWriter=new IndexWriter(dir, conf);
        Document document=new Document();
        document.add(new Field("name","zhangs",Store.YES,Index.NOT_ANALYZED_NO_NORMS));
        document.add(new Field("age","30",Store.YES,Index.NOT_ANALYZED_NO_NORMS));
        indexWriter.addDocument(document);
        indexWriter.commit();
        indexWriter.close();
        dir.close();
    }

}