package cn.com.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.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
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;
public class HelloLucene {
/**
* 建立索引
*/
public void index(){
//1、创建directory
//内存索引
// Directory directory = new RAMDirectory();
Directory directory = null;
//2、创建IndexWriter
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35));
IndexWriter writer = null;
try {
//硬盘索引
directory = FSDirectory.open(new File("d:/lucene/index01"));
writer = new IndexWriter(directory,iwc);
//3、创建Document对象
Document doc = null;
//4、为Document添加Field
File file = new File("d:/lucene/example");
for(File f:file.listFiles()){
doc = new Document();
doc.add(new Field("content",new FileReader(f)));
doc.add(new Field("fileName",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
}} catch (CorruptIndexException e) {e.printStackTrace();} catch (LockObtainFailedException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(writer!=null)try {writer.close();} catch (CorruptIndexException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}/** * 搜索 */public void searcher(){Directory directory = null;IndexReader indexReader = null;try {//创建Directorydirectory = FSDirectory.open(new File("d:/lucene/index01"));//创建IndexReader()indexReader = IndexReader.open(directory);//根据IndexReader创建IndexSearcherIndexSearcher searcher = new IndexSearcher(indexReader);//创建搜索的Query//创建parser来确定要搜索文件的内容,第二个参数表示搜索的域QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));//创建Query,表示搜索域为content中包含java的文档Query query = parser.parse("JAVA");//根据searcher搜索并返回TopDocsTopDocs topDocs = searcher.search(query, 10);//根据TopDocs获取ScoreDoc对象ScoreDoc[] sds = topDocs.scoreDocs;for(ScoreDoc sd :sds){//根据searcher和ScoreDoc对象获取具体的Document对象Document doc = searcher.doc(sd.doc);//根据Document对象获取需要的值System.out.println(doc.get("fileName")+"["+doc.get("path")+"]");}//关闭ReaderindexReader.close();} catch (IOException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();}}}//5、通过IndexWriter添加文档到索引中 writer.addDocument(doc);
测试代码:
package cn.com.lucene;
import org.junit.Test;
public class TestLucene {
@Test
public void testIndex(){
HelloLucene hlc = new HelloLucene();
hlc.index();
}
@Test
public void testSearch(){
HelloLucene hlc = new HelloLucene();
hlc.searcher();
}
}