lucene06

来源:互联网 发布:数据接口程序 编辑:程序博客网 时间:2024/06/07 19:44
//主类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.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;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.apache.lucene.search.Query;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_35,new StandardAnalyzer(Version.LUCENE_35)));//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 (CorruptIndexException e) {e.printStackTrace();// TODO: handle exception}catch (LockObtainFailedException e) {e.printStackTrace();// TODO: handle exception}catch (IOException e) {e.printStackTrace();// TODO: handle exception}finally{try {if(writer!=null)writer.close();     }catch (CorruptIndexException e2) {e2.printStackTrace();// TODO: handle exception}catch (IOException e2) {e2.printStackTrace();// TODO: handle exception}}}public void search(){try {//1.创建DirectroyDirectory directory = FSDirectory.open(new File("f:/lucene/Index03"));//2.创建IndexReaderIndexReader reader = IndexReader.open(directory);//3.根据IndexReader创建IndexSearcherIndexSearcher searcher = new IndexSearcher(reader);//4.创建搜索的QueryQueryParser parser = new QueryParser(Version.LUCENE_35, "content",new StandardAnalyzer(Version.LUCENE_35));   Query query=parser.parse("ask");//5.根据qurey搜索并返回TopDocsTopDocs tds = searcher.search(query, 10);//6.根据TopDocs获取Scoredoc对象ScoreDoc[] sDoc = tds.scoreDocs;for (ScoreDoc sds : sDoc) {//7.根据searcher和scoredoc对象获取具体DocumentDocument document = searcher.doc(sds.doc);//8.根据Document获取具体的值System.out.println(document.get("filename") + "["+ document.get("path") + "]");}reader.close();} catch (Exception e) {e.printStackTrace();}}}//测试类package org.se.lucene;import org.junit.Test;public class LuceneTest {@Testpublic void test_Index(){hellowLucene hLucene=new hellowLucene();hLucene.index();}@Testpublic void test_Search(){hellowLucene hLucene=new hellowLucene();hLucene.search();}}