Lucene 的基本原理與《 全文檢索的基本原理 》是差不多的。
?
Lucene 的源碼主要有 7 個(gè)子包,每個(gè)包完成特定的功能:
?
包名 |
功能描述 |
org.apache.lucene.analysis |
語(yǔ)言分析器,主要用于的切詞,支持中文主要是擴(kuò)展此類 |
org.apache.lucene.document |
索引存儲(chǔ)時(shí)的文檔結(jié)構(gòu)管理,類似于關(guān)系型數(shù)據(jù)庫(kù)的表結(jié)構(gòu) |
org.apache.lucene.index |
索引管理,包括索引建立、刪除等 |
org.apache.lucene.queryParser |
查詢分析器,實(shí)現(xiàn)查詢關(guān)鍵詞間的運(yùn)算,如與、或、非等 |
org.apache.lucene.search |
檢索管理,根據(jù)查詢條件,檢索得到結(jié)果 |
org.apache.lucene.store |
數(shù)據(jù)存儲(chǔ)管理,主要包括一些底層的 I/O 操作 |
org.apache.lucene.util |
一些公用類 |
?
?
? ??? 另外: Lucene 3.0 還有一個(gè) org.apache.lucene.messages 包,這個(gè)包增加了本地語(yǔ)言支持 NLS 和軟件系統(tǒng)國(guó)際化。
?
?
?
???? 上面的圖可以很明顯的看出 Lucene 的兩大主要的功能: 建立索引 ( 紅色箭頭: Index), 檢索索引 ( 藍(lán)色箭頭: Search) 。
- analysis 模塊主要負(fù)責(zé)詞法分析及語(yǔ)言處理而形成 Term( 詞 ) 。 具體參見文章《 Lucene分析器—Analyzer 》
- index 模塊主要負(fù)責(zé)索引的創(chuàng)建,里面有 IndexWriter 。
- store 模塊主要負(fù)責(zé)索引的讀寫。
- queryParser 主要負(fù)責(zé)語(yǔ)法分析。
- search 模塊主要負(fù)責(zé)對(duì)索引的搜索 ( 其中 similarity 就是相關(guān)性打分 ) 。
講到這里基本上對(duì)全文檢索工具包Lucene的原理和結(jié)構(gòu)已經(jīng)有了大致的了解了,下面給出Lucene3.0.1建立索引和檢索索引的基本代碼,關(guān)于Lucene的細(xì)節(jié)探討將在后續(xù)文章中展開。
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:\\實(shí)驗(yàn)\\index"); File docDir = new File("e:\\實(shí)驗(yàn)\\content"); try { //索引器 IndexWriter standardWriter = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); //不建立復(fù)合式索引文件,默認(rèn)的情況下是復(fù)合式的索引文件 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)); //文件內(nèi)容,需要檢索 doc.add(new Field("content", new FileReader(file))); standardWriter.addDocument(doc); } } standardWriter.optimize(); //關(guān)閉索引器 ?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:\\實(shí)驗(yàn)\\index"; String field = "content"; String queries = null; boolean raw = false; // 要顯示條數(shù) 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"); } } } }
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
