package cn.jiayen.Dao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
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.util.Version;
import cn.jiayen.Util.LuceneUtil;
import cn.jiayen.domain.Article;
import cn.jiayen.domain.QueryResult;
public class LuceneDao implements LuceneDaoInterFace {
IndexWriter indexWriter = LuceneUtil.getIndexWriter();
//添加索引库
@Override
public void save(Article article){
try {
indexWriter.addDocument(LuceneUtil.getArticle2doucment(article));
indexWriter.commit();
} catch (IOException e) {
LuceneUtil.rollBack();
e.printStackTrace();
}
}
@Override
public void delete(Integer id){
// IndexWriter indexWriter = LuceneUtil.getIndexWriter();
Term term = new Term(“id“ id.toString());
try {
indexWriter.deleteDocuments(term);
indexWriter.commit();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void update(Article article){
//更新是先删除,再创建
// IndexWriter indexWriter = LuceneUtil.getIndexWriter();
Term term = new Term(“id“ article.getId().toString());
try {
indexWriter.updateDocument(term LuceneUtil.getArticle2doucment(article));
}catch (IOException e) {
e.printStackTrace();
}
}
@Override
public QueryResult query(String queryStringint firstResult int maxResult){
IndexSearcher indexSearcher = null;
try {
//把查询结构转换为Query对象
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30new String[]{“content““title“} LuceneUtil.getAnalyzer());
Query query = queryParser.parse(queryString);
//执行查询结果
indexSearcher = new IndexSearcher(LuceneUtil.getDirecotry());
TopDocs topDocs = indexSearcher.search(query 1000);
int count = topDocs.totalHits;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//分页处理
int endIndex = Math.min(firstResult+maxResult scoreDocs.length);
//处理查询结果
List list = new ArrayList();
for (int i = firstResult; i < endIndex; i++) {
// float score = scoreDocs[i].score;//相关度得分
//根据编号获取document对象
int docNo = scoreDocs[i].doc;//内部编号
Document document = indexSearcher.doc(docNo);
//把document对象转换为article添加到集合
Article article = LuceneUtil.getDoucment2article(document);
list.add(article);
}
return new QueryResult(list count);
} catch (Excep
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 649 2013-02-23 13:19 Lunece\.classpath
文件 382 2013-02-23 13:16 Lunece\.project
文件 629 2013-02-23 13:16 Lunece\.settings\org.eclipse.jdt.core.prefs
文件 4806 2013-02-23 20:43 Lunece\bin\cn\jiayen\Dao\LuceneDao.class
文件 411 2013-02-23 18:10 Lunece\bin\cn\jiayen\Dao\LuceneDaoInterFace.class
文件 971 2013-02-23 13:19 Lunece\bin\cn\jiayen\domain\Article.class
文件 1176 2013-02-23 18:13 Lunece\bin\cn\jiayen\domain\QueryResult.class
文件 829 2013-02-23 15:26 Lunece\bin\cn\jiayen\lunece\Lucene$1.class
文件 6159 2013-02-23 15:26 Lunece\bin\cn\jiayen\lunece\Lucene.class
文件 3221 2013-02-23 20:39 Lunece\bin\cn\jiayen\Test\LuceneDaoTest.class
文件 934 2013-02-23 19:57 Lunece\bin\cn\jiayen\Util\LuceneUtil$1.class
文件 4015 2013-02-23 19:57 Lunece\bin\cn\jiayen\Util\LuceneUtil.class
文件 20 2013-02-23 20:34 Lunece\indexDir\segments.gen
文件 1468 2013-02-23 20:34 Lunece\indexDir\segments_55
文件 22106 2013-02-23 20:30 Lunece\indexDir\_3m.cfs
文件 16 2013-02-23 20:32 Lunece\indexDir\_3m_1.del
文件 759 2013-02-23 20:30 Lunece\indexDir\_3n.cfs
文件 759 2013-02-23 20:30 Lunece\indexDir\_3o.cfs
文件 759 2013-02-23 20:30 Lunece\indexDir\_3p.cfs
文件 407 2013-02-23 20:32 Lunece\indexDir\_3q.cfs
文件 9 2013-02-23 20:33 Lunece\indexDir\_3q_1.del
文件 433 2013-02-23 20:33 Lunece\indexDir\_3r.cfs
文件 9 2013-02-23 20:34 Lunece\indexDir\_3r_1.del
文件 407 2013-02-23 20:34 Lunece\indexDir\_3s.cfs
文件 200780 2013-02-23 13:16 Lunece\lib\lucene-analyzers-3.0.1.jar
文件 1025635 2013-02-23 13:16 Lunece\lib\lucene-core-3.0.1.jar
文件 46963 2013-02-23 13:16 Lunece\lib\lucene-highlighter-3.0.1.jar
文件 27626 2013-02-23 13:16 Lunece\lib\lucene-memory-3.0.1.jar
文件 3297 2013-02-23 20:43 Lunece\src\cn\jiayen\Dao\LuceneDao.java
文件 385 2013-02-23 18:10 Lunece\src\cn\jiayen\Dao\LuceneDaoInterFace.java
............此处省略28个文件信息
评论
共有 条评论