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