2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程
1??Lucen目錄介紹
2? lucene-core-3.6.2.jar是lucene開發核心jar包
?? contrib? 目錄存放,包含一些擴展jar包
3? 案例
建立第一個Lucene項目:lucene3_day1
?? (1)需要先將數據轉換成為Document對象,每一個數據信息轉換成為Field(String name, String value, Field.Store store, Field.Indexindex)
?? (2)指定索引庫位置Directorydirectory = FSDirectory.open(new File("index"));// 當前Index目錄
?? (3)分詞器Analyzeranalyzer = new StandardAnalyzer(Version.LUCENE_36);
?? (4)寫入索引:
| IndexWriterConfig indexWriterConfig = new IndexWriterConfig( ??????????? Version.LUCENE_36, analyzer); IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig); ????? //將document數據寫入索引庫 indexWriter.addDocument(document); //關閉索引 indexWriter.close(); |
案例編寫:
| 案例目錄:
|
| Article.java |
| package cn.toto.lucene.quickstart; ? public class Article { ?? private int id; ?? private String title; ?? private String content; ?? /** ?? ?* @return the id ?? ?*/ ?? public int getId() { ????? return id; ?? } ?? /** ?? ?* @param id the id to set ?? ?*/ ?? public void setId(int id) { ????? this.id = id; ?? } ?? /** ?? ?* @return the title ?? ?*/ ?? public String getTitle() { ????? return title; ?? } ?? /** ?? ?* @param title the title to set ?? ?*/ ?? public void setTitle(String title) { ????? this.title = title; ?? } ?? /** ?? ?* @return the content ?? ?*/ ?? public String getContent() { ????? return content; ?? } ?? /** ?? ?* @param content the content to set ?? ?*/ ?? public void setContent(String content) { ????? this.content = content; ?? } } |
| package cn.toto.lucene.quickstart; ? import java.io.File; ? import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; 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 org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; ? /** ?* @brief LuceneTest.java 測試Lucene的案例 ?* @attention ?* @author toto-pc ?* @date 2014-12-7 ?* @note begin modify by 涂作權 2014/12/07 null ?*/ public class LuceneTest { ?? @Test ?? public void buildIndex() throws Exception { ????? Article article = new Article(); ????? article.setId(100); ????? article.setTitle("Lucene快速入門"); ????? article.setContent("Lucene是提供了一個簡單卻強大的應用程式接口," ??????????? + "能夠做全文檢索索引和搜尋,在Java開發環境里Lucene是" + ??????????? "一個成熟的免費的開放源代碼工具。"); ? ????? // 將索引數據轉換成為Document對象(Lucene要求) ????? Document document = new Document(); ????? document.add(new Field("id", // 字段 ??????????? article.getId() + "", Store.YES, // 是否建立索引 ??????????? Index.ANALYZED // 表示使用分詞索引 ????? )); ????? document.add(new Field("title", article.getTitle(), Store.YES,Index.ANALYZED)); ????? document.add(new Field("content", article.getContent(), Store.YES, Index.ANALYZED)); ? ????? // 建立索引庫 ????? // 索引目錄位置 ????? Directory directory = FSDirectory.open(new File("index"));// 當前Index目錄 ????? // 分詞器 ????? Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); ????? // 寫入索引 ????? IndexWriterConfig indexWriterConfig = new IndexWriterConfig( ??????????? Version.LUCENE_36, analyzer); ????? IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); ? ????? // 將document數據寫入索引庫 ????? indexWriter.addDocument(document); ????? // 關閉索引 ????? indexWriter.close(); ?? } } |
| 運行單元測試后的結果:
運行后index目錄下的結果:
|
4 ?可以通過luke工具查看索引庫中內容(它是一個jar包)
下載網址:http://code.google.com/p/luke/
打開方式:
如果用這種方式打不可以,可以用命令的方式打開文件,進入這個目錄,選中Shift+鼠標右鍵—>此處打開命令窗口—>輸入命令:java -jar lukeall-3.5.0.jar
工具的截圖如下:
點擊OK后的結果:
通過overview可以查看到索引信息,通過Document可以查看文檔對象信息
5? 查找
| 和上面的并集的query代碼如下: |
| @Test public void searchIndex() throws Exception { ?? //建立Query對象--根據標題 ?? String queryString = "Lucene"; ?? //第一個參數,版本號 ?? //第二個參數,字段 ?? //第三個參數,分詞器 ?? Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); ?? QueryParser queryParser = new QueryParser(Version.LUCENE_36,"title",analyzer); ??? Query query = queryParser.parse(queryString); ?? ??? ??? //根據Query查找 ??? // 索引目錄位置 ?? Directory directory = FSDirectory.open(new File("index")); ??? IndexSearcher indexSearcher = new IndexSearcher(IndexReader.open(directory)); ?? //查詢滿足結果的前100條數據 ?? TopDocs topDocs = indexSearcher.search(query, 100); ??? System.out.println("滿足結果記錄條數:" + topDocs.totalHits); ?? ??? ??? //獲取結果 ??? ScoreDoc[] scoreDocs = topDocs.scoreDocs; ??? for (int i = 0; i < scoreDocs.length; i++) { ????? //先獲得Document下標 ??? ?? int docID = scoreDocs[i].doc; ??? ?? Document document = indexSearcher.doc(docID); ??? ?? System.out.println("id:" + document.get("id")); ??? ?? System.out.println("title:" + document.get("title")); ??? ?? System.out.println("content:" + document.get("content")); ?? } ? ??? indexSearcher.close(); } |
| 運行結果:
|
?
?Luke查看的索引庫內容:
索引庫中信息,包括兩大部分:
A 索引詞條信息
B 文檔對象信息
?每個Field中都存在一個Store和一個Index
?索引內容和Document內容有什么關系
查找時,通過索引內容? 查找? 文檔對象信息
?
索引的查找過程
?
總結
以上是生活随笔為你收集整理的2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梭子蟹死了能不能吃了,梭子蟹怎么保存?
- 下一篇: 3.Lucene3.x API分析,Di