Lucene小练五(复习index)

来源:互联网 发布:hp1005扫描仪软件 编辑:程序博客网 时间:2024/06/06 14:08
package org.se.lucene;//主类import java.io.File;import java.io.FileReader;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;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.util.Version;public class hellowLucene {public void index(){IndexWriter writer=null;try {//1.创建directoryDirectory directory=FSDirectory.open(new File("F/lucene/Index03"));//2.创建IndexWriter writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_36,new StandardAnalyzer(Version.LUCENE_36)));//3.创建Document对象Document document=null;//4.为Document添加FieldFile f=new File("f:/lucene/lucenes");   for(File file:f.listFiles())   {   document=new Document();   document.add(new Field("content",new FileReader(file)));   document.add(new Field("filename",file.getName(),Field.Store.YES,   Field.Index.NOT_ANALYZED));   document.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,   Field.Index.NOT_ANALYZED));   } //5.通过IndexWriter添加文档到索引中   writer.addDocument(document);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(writer!=null)writer.close();     }catch (CorruptIndexException e2) {e2.printStackTrace();// TODO: handle exception}catch (IOException e2) {e2.printStackTrace();// TODO: handle exception}}}}//测试类package org.se.lucene;import org.junit.Test;public class LuceneTest {@Testpublic void test_Index(){hellowLucene hLucene=new hellowLucene();hLucene.index();}}


原创粉丝点击