import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene.analysis.Analyzer;
import
org.apache.lucene.document.Document;
import
org.apache.lucene.document.Field;
import
org.apache.lucene.index.IndexReader;
import
org.apache.lucene.index.IndexWriter;
import
org.apache.lucene.index.IndexWriterConfig;
import
org.apache.lucene.index.Term;
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.Formatter;
import
org.apache.lucene.search.highlight.Highlighter;
import
org.apache.lucene.search.highlight.QueryScorer;
import
org.apache.lucene.search.highlight.Scorer;
import
org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import
org.apache.lucene.store.Directory;
import
org.apache.lucene.store.FSDirectory;
import
org.apache.lucene.util.Version;
import
org.wltea.analyzer.lucene.IKAnalyzer;
import
org.wltea.analyzer.lucene.IKSimilarity;
/**
* lucene3.5+ik的例子
*
* @author steven
* @date 2012-3-1 下午2:12:24
*/
public
class
LuceneDemo {
File dataFile =
new
File(
"D://indexFile"
);
Analyzer analyzer =
new
IKAnalyzer();
public
void
bulidIndex(){
Directory directory =
null
;
IndexWriter writer =
null
;
try
{
directory = FSDirectory.open(dataFile);
IndexWriterConfig writerConfig =
new
IndexWriterConfig(Version.LUCENE_35, analyzer);
writer =
new
IndexWriter(directory, writerConfig);
writer.addDocument(addDocument(
1
,
"聚资库"
,
"聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。"
));
writer.addDocument(addDocument(
2
,
"聚资库"
,
"聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。"
));
writer.addDocument(addDocument(
3
,
"聚资库"
,
"聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。"
));
writer.addDocument(addDocument(
4
,
"资料"
,
"聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。"
));
writer.addDocument(addDocument(
5
,
"微知识"
,
"聚资库,是以分享知识与经验的学习交流平台,加入聚资库,学习新知识、结交新朋友、塑造个人形象。"
));
}
catch
(Exception ex){
ex.printStackTrace();
}
finally
{
try
{
writer.close();
directory.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
/**
* 添加Document
*/
public
Document addDocument(Integer id, String title, String content) {
Document doc =
new
Document();
doc.add(
new
Field(
"id"
, String.valueOf(id), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(
new
Field(
"title"
, title, Field.Store.YES, Field.Index.ANALYZED));
doc.add(
new
Field(
"content"
, content, Field.Store.YES, Field.Index.ANALYZED));
return
doc;
}
/**
* 更新索引
*/
public
void
update(Integer id, String title, String content) {
try
{
Directory directory = FSDirectory.open(dataFile);
IndexWriterConfig writerConfig =
new
IndexWriterConfig(Version.LUCENE_35, analyzer);
IndexWriter writer =
new
IndexWriter(directory, writerConfig);
Document doc = addDocument(id, title, content);
Term term =
new
Term(
"id"
, String.valueOf(id));
writer.updateDocument(term, doc);
writer.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 删除索引
*/
public
void
delete(Integer id) {
try
{
Directory directory = FSDirectory.open(dataFile);
IndexWriterConfig writerConfig =
new
IndexWriterConfig(Version.LUCENE_35, analyzer);
IndexWriter writer =
new
IndexWriter(directory, writerConfig);
Term term =
new
Term(
"id"
, String.valueOf(id));
writer.deleteDocuments(term);
writer.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 搜索
* @param where 搜索条件
* @param after 分页时要用到,不分页时为null
*/
public
void
search(String where, ScoreDoc after) {
IndexSearcher isearcher =
null
;
Directory directory =
null
;
try
{
directory = FSDirectory.open(dataFile);
IndexReader reader = IndexReader.open(FSDirectory.open(dataFile),
true
);
isearcher =
new
IndexSearcher(reader);
isearcher.setSimilarity(
new
IKSimilarity());
QueryParser parser =
new
QueryParser(Version.LUCENE_35,
null
, analyzer);
Query query = parser.parse(where);
TopDocs topDocs = isearcher.searchAfter(after, query,
10
);
ScoreDoc[] hits = topDocs.scoreDocs;
Formatter formatter =
new
SimpleHTMLFormatter(
"<font color='red'>"
,
"</font>"
);
Scorer scorer =
new
QueryScorer(query);
Highlighter highlighter =
new
Highlighter(formatter, scorer);
for
(ScoreDoc scoreDoc : hits){
Document hitDoc = isearcher.doc(scoreDoc.doc);
String id = hitDoc.get(
"id"
);
String title = hitDoc.get(
"title"
);
String content = hitDoc.get(
"content"
);
float
score = scoreDoc.score;
title = highlighter.getBestFragment(analyzer,
"title"
, title);
content = highlighter.getBestFragment(analyzer,
"content"
, content);
if
(title ==
null
){
title = hitDoc.get(
"title"
);
}
if
(content ==
null
){
content = hitDoc.get(
"content"
);
}
System.out.println(
"doc:"
+ scoreDoc.doc +
" score:"
+ score +
" id:"
+ id +
" title:"
+ title +
" content:"
+ content);
}
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
finally
{
try
{
isearcher.close();
directory.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}