一、Lucene入门实例
程序员文章站
2022-07-09 09:55:51
...
package cn.lucene.tramp;
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.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.store.RAMDirectory;
import org.apache.lucene.util.Version;
/**
* ClassName:HelloLucene <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2014年2月5日 下午12:59:10 <br/>
* @author zhangzhaoyu
* @version
* @since JDK 1.7
* @see
*/
public class HelloLucene {
/**
*
* index:<br />
* 建立索引
*
* @author zhangzhaoyu
*/
public void index() {
IndexWriter writer = null;
try {
// 1. 创建 directory
// Directory directory = new RAMDirectory(); // 建立在内存中
Directory directory = FSDirectory.open(new File("E:\\BaiduYunDownload\\lucene\\index01"));
// 2. 创建 IndexWriter
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
writer = new IndexWriter(directory, config);
// 3. 创建Document 对象
Document document = null;
// 4. 为Document 添加Field
File f = new File("E:\\BaiduYunDownload\\lucene\\example");
System.out.println("filesize: " + f.listFiles().length);
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();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* searcher:<br />
* 搜索
*
* @author zhangzhaoyu
*/
public void searcher() {
try {
// 1. 创建 Directory
Directory directory = FSDirectory.open(new File("E:\\BaiduYunDownload\\lucene\\index01"));
// 2. 创建 IndexReader
IndexReader reader = IndexReader.open(directory);
// 3. 根据 IndexReader 创建 IndexSearcher
IndexSearcher searcher = new IndexSearcher(reader);
// 4. 创建搜索的 Query
// 创建parser 来确定搜索文件的内容,第二个参数表示搜索的域
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
// 创建 query, 表示搜索域为content中包含的java 文档
Query query = parser.parse("dojo");
// 5. 根据 searcher搜索并返回 TopDocs
TopDocs topDocs = searcher.search(query, 10);
// 6. 根据 TopDocs 获取 SocreDoc 对象
ScoreDoc[] sds = topDocs.scoreDocs;
for (ScoreDoc sd : sds) {
// 7. 根据 searcher 和 SocreDoc对象获取具体的 Document对象
Document d = searcher.doc(sd.doc);
// 8. 根据 Document 对象获取需要的值
System.out.println(d.get("filename") + ": " + d.get("path"));
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
public class TestLucene {
@Ignore
public void testIndex() {
HelloLucene lucene = new HelloLucene();
lucene.index();
}
@Test
public void testSearch() {
HelloLucene lucene = new HelloLucene();
lucene.searcher();
}
}