lucene 簡單實用

 1 package com.kite.luncene.helloworld;  2 
 3 import java.io.File;  4 import java.util.ArrayList;  5 import java.util.List;  6 
 7 import org.apache.lucene.analysis.Analyzer;  8 import org.apache.lucene.analysis.standard.StandardAnalyzer;  9 import org.apache.lucene.document.Document;  10 import org.apache.lucene.document.Field;  11 import org.apache.lucene.document.Field.Index;  12 import org.apache.lucene.document.Field.Store;  13 import org.apache.lucene.index.IndexWriter;  14 import org.apache.lucene.index.IndexWriter.MaxFieldLength;  15 import org.apache.lucene.queryParser.QueryParser;  16 import org.apache.lucene.search.IndexSearcher;  17 import org.apache.lucene.search.Query;  18 import org.apache.lucene.search.ScoreDoc;  19 import org.apache.lucene.search.TopDocs;  20 import org.apache.lucene.store.Directory;  21 import org.apache.lucene.store.FSDirectory;  22 import org.apache.lucene.util.Version;  23 import org.junit.Test;  24 
 25 import com.kite.bean.Article;  26 
 27 public class HelloWorld  28 {  29  @Test  30     public void testCreate() throws Exception  31  {  32         /**
 33  * 一、建立一個article對象,而且把信息存放進去  34  * 二、調用indexWriter的API把數據存放在索引庫中  35  * 三、關閉indexWriter  36          */
 37         //建立一個article對象,而且把信息存放進去
 38         Article article = new Article();  39         article.setId(1L);  40         article.setTitle("java goodnice");  41         article .setContent("多年來就是這麼吊");  42         
 43         //二、調用indexWriter的API把數據存放在索引庫中
 44          /**
 45  * 建立一個IndexWriter  46  * 參數三個  47  * 一、索引庫 指向索引庫的位置  48  * 二、分詞器  49             */
 50             //建立索引庫
 51             Directory directory = FSDirectory.open(new File("./indexDir"));  52             //建立分詞器
 53             Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);  54         IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);  55         
 56         //把一個對象轉換成document
 57         Document document = new Document();  58         Field idField = new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED);  59         Field titleField = new Field("title", article.getTitle(), Store.YES, Index.ANALYZED);  60         Field contentField = new Field("content", article.getContent(), Store.YES, Index.ANALYZED);  61  document.add(idField);  62  document.add(titleField);  63  document.add(contentField);  64  indexWriter.addDocument(document);  65         
 66         //三、關閉indexWriter
 67  indexWriter.close();  68  }  69     
 70  @Test  71     public  void testSearchIndex() throws Exception  72  {  73         /**
 74  * 1.建立一個 indexSerach對象  75  * 2.調用search方法進行檢索  76  * 3.輸出內容  77          */
 78         
 79         // 1.建立一個 indexSerach對象  80         //--索引庫
 81         Directory directory = FSDirectory.open(new File("./indexDir"));  82         IndexSearcher searcher = new IndexSearcher(directory);  83         //2..調用search方法進行檢索  84         //--
 85         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);  86         QueryParser queryParser = new QueryParser(Version.LUCENE_30, "id", analyzer);  87         //設置搜索的關鍵字
 88         Query query = queryParser.parse("1");  89         TopDocs topDocs = searcher.search(query, 10);  90         //得到根據關鍵字查詢出來的總的記錄數
 91         int count = topDocs.totalHits;  92         List<Article> articles = new ArrayList<Article>();  93         //得到數組
 94         ScoreDoc[] scoreDocs = topDocs.scoreDocs;  95         for(ScoreDoc scoreDoc : scoreDocs)  96  {  97             //關鍵字得分
 98             float score = scoreDoc.score;  99             //索引的下標
100             int index = scoreDoc.doc; 101             //根據索引得到document對象
102             Document document = searcher.doc(index); 103             //把document轉化成article
104             Article article = new Article(); 105             article.setId(Long.parseLong(document.get("id"))); 106             article.setTitle(document.get("title")); 107             article.setContent(document.get("content")); 108  articles.add(article); 109  } 110         for(Article article : articles) 111  { 112  System.out.println(article.getId()); 113  System.out.println(article.getTitle()); 114  System.out.println(article.getContent()); 115  } 116  } 117 }


兩個工具類
package com.kite.luncene.utils; import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class LunceneUtils { //索引庫
    public static Directory directory; //分詞器
    public static Analyzer analyzer; static { try { directory = FSDirectory.open(new File("./indexDor")); analyzer = new StandardAnalyzer(Version.LUCENE_30); } catch (Exception e) { e.printStackTrace(); } } }

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(); } }
相關文章
相關標籤/搜索