import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
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.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
/**
* Lucene create Index and search key word
*
* @author Hongten (hongtenzone@foxmail.com) <br />
* @date 2012-11-28
*/
public class LuceneIndex {
/**
* 创建索引
*/
public void index() {
IndexWriter writer = null;
try {
// 1.创建Directory
// 这种方式是建立在内存中
// Directory directory = new RAMDirectory();
// 这种方式是存放在硬盘中
Directory directory = FSDirectory.open(new File(
"D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index"));
// 2.创建IndexWriter
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35));
writer = new IndexWriter(directory, iwc);
// 3.创建Document对象
Document doc = null;
// 4.为Document添加Field
File f = new File(
"D:/WordPlace/lucene/lucene_0100_helloworld/lucene/example");
for (File file : f.listFiles()) {
doc = new Document();
doc.add(new Field("content", new FileReader(file)));
doc.add(new Field("filename", file.getName(), Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
// 5.通过IndexWriter添加文档到索引中
writer.addDocument(doc);
}
} catch (CorruptIndexException ce) {
ce.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (CorruptIndexException e2) {
e2.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
/**
* 搜索关键字为key的n条记录
*
* @param key
* 关键字
* @param n
* 搜索的记录数
*/
public void search(String key, int n) {
try {
// 1.创建Directory
Directory directory = FSDirectory.open(new File(
"D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index"));
// 2.创建IndexReader
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
IndexReader reader = IndexReader.open(directory);
// 3.根据IndexReader创建IndexSearcher
IndexSearcher searcher = new IndexSearcher(reader);
// 4.创建搜索的Query
// 创建parser来确定要搜索的文件的内容,第二个参数表示搜索的域
QueryParser parser = new QueryParser(Version.LUCENE_35, "content",analyzer);
// 创建query,表示搜索域为content中包含key的文档
Query query = parser.parse(key);
// 5.根据searcher搜索并返回TopDocs
TopDocs tds = searcher.search(query, n);
// 6.根据TopDocs获取ScoreDoc对象
ScoreDoc[] sds = tds.scoreDocs;
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<span>", "</span>");
Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
for (int i=0;i<sds.length;i++) {
// 7.根据searcher和ScordDoc对象获取具体的Document对象
Document document = searcher.doc(sds[i].doc);
document.get("filename");
}
// 9.关闭reader
reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}