首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

Lucene建立目录

2013-11-03 
Lucene建立索引package comimport java.io.Fileimport org.apache.lucene.analysis.Analyzerimport org

Lucene建立索引

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();}}}

热点排行