package com; import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.wltea.analyzer.lucene.IKAnalyzer; public class Index { public static void main(String[] args) { // Lucene Document的域名 String fieldName = "discript"; // 检索内容 String text = "是一个好人民,住在地球村,拥有别墅,花园。"; // 实例化IKAnalyzer分词器 Analyzer analyzer = new IKAnalyzer(); Directory directory = null; IndexWriter iwriter = null; File file = new File("C:/commFile/lucence"); try { directory = FSDirectory.open(file); iwriter = new IndexWriter(directory,analyzer,true,IndexWriter.MaxFieldLength.LIMITED); //建立文档 Document doc = new Document(); doc.add(new Field(fieldName, text, Field.Store.YES,Field.Index.ANALYZED)); doc.add(new Field("name", "anay", Field.Store.YES,Field.Index.ANALYZED)); Document doc2 = new Document(); doc2.add(new Field(fieldName, "中国人民", Field.Store.YES,Field.Index.ANALYZED)); doc2.add(new Field("name", "国家", Field.Store.YES,Field.Index.ANALYZED)); Document doc3 = new Document(); doc3.add(new Field(fieldName, "要作主", Field.Store.YES,Field.Index.ANALYZED)); doc3.add(new Field("name", "人民要作主", Field.Store.YES,Field.Index.ANALYZED)); iwriter.addDocument(doc3); iwriter.addDocument(doc2); iwriter.addDocument(doc); // 索引优化 iwriter.optimize(); iwriter.close(); }catch(Exception e ){ e.printStackTrace(); } } }
Lucene建立索引
猜你喜欢
转载自javafu.iteye.com/blog/1965833
今日推荐
周排行