import java.util.ArrayList; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriter.MaxFieldLength; 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.TopDocs; import cn.itcast.cc.lucene.helloword.Article; import cn.itcast.cc.lucene.helloword.utils.ArticleDocUtils; public class IndexDao { // 索引目录 private String indexPath = "./index"; // 分词器 private Analyzer analyzer = new StandardAnalyzer(); /** * 保存记录 * * @param art */ public void save(Article art) { // lucene的写出索引类 IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexPath, this.analyzer, MaxFieldLength.LIMITED); // 添加到索引库 indexWriter.addDocument(ArticleDocUtils.Article2Doc(art)); } catch (Exception e) { e.printStackTrace(); } // 释放indexWriter if (indexWriter != null) { try { // 使用后一定要关闭 indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 更新记录 * * @param art */ public void update(Article art) { IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexPath, this.analyzer, MaxFieldLength.LIMITED); Term term = new Term("id", art.getId() + ""); // 更新 indexWriter.updateDocument(term, ArticleDocUtils.Article2Doc(art)); } catch (Exception e) { e.printStackTrace(); } // 释放indexWriter if (indexWriter != null) { try { indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 删除记录 * * @param id */ public void delete(int id) { IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexPath, this.analyzer, MaxFieldLength.LIMITED); Term term = new Term("id", id + ""); // 删除 indexWriter.deleteDocuments(term); } catch (Exception e1) { e1.printStackTrace(); } // 释放indexWriter if (indexWriter != null) { try { indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 查询记录(具有分页功能) * * @param queryString * @param startIndex * @param recordCount * @return */ public List search(String queryString, int startIndex, int recordCount) { List result = new ArrayList(); IndexSearcher indexSearcher = null; try { indexSearcher = new IndexSearcher(this.indexPath); String[] fields = new String[] {"title","content"}; // 分析查询条件 QueryParser queryParser = new MultiFieldQueryParser(fields, this.analyzer); // 生成查询对象 Query query = queryParser.parse(queryString); int findTotalRecord = startIndex + recordCount; // 查询 TopDocs topDocs = indexSearcher.search(query, null, findTotalRecord); //获取分页数据 int endIndex = Math.min(startIndex+recordCount,topDocs.totalHits); for(int i=startIndex; i<endIndex;i++){ result.add(indexSearcher.doc(topDocs.scoreDocs[i].doc)); } } catch (Exception e1) { e1.printStackTrace(); } // 释放indexWriter if (indexSearcher != null) { try { indexSearcher.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } } |