package com.kite.luncene.utils; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Field.Index; import org.apache.lucene.document.Field.Store; import com.kite.bean.Article; public class DocumentUtils { /** * 經過 article得到document * @param article * @return
*/
public static Document articleToDocument(Article article) { Document document = new Document(); Field idField = new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED); Field titleField = new Field("title", article.getTitle(), Store.YES, Index.ANALYZED); Field contentField = new Field("content", article.getContent(), Store.YES, Index.ANALYZED); document.add(idField); document.add(titleField); document.add(contentField); return document; } /** * 經過document 得到article對象 * @param document * @return
*/
public static Article documentToArticle(Document document) { Article article = new Article(); article.setId(Long.parseLong(document.get("id"))); article.setTitle((document.get("title"))); article.setContent(document.get("content")); return article; } }
實用工具類實現簡單的增刪改查功能
package com.kite.luncene.index; import java.util.ArrayList; import java.util.List; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriter.MaxFieldLength; 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 org.junit.Test; import com.kite.bean.Article; import com.kite.luncene.utils.DocumentUtils; import com.kite.luncene.utils.LunceneUtils; public class ArticleIndex { @Test public void testCreateIndex() throws Exception { IndexWriter indexWriter = new IndexWriter(LunceneUtils.directory, LunceneUtils.analyzer, MaxFieldLength.LIMITED); Article article = new Article(); article.setId(1L); article.setTitle("luncenes是一個好難寫的東西"); article.setContent("百度,谷歌是很好的搜索引擎"); //經過工具類轉換成document
Document document = DocumentUtils.articleToDocument(article); indexWriter.addDocument(document); indexWriter.close(); } @Test public void testSearchIndex() throws Exception { IndexSearcher indexSearcher = new IndexSearcher(LunceneUtils.directory); /** * Version.LUCENE_30 版本 * "title" 根據那個字段 * LunceneUtils.analyzer 分詞器 */ QueryParser queryParser = new QueryParser(Version.LUCENE_30, "title", LunceneUtils.analyzer); //luncene 關鍵字
Query query = queryParser.parse("luncene"); TopDocs topDocs = indexSearcher.search(query, 2); //得到根據關鍵字查詢到的全部的記錄數
int count = topDocs.totalHits; ScoreDoc[] scoreDocs = topDocs.scoreDocs; List<Article> articles = new ArrayList<Article>(); for(ScoreDoc scoreDoc : scoreDocs) { //scoreDoc.score 得到關鍵字得分
float score = scoreDoc.score; //scoreDoc.doc 得到索引的下標
int index = scoreDoc.doc; //經過索引的下標進行查詢
Document document = indexSearcher.doc(index); Article article = DocumentUtils.documentToArticle(document); articles.add(article); } //遍歷輸出
for(Article article : articles) { System.out.println(article.getId().toString()); System.err.println(article.getTitle()); System.out.println(article.getContent()); } } /** * 修改是先刪除 而後進行添加 * @throws Exception */ @Test public void testDeleteIndex() throws Exception { IndexWriter indexWriter = new IndexWriter(LunceneUtils.directory, LunceneUtils.analyzer, MaxFieldLength.LIMITED); //indexWriter.deleteAll();--刪除全部
/* * term 關鍵字對象 * title 字段名 * luncenes 關鍵字的內容 */ Term term = new Term("title", "luncenes"); //根據關鍵字進行刪除 會在文件夾中增長一個.del結尾的文件
indexWriter.deleteDocuments(term); indexWriter.close(); } @Test public void testUpdateIndex() throws Exception { IndexWriter indexWriter = new IndexWriter(LunceneUtils.directory, LunceneUtils.analyzer, MaxFieldLength.LIMITED); Term term = new Term("title","luncenes"); Article article = new Article(); article.setId(1L); article.setTitle("luncene是一個好難寫的東西,少個s不解釋"); article.setContent("百度,谷歌是很好的搜索引擎"); /* * term 根據關鍵字進行修改 * doc 修改後的內容 */ indexWriter.updateDocument(term, DocumentUtils.articleToDocument(article)); indexWriter.close(); } }