Lucene是apache軟體基金會4 jakarta項目組的一個子項目,是一個開放原始碼的全文檢索索引引擎工具包,即它不是一個完整的全文檢索索引引擎,而是一個全文檢索索引引擎的架構,提供了完整的查詢引擎和索引引擎,部分文本分析引擎(英文與德文兩種西方語言)。Lucene的目的是為軟體開發人員提供一個簡單易用的工具包,以方便的在目標系統中實現全文檢索索引的功能,或者是以此為基礎建立起完整的全文檢索索引引擎。
以下類比一個簡單的Lucene入門案例
接下來為Lucene開發步驟:
1、建立一個java工程,匯入Lucene所需jar,如
目錄結構
其中:luceneds為資料來源儲存位置,luceneindex存放索引檔案的位置,即索引庫。如果索引庫已被建立,那麼luceneindex目錄下會有索引檔案,如:
代碼
package com.ljq.lucene;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
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.NumberTools;
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.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
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.junit.Test;
/**
* 開發lucene步驟:先建立索引,再搜尋
*
* @author jiqinlin
*
*/
public class HelloWorld {
// 資料來源路徑
String dspath = "F:\\android\\luceneprj\\luceneds\\IndexWriter addDocument's a javadoc .txt";
//存放索引檔案的位置,即索引庫
String indexpath = "F:\\android\\luceneprj\\luceneindex";
//分詞器
Analyzer analyzer = new StandardAnalyzer();
/**
* 建立索引
*
* IndexWriter 用來操作(增、刪、改)索引庫的
*/
@Test
public void createIndex() throws Exception {
File file = new File(dspath);
//Document存放經過組織後的資料來源,只有轉換為Document對象才可以被索引和搜尋到
Document doc = new Document();
//檔案名稱
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
//檢索到的內容
doc.add(new Field("content", readFileContent(file), Store.YES, Index.ANALYZED));
//檔案大小
doc.add(new Field("size", NumberTools.longToString(file.length()),
Store.YES, Index.NOT_ANALYZED));
//檢索到的檔案位置
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED));
// 建立索引
IndexWriter indexWriter = new IndexWriter(indexpath, analyzer, true,
MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}
/**
* 搜尋
*
* IndexSearcher 用來在索引庫中進行查詢
*/
@Test
public void search() throws Exception {
//請求欄位
//String queryString = "document";
String queryString = "adddocument";
// 1,把要搜尋的文本解析為 Query
String[] fields = { "name", "content" };
QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = queryParser.parse(queryString);
// 2,進行查詢,從索引庫中尋找
IndexSearcher indexSearcher = new IndexSearcher(indexpath);
Filter filter = null;
TopDocs topDocs = indexSearcher.search(query, filter, 10000);
System.out.println("總共有【" + topDocs.totalHits + "】條匹配結果");
// 3,列印結果
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
// 文檔內部編號
int index = scoreDoc.doc;
// 根據編號取出相應的文檔
Document doc = indexSearcher.doc(index);
System.out.println("------------------------------");
System.out.println("name = " + doc.get("name"));
System.out.println("content = " + doc.get("content"));
System.out.println("size = " + NumberTools.stringToLong(doc.get("size")));
System.out.println("path = " + doc.get("path"));
}
}
/**
* 讀取檔案內容
*/
public static String readFileContent(File file) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer content = new StringBuffer();
for (String line = null; (line = reader.readLine()) != null;) {
content.append(line).append("\n");
}
reader.close();
return content.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
運行結果如下: