1. 程式人生 > >4.Lucene3.案例介紹,建立索引,查詢等操作驗證

4.Lucene3.案例介紹,建立索引,查詢等操作驗證


  1. 案例:

Article.java

package cn.toto.lucene.quickstart;

publicclassArticle {

privateintid;

private Stringtitle;

private Stringcontent;

/**

* @return the id

*/

publicint getId() {

returnid;

}

/**

* @param id the id to set

*/

publicvoid setId(int id) {

this.id = id;

}

/**

*

@return the title

*/

public String getTitle() {

returntitle;

}

/**

* @param title the title to set

*/

publicvoid setTitle(String title) {

this.title = title;

}

/**

* @return the content

*/

public String getContent() {

returncontent;

}

/**

* @param content the content to set

*/

public

void setContent(String content) {

this.content = content;

}

}

工具類Configuration.java

package cn.toto.lucene.util;

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;

/**

* @brief Configuration.java配置物件,提供Lucene需要索引目錄,分詞器

* @attention

* @author toto

* @date 2014-12-7

* @note begin modify by塗作權

*/

public class Configuration {

private static Directory directory;

private static Analyzer analyzer;

private static Version version;

static {

try {

//設定磁碟目錄,表示的是本地index目錄

directory = FSDirectory.open(new File("index"));

} catch (Exception e) {

e.printStackTrace();

}

//表示LUCENE版本

version = Version.LUCENE_36;

//表示使用版本

analyzer = new StandardAnalyzer(version);

}

//提供目錄

public static Directory getDirectory()

{

return directory;

}

//提供分詞器

public static Analyzer getAnalyzer()

{

return analyzer;

}

//獲取版本

public static Version getVersion()

{

return version;

}

}

工具類LuceneUtils.java

package cn.toto.lucene.util;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.search.IndexSearcher;

// lucene工具類

public class LuceneUtils {

private static IndexWriter indexWriter;

static {

// 索引目錄位置

try {

// 寫入索引

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(

Configuration.getVersion(), Configuration.getAnalyzer());

indexWriter = new IndexWriter(Configuration.getDirectory(),

indexWriterConfig);

// 繫結虛擬機器退出事件,關閉IndexWriter

Runtime.getRuntime().addShutdownHook(new Thread() {

@Override

public void run() {

try {

indexWriter.close();

} catch (CorruptIndexException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

});

} catch (IOException e) {

e.printStackTrace();

}

}

// 提供獲取IndexWriter物件

public static IndexWriter getIndexWriter() {

return indexWriter;

}

// 獲得查詢IndexSeacher物件

public static IndexSearcher getIndexSearcher() throws Exception {

return new IndexSearcher(IndexReader.open(Configuration.getDirectory()));

}

}

LuceneTest.java

package cn.toto.lucene.api;

import java.io.File;

importjava.io.IOException;

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.Store;

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

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.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.store.LockObtainFailedException;

import org.apache.lucene.store.RAMDirectory;

import org.apache.lucene.util.Version;

import org.junit.Test;

import cn.toto.lucene.quickstart.Article;

import cn.toto.lucene.util.LuceneUtils;

// API詳細分析

publicclass LuceneTest {

@Test

//使用LuceneUtils解決 IndexWriter併發問題

@SuppressWarnings("unused")

publicvoid testLock2() {

IndexWriter indexWriter2 = LuceneUtils.getIndexWriter();

IndexWriter indexWriter1 = LuceneUtils.getIndexWriter();

}

@Test

@SuppressWarnings("all")

//使用兩個IndexWrtier報錯,鎖使用問題

publicvoid testLock()throws CorruptIndexException,

LockObtainFailedException, IOException {

//索引目錄位置

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

IndexWriterConfig indexWriterConfig2 = new IndexWriterConfig(

Version.LUCENE_36, analyzer);

IndexWriter indexWriter2 = new IndexWriter(directory,

indexWriterConfig2);

}

上面的執行結果如下:

@Test

//測試Store Index

/*

* Store.YES 儲存Store.NO不儲存 Index.NO不建立索引 Index.ANALYZED分詞建立索引

* Index.NOT_ANALYZED 不分詞建立索引Index.ANALYZED_NO_NORMS 分詞建立索引,不存放權重資訊

* Index.NOT_ANALYZED_NO_NORMS 不分詞建立索引,不存放權重資訊

*/

publicvoid testIndex()throws Exception {

//需要建立索引目標資料

Article article = new Article();

article.setId(100);

article.setTitle("學習全文檢索");

article.setContent("lucene是搜尋引擎開發技術lucene並不是一個現成的產品,Apache提供");

//將索引資料轉換 Document物件lucene要求)

Document document = new Document();

document.add(new Field("id", article.getId() + "", Store.YES,

Field.Index.NOT_ANALYZED));//對於id通常不分詞

document.add(new Field("title", article.getTitle(), Store.YES,

Field.Index.ANALYZED_NO_NORMS));

document.add(new Field("content", article.getContent(), Store.YES,

Field.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();

}

上面的單元測試的結果

@Test

//查詢索引庫 ,檢視norms效果

publicvoid testQuery()throws Exception {

//建立Query物件 ---- 根據標題

String queryStrng = "檢索";

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);