日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

發(fā)布時間:2024/9/27 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


1??Lucen目錄介紹

2? lucene-core-3.6.2.jarlucene開發(fā)核心jar

?? contrib? 目錄存放,包含一些擴展jar

3? 案例

建立第一個Lucene項目:lucene3_day1

?? 1)需要先將數(shù)據(jù)轉(zhuǎn)換成為Document對象,每一個數(shù)據(jù)信息轉(zhuǎn)換成為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數(shù)據(jù)寫入索引庫

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 測試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是提供了一個簡單卻強大的應(yīng)用程式接口,"

??????????? + "能夠做全文檢索索引和搜尋,在Java開發(fā)環(huán)境里Lucene" +

??????????? "一個成熟的免費的開放源代碼工具。");

?

????? // 將索引數(shù)據(jù)轉(zhuǎn)換成為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數(shù)據(jù)寫入索引庫

????? indexWriter.addDocument(document);

????? // 關(guān)閉索引

????? indexWriter.close();

?? }

}

運行單元測試后的結(jié)果:

運行后index目錄下的結(jié)果:

4 ?可以通過luke工具查看索引庫中內(nèi)容(它是一個jar包)

下載網(wǎng)址:http://code.google.com/p/luke/

打開方式:

如果用這種方式打不可以,可以用命令的方式打開文件,進入這個目錄,選中Shift+鼠標右鍵—>此處打開命令窗口—>輸入命令:java -jar lukeall-3.5.0.jar

工具的截圖如下:

點擊OK后的結(jié)果:

通過overview可以查看到索引信息,通過Document可以查看文檔對象信息

5? 查找

和上面的并集的query代碼如下:

@Test

public void searchIndex() throws Exception

{

?? //建立Query對象--根據(jù)標題

?? String queryString = "Lucene";

?? //第一個參數(shù),版本號

?? //第二個參數(shù),字段

?? //第三個參數(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下標

??? ?? 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();

}

運行結(jié)果:

?

  • ?Luke查看的索引庫內(nèi)容:

  • 索引庫中信息,包括兩大部分:

    A 索引詞條信息

    B 文檔對象信息

  • ?每個Field中都存在一個Store和一個Index

  • ?索引內(nèi)容和Document內(nèi)容有什么關(guān)系

  • 查找時,通過索引內(nèi)容? 查找? 文檔對象信息

    ?

  • 索引的查找過程

  • ?

    總結(jié)

    以上是生活随笔為你收集整理的2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。