lucene 索引創建步驟


一、步驟:

  1.存儲位置:1)文件:

Directory dir= FSDirectory.open(new File("D:\\LuceneIndex"));

 

        2)內存:

new RAMDirectory(FSDirectory.getDirectory(file));//不建議,只會把一些搜索相關的信息放入到內存,不是全部的索引文件

 

  2.分詞器:

Analyzer analyzer=new IKAnalyzer();//這個是中文分詞器,並不是lucene自帶的(StandardAnalyzer)

 

  3.創建IndexWriter配置實例IndexWriterConfig:

IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_4,analyzer );

 

    IndexWriterConfig實例的setter方法可以設置IndexWriter的配置。

  4.創建IndexWriter:

IndexWriter  iwrite=new IndexWriter(dir, config);

  5.創建Document域:

Document doc=new Document();

  6.創建Field實例

Field title=new TextField("title", rs.getString("title"),Store.YES);

    或者:

FieldType type = new FieldType();
type.setStored(true);
type.setIndexed(true);
type.setTokenized(false);
Field id=new Field("id",String.valueOf(rs.getInt("id")), type);

  7.把Field實例添加到Document域中:

doc.add(id);
doc.add(title);

  8.IndexWriter把Document域寫入索引文件:

iwrite.addDocument(doc);

  9.提交、關閉IndexWriter

iwrite.commit();
iwrite.close();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM