Lucene 的基本原理與《 全文檢索的基本原理 》是差不多的。
?
Lucene 的源碼主要有 7 個子包,每個包完成特定的功能:
?
|
包名 |
功能描述 |
|
org.apache.lucene.analysis |
語言分析器,主要用于的切詞,支持中文主要是擴展此類 |
|
org.apache.lucene.document |
索引存儲時的文檔結構管理,類似于關系型數據庫的表結構 |
|
org.apache.lucene.index |
索引管理,包括索引建立、刪除等 |
|
org.apache.lucene.queryParser |
查詢分析器,實現查詢關鍵詞間的運算,如與、或、非等 |
|
org.apache.lucene.search |
檢索管理,根據查詢條件,檢索得到結果 |
|
org.apache.lucene.store |
數據存儲管理,主要包括一些底層的 I/O 操作 |
|
org.apache.lucene.util |
一些公用類 |
?
?
? ??? 另外: Lucene 3.0 還有一個 org.apache.lucene.messages 包,這個包增加了本地語言支持 NLS 和軟件系統國際化。
?
?
?
???? 上面的圖可以很明顯的看出 Lucene 的兩大主要的功能: 建立索引 ( 紅色箭頭: Index), 檢索索引 ( 藍色箭頭: Search) 。
- analysis 模塊主要負責詞法分析及語言處理而形成 Term( 詞 ) 。 具體參見文章《 Lucene分析器—Analyzer 》
- index 模塊主要負責索引的創建,里面有 IndexWriter 。
- store 模塊主要負責索引的讀寫。
- queryParser 主要負責語法分析。
- search 模塊主要負責對索引的搜索 ( 其中 similarity 就是相關性打分 ) 。
講到這里基本上對全文檢索工具包Lucene的原理和結構已經有了大致的了解了,下面給出Lucene3.0.1建立索引和檢索索引的基本代碼,關于Lucene的細節探討將在后續文章中展開。
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class IndexFiles {
// 主要代碼 索引docDir文件夾下文檔,索引文件在INDEX_DIR文件夾中
public static void main(String[] args) {
File indexDir=new File("e:\\實驗\\index");
File docDir = new File("e:\\實驗\\content");
try {
//索引器
IndexWriter standardWriter = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
//不建立復合式索引文件,默認的情況下是復合式的索引文件
standardWriter.setUseCompoundFile(false);
String[] files = docDir.list();
for (String fileStr : files) {
File file = new File(docDir, fileStr);
if (!file.isDirectory()) {
Document doc = new Document();
//文件名稱,可查詢,不分詞
String fileName=file.getName().substring(0,file.getName().indexOf("."));
doc.add(new Field("name",fileName, Field.Store.YES, Field.Index.NOT_ANALYZED));
//文件路徑,可查詢,不分詞
String filePath=file.getPath();
doc.add(new Field("path", filePath, Field.Store.YES, Field.Index.NOT_ANALYZED));
//文件內容,需要檢索
doc.add(new Field("content", new FileReader(file)));
standardWriter.addDocument(doc);
}
}
standardWriter.optimize();
//關閉索引器
?standardWriter.close();
} catch (IOException e) {
System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
}
}
}
?
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
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.index.IndexReader;
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.Searcher;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
/**
* 檢索索引
*/
public class SearchFiles {
/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
String index = "E:\\實驗\\index";
String field = "content";
String queries = null;
boolean raw = false;
// 要顯示條數
int hitsPerPage = 10;
// searching, so read-only=true
IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)), true); // only
Searcher searcher = new IndexSearcher(reader);
Analyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,field, standardAnalyzer);
while (true) {
if (queries == null) // prompt the user
System.out.println("Enter query: ");
String line = in.readLine();
if (line == null || line.length() == -1)
break;
line = line.trim();
if (line.length() == 0)
break;
Query query = parser.parse(line);
System.out.println("Searching for: " + query.toString(field));
doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null);
}
reader.close();
}
public static void doPagingSearch(BufferedReader in, Searcher searcher, Query query, int hitsPerPage, boolean raw,
boolean interactive) throws IOException {
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, false);
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
int end, numTotalHits = collector.getTotalHits();
System.out.println(numTotalHits + " total matching documents");
int start = 0;
end = Math.min(hits.length, start + hitsPerPage);
for (int i = start; i < end; i++) {
Document doc = searcher.doc(hits[i].doc);
String path = doc.get("path");
if (path != null) {
System.out.println((i + 1) + ". " + path);
} else {
System.out.println((i + 1) + ". " + "No path for this document");
}
}
}
}
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

