本文將探討 Apache Lucene —— 性能卓越、功能全面的文本搜索引擎庫。我們將學(xué)習(xí) Lucene 架構(gòu)及其核心 API。學(xué)習(xí)如何使用 Lucene 進(jìn)行跨平臺全文本搜索、建立索引、顯示結(jié)果,以及如何擴(kuò)展搜索。
簡介
Lucene 是一個開源、高度可擴(kuò)展的搜索引擎庫,可以從 Apache Software Foundation 獲取。您可以將 Lucene 用于商業(yè)和開源應(yīng)用程序。Lucene 強(qiáng)大的 API 主要關(guān)注文本索引和搜索。它可以用于為各種應(yīng)用程序構(gòu)建搜索功能,比如電子郵件客戶端、郵件列表、Web 搜索、數(shù)據(jù)庫搜索等等。Wikipedia、TheServerSide、jGuru 和 LinkedIn 等網(wǎng)站都使用了 Lucene。
Lucene 還為 Eclipse IDE、Nutch(著名的開源 Web 搜索引擎)以及 IBM®、AOL 和 Hewlett-Packard 等公司提供搜索功能。Lucene 已經(jīng)兼容許多其他編程語言,包括 Perl、Python、C++ 和 .NET。到 2009 年 7 月 30 日止,用于 Java™ 編程語言的最新版 Lucene 為 V2.4.1。
Lucene 功能眾多:
- 擁有強(qiáng)大、準(zhǔn)確、有效的搜索算法。
- 計算每個文檔匹配給定查詢的分?jǐn)?shù),并根據(jù)分?jǐn)?shù)返回最相關(guān)的文檔。
- 支持許多強(qiáng)大的查詢類型,比如 PhraseQuery、WildcardQuery、RangeQuery、FuzzyQuery、BooleanQuery 等。
- 支持解析人們輸入的豐富查詢表達(dá)式。
- 允許用戶使用定制排序、過濾和查詢表達(dá)式解析擴(kuò)展搜索行為。
- 使用基于文件的鎖定機(jī)制保護(hù)并發(fā)索引修改。
- 允許同時搜索和編制索引。
使用 Lucene 構(gòu)建應(yīng)用程序
如圖 1 所示,使用 Lucene 構(gòu)建功能全面的搜索應(yīng)用程序主要涉及編制數(shù)據(jù)索引、搜索數(shù)據(jù)和顯示搜索結(jié)果幾個方面。
圖 1. 使用 Lucene 構(gòu)建應(yīng)用程序的步驟

本文從使用 Lucene V2.4.1 和 Java 技術(shù)開發(fā)的樣例應(yīng)用程序中挑選了一些代碼片段。示例應(yīng)用程序為存儲在屬性文件中一組電子郵件文檔編制索引,并展示了如何使用 Lucene 的查詢 API 搜索索引。該示例還讓您熟悉基本的索引操作。
為數(shù)據(jù)編制索引
Lucene 允許您為任何文本格式的數(shù)據(jù)編制索引。Lucene 可以用于幾乎任何數(shù)據(jù)源以及從中提取的文本信息。您可以使用 Lucene 編制索引并搜索 HTML 文檔、Microsoft® Word 文檔、PDF 文件中存儲的數(shù)據(jù)。編制數(shù)據(jù)索引的第一步是讓數(shù)據(jù)變成一個簡單的文本格式。您可以使用定制解析器和數(shù)據(jù)轉(zhuǎn)換器實現(xiàn)這一點。
編制索引的過程
編制索引 是將文本數(shù)據(jù)轉(zhuǎn)換為有利于快速搜索的格式。這類似于書本后面的索引:為您指出主題在書中出現(xiàn)的位置。
Lucene 將輸入數(shù)據(jù)存儲在名為逆序 索引的數(shù)據(jù)結(jié)構(gòu)中, 該數(shù)據(jù)結(jié)構(gòu)以索引文件集的形式存儲在文件系統(tǒng)或內(nèi)存中。大部分 Web 搜索引擎都使用逆序索引。它允許用戶執(zhí)行快速關(guān)鍵字查詢,查找匹配給定查詢的文檔。在將文本數(shù)據(jù)添加到索引前,由分析程序(使用分析過程)進(jìn)行處理。
分析
分析 是將文本數(shù)據(jù)轉(zhuǎn)換為搜索基本單位(稱為項(term))的過程。在分析過程中,文本數(shù)據(jù)將經(jīng)歷多項操作:提取單詞、移除通用單詞、忽略標(biāo)點符號、將單詞變?yōu)樵~根形式、將單詞變成小寫等等。分析過程發(fā)生在編制索引和查詢解析之前。分析將文本數(shù)據(jù)轉(zhuǎn)換為標(biāo)記,這些標(biāo)記將作為項添加到 Lucene 索引中。
Lucene 有多種內(nèi)置分析程序,比如 SimpleAnalyzer、StandardAnalyzer、StopAnalyzer、SnowballAnalyzer 等。它們在標(biāo)記文本和應(yīng)用過濾器的方式上有所區(qū)別。因為分析在編制索引之前移除單詞,它減少了索引的大小,但是不利用精確的查詢過程。您可以使用 Lucene 提供的基本構(gòu)建塊創(chuàng)建定制分析程序,以自己的方式控制分析過程。表 1 展示了一些內(nèi)置分析程序及其處理數(shù)據(jù)的方式。
表 1. Lucene 的內(nèi)置分析程序
分析程序 |
分解空白處的標(biāo)記 |
WhitespaceAnalyzer |
分解非字母字符的文本,并將文本轉(zhuǎn)為小寫形式 |
SimpleAnalyzer |
分解非字母字符的文本,并將文本轉(zhuǎn)為小寫形式 |
StopAnalyzer |
移除虛字(stop word)—— 對檢索無用的字,并將文本轉(zhuǎn)為小寫形式 |
StandardAnalyzer |
根據(jù)一種復(fù)雜語法(識別電子郵件地址、縮寫、中文、日文、韓文字符、字母數(shù)字等等)標(biāo)記文本將文本轉(zhuǎn)為小寫形式移除虛字 |
核心索引編制類
| |
|
|
|
|
|
|
Directory
- 表示索引文件存儲位置的抽象類。有兩個常用的子類:
FSDirectory
— 在實際文件系統(tǒng)中存儲索引的 Directory
實現(xiàn)。該類對于大型索引非常有用。
RAMDirectory
— 在內(nèi)存中存儲所有索引的實現(xiàn)。該類適用于較小的索引,可以完整加載到內(nèi)存中,在應(yīng)用程序終止之后銷毀。由于索引保存在內(nèi)存中,所以速度相對較快。
Analyzer
- 正如上文所述,分析程序負(fù)責(zé)處理文本數(shù)據(jù)并將其轉(zhuǎn)換為標(biāo)記存儲在索引中。在編制索引前,
IndexWriter
接收用于標(biāo)記數(shù)據(jù)的分析程序。要為文本編制索引,您應(yīng)該使用適用于該文本語言的分析程序。
默認(rèn)分析程序適用于英語。在 Lucene 沙盒中還有其他分析程序,包括用于中文、日文和韓文的分析程序。
IndexDeletionPolicy
- 該接口用來實現(xiàn)從索引目錄中定制刪除過時提交的策略。默認(rèn)刪除策略是
KeepOnlyLastCommitDeletionPolicy
,該策略僅保留最近的提交,并在完成一些提交之后立即移除所有之前的提交。
IndexWriter
- 創(chuàng)建或維護(hù)索引的類。它的構(gòu)造函數(shù)接收布爾值,確定是否創(chuàng)建新索引,或者打開現(xiàn)有索引。它提供在索引中添加、刪除和更新文檔的方法。
對索引所做的更改最初緩存在內(nèi)存中,并周期性轉(zhuǎn)儲到索引目錄。IndexWriter
公開了幾個控制如何在內(nèi)存中緩存索引并寫入磁盤的字段。對索引的更改對于 IndexReader
不可見,除非調(diào)用 IndexWriter
的提交或關(guān)閉方法。IndexWriter
創(chuàng)建一個目錄鎖定文件,以通過同步索引更新保護(hù)索引不受破壞。IndexWriter
允許用戶指定可選索引刪除策略。
列表 1. 使用 Lucene IndexWriter
-
-
- Directory fsDirectory = FSDirectory.getDirectory(indexDirectory);
-
-
- Analyzer standardAnalyzer = new StandardAnalyzer();
-
- boolean create = true;
-
- IndexDeletionPolicy deletionPolicy = new KeepOnlyLastCommitDeletionPolicy();
- indexWriter =new IndexWriter(fsDirectory,standardAnalyzer,create,
- deletionPolicy,IndexWriter.MaxFieldLength.UNLIMITED);
//Create instance of Directory where index files will be stored
Directory fsDirectory = FSDirectory.getDirectory(indexDirectory);
/* Create instance of analyzer, which will be used to tokenize
the input data */
Analyzer standardAnalyzer = new StandardAnalyzer();
//Create a new index
boolean create = true;
//Create the instance of deletion policy
IndexDeletionPolicy deletionPolicy = new KeepOnlyLastCommitDeletionPolicy();
indexWriter =new IndexWriter(fsDirectory,standardAnalyzer,create,
deletionPolicy,IndexWriter.MaxFieldLength.UNLIMITED);
將數(shù)據(jù)添加到索引
將文本數(shù)據(jù)添加到索引涉及到兩個類。
Field
表示搜索中查詢或檢索的數(shù)據(jù)片。Field
類封裝一個字段名稱及其值。Lucene 提供了一些選項來指定字段是否需要編制索引或分析,以及值是否需要存儲。這些選項可以在創(chuàng)建字段實例時傳遞。下表展示了 Field
元數(shù)據(jù)選項的詳細(xì)信息。
表 2. Field
元數(shù)據(jù)選項的詳細(xì)信息
選項 |
描述 |
Field.Store.Yes |
用于存儲字段值。適用于顯示搜索結(jié)果的字段 — 例如,文件路徑和 URL。 |
Field.Store.No |
沒有存儲字段值 — 例如,電子郵件消息正文。 |
Field.Index.No |
適用于未搜索的字段 — 僅用于存儲字段,比如文件路徑。 |
Field.Index.ANALYZED |
用于字段索引和分析 — 例如,電子郵件消息正文和標(biāo)題。 |
Field.Index.NOT_ANALYZED |
用于編制索引但不分析的字段。它在整體中保留字段的原值 — 例如,日期和個人名稱。 |
Document
是一個字段集合。Lucene 也支持推進(jìn)文檔和字段,這在給某些索引數(shù)據(jù)賦予重要性時非常有用。給文本文件編制索引包括將文本數(shù)據(jù)封裝在字段中、創(chuàng)建文檔、填充字段,使用 IndexWriter
向索引添加文檔。
列表 2 展示向索引添加數(shù)據(jù)的示例。
列表 2. 向索引添加數(shù)據(jù)
- *Step 1. Prepare the data for indexing. Extract the data. */
-
- String sender = properties.getProperty("sender");
- String date = properties.getProperty("date");
- String subject = properties.getProperty("subject");
- String message = properties.getProperty("message");
- String emaildoc = file.getAbsolutePath();
-
-
-
- Field senderField =
- new Field("sender",sender,Field.Store.YES,Field.Index.NOT_ANALYZED);
- Field emaildatefield =
- new Field("date",date,Field.Store.NO,Field.Index.NOT_ANALYZED);
- Field subjectField =
- new Field("subject",subject,Field.Store.YES,Field.Index.ANALYZED);
- Field messagefield =
- new Field("message",message,Field.Store.NO,Field.Index.ANALYZED);
- Field emailDocField =
- new Field("emailDoc",emaildoc,Field.Store.YES,
- Field.Index.NO);
-
- Document doc = new Document();
-
- doc.add(senderField);
- doc.add(emaildatefield);
- doc.add(subjectField);
- doc.add(messagefield);
- doc.add(emailDocField);
-
-
- indexWriter.addDocument(doc);
*Step 1. Prepare the data for indexing. Extract the data. */
String sender = properties.getProperty("sender");
String date = properties.getProperty("date");
String subject = properties.getProperty("subject");
String message = properties.getProperty("message");
String emaildoc = file.getAbsolutePath();
/* Step 2. Wrap the data in the Fields and add them to a Document */
Field senderField =
new Field("sender",sender,Field.Store.YES,Field.Index.NOT_ANALYZED);
Field emaildatefield =
new Field("date",date,Field.Store.NO,Field.Index.NOT_ANALYZED);
Field subjectField =
new Field("subject",subject,Field.Store.YES,Field.Index.ANALYZED);
Field messagefield =
new Field("message",message,Field.Store.NO,Field.Index.ANALYZED);
Field emailDocField =
new Field("emailDoc",emaildoc,Field.Store.YES,
Field.Index.NO);
Document doc = new Document();
// Add these fields to a Lucene Document
doc.add(senderField);
doc.add(emaildatefield);
doc.add(subjectField);
doc.add(messagefield);
doc.add(emailDocField);
//Step 3: Add this document to Lucene Index.
indexWriter.addDocument(doc);
搜索索引數(shù)據(jù)
搜索是在索引中查找單詞并查找包含這些單詞的文檔的過程。使用 Lucene 的搜索 API 構(gòu)建的搜索功能非常簡單明了。本小節(jié)討論 Lucene 搜索 API 的主要類。
Searcher
Searcher
是一個抽象基類,包含各種超負(fù)荷搜索方法。IndexSearcher
是一個常用的子類,允許在給定的目錄中存儲搜索索引。Search
方法返回一個根據(jù)計算分?jǐn)?shù)排序的文檔集合。Lucene 為每個匹配給定查詢的文檔計算分?jǐn)?shù)。IndexSearcher
是線程安全的;一個實例可以供多個線程并發(fā)使用。
Term
Term 是搜索的基本單位。它由兩部分組成:單詞文本和出現(xiàn)該文本的字段的名稱。Term 對象也涉及索引編制,但是可以在 Lucene 內(nèi)部創(chuàng)建。
Query 和子類
Query
是一個用于查詢的抽象基類。搜索指定單詞或詞組涉及到在項中包裝它們,將項添加到查詢對象,將查詢對象傳遞到 IndexSearcher
的搜索方法。
Lucene 包含各種類型的具體查詢實現(xiàn),比如 TermQuery、BooleanQuery、PhraseQuery、PrefixQuery、RangeQuery、MultiTermQuery、FilteredQuery、SpanQuery 等。以下部分討論 Lucene 查詢 API 的主查詢類。
TermQuery
- 搜索索引最基本的查詢類型??梢允褂脝蝹€項構(gòu)建
TermQuery
。項值應(yīng)該區(qū)分大小寫,但也并非全是如此。注意,傳遞的搜索項應(yīng)該與文檔分析得到的項一致,因為分析程序在構(gòu)建索引之前對原文本執(zhí)行許多操作。
例如,考慮電子郵件標(biāo)題 “Job openings for Java Professionals at Bangalore”。假設(shè)您使用 StandardAnalyzer
編制索引?,F(xiàn)在如果我們使用 TermQuery
搜索 “Java”,它不會返回任何內(nèi)容,因為本文本應(yīng)該已經(jīng)規(guī)范化,并通過 StandardAnalyzer
轉(zhuǎn)成小寫。如果搜索小寫單詞 “java”,它將返回所有標(biāo)題字段中包含該單詞的郵件。
列表 3. 使用 TermQuery
搜索
-
- Searcher indexSearcher = new IndexSearcher(indexDirectory);
- Term term = new Term("subject","java");
- Query termQuery = new TermQuery(term);
- TopDocs topDocs = indexSearcher.search(termQuery,10);
//Search mails having the word "java" in the subject field
Searcher indexSearcher = new IndexSearcher(indexDirectory);
Term term = new Term("subject","java");
Query termQuery = new TermQuery(term);
TopDocs topDocs = indexSearcher.search(termQuery,10);
RangeQuery
- 您可以使用
RangeQuery
在某個范圍內(nèi)搜索。索引中的所有項都以字典順序排列。Lucene 的 RangeQuery
允許用戶在某個范圍內(nèi)搜索項。該范圍可以使用起始項和最終項(包含兩端或不包含兩端均可)指定。
列表 4. 在某個范圍內(nèi)搜索
-
-
- Term begin = new Term("date","20090601");
- Term end = new Term("date","20090606");
- Query query = new RangeQuery(begin, end, true);
/* RangeQuery example:Search mails from 01/06/2009 to 6/06/2009
both inclusive */
Term begin = new Term("date","20090601");
Term end = new Term("date","20090606");
Query query = new RangeQuery(begin, end, true);
PrefixQuery
- 您可以使用
PrefixQuery
通過前綴單詞進(jìn)行搜索,該方法用于構(gòu)建一個查詢,該查詢查找包含以指定單詞前綴開始的詞匯的文檔。
列表 5. 使用 PrefixQuery
搜索
-
- PrefixQuery prefixQuery = new PrefixQuery(new Term("sender","job"));
- PrefixQuery query = new PrefixQuery(new Term("sender","job"));
//Search mails having sender field prefixed by the word 'job'
PrefixQuery prefixQuery = new PrefixQuery(new Term("sender","job"));
PrefixQuery query = new PrefixQuery(new Term("sender","job"));
BooleanQuery
- 您可以使用
BooleanQuery
組合任何數(shù)量的查詢對象,構(gòu)建強(qiáng)大的查詢。它使用 query
和一個關(guān)聯(lián)查詢的子句,指示查詢是應(yīng)該發(fā)生、必須發(fā)生還是不得發(fā)生。在 BooleanQuery
中,子句的最大數(shù)量默認(rèn)限制為 1,024。您可以調(diào)用 setMaxClauseCount
方法設(shè)置最大子句數(shù)。
列表 6. 使用 BooleanQuery
進(jìn)行搜索
-
- Query query1 = new TermQuery(new Term("subject","java"));
- Query query2 = new TermQuery(new Term("subject","bangalore"));
- BooleanQuery query = new BooleanQuery();
- query.add(query1,BooleanClause.Occur.MUST);
- query.add(query2,BooleanClause.Occur.MUST);
// Search mails have both 'java' and 'bangalore' in the subject field
Query query1 = new TermQuery(new Term("subject","java"));
Query query2 = new TermQuery(new Term("subject","bangalore"));
BooleanQuery query = new BooleanQuery();
query.add(query1,BooleanClause.Occur.MUST);
query.add(query2,BooleanClause.Occur.MUST);
PhraseQuery
- 您可以使用
PhraseQuery
進(jìn)行短語搜索。PhraseQuery
匹配包含特定單詞序列的文檔。PhraseQuery
使用索引中存儲的項的位置信息??紤]匹配的項之間的距離稱為 slop。默認(rèn)情況下,slop 的值為零,這可以通過調(diào)用 setSlop
方法進(jìn)行設(shè)置。PhraseQuery
還支持多個項短語。
列表 7. 使用 PhraseQuery
進(jìn)行搜索
-
-
- PhraseQuery query = new PhraseQuery();
- query.setSlop(1);
- query.add(new Term("subject","job"));
- query.add(new Term("subject","opening"));
- query.add(new Term("subject","j2ee"));
/* PhraseQuery example: Search mails that have phrase 'job opening j2ee'
in the subject field.*/
PhraseQuery query = new PhraseQuery();
query.setSlop(1);
query.add(new Term("subject","job"));
query.add(new Term("subject","opening"));
query.add(new Term("subject","j2ee"));
WildcardQuery
WildcardQuery
實現(xiàn)通配符搜索查詢,這允許您搜索 arch*(可以查找包含 architect、architecture 等)之類的單詞。使用兩個標(biāo)準(zhǔn)通配符:
如果使用以通配符查詢開始的模式進(jìn)行搜索,則可能會引起性能的降低,因為這需要查詢索引中的所有項以查找匹配文檔。
列表 8. 使用 WildcardQuery 進(jìn)行搜索
|
|
-
- field./
- Query query = new WildcardQuery(new Term("subject","arch*"));
//Search for 'arch*' to find e-mail messages that have word 'architect' in the subject
field./
Query query = new WildcardQuery(new Term("subject","arch*"));
FuzzyQuery
您可以使用 FuzzyQuery
搜索類似項,該類匹配類似于指定單詞的單詞。類似度測量基于 Levenshtein(編輯距離)算法進(jìn)行。在列表 9 中,FuzzyQuery
用于查找與拼錯的單詞 “admnistrtor” 最接近的項,盡管這個錯誤單詞沒有索引。
列表 9. 使用 FuzzyQuery
進(jìn)行搜索
-
-
- Query query = new FuzzyQuery(new Term("subject", "admnistrtor"));
/* Search for emails that have word similar to 'admnistrtor' in the
subject field. Note we have misspelled admnistrtor here.*/
Query query = new FuzzyQuery(new Term("subject", "admnistrtor"));
QueryParser
QueryParser
對于解析人工輸入的查詢字符非常有用。您可以使用它將用戶輸入的查詢表達(dá)式解析為 Lucene 查詢對象,這些對象可以傳遞到 IndexSearcher
的搜索方法。它可以解析豐富的查詢表達(dá)式。 QueryParser
內(nèi)部將人們輸入的查詢字符串轉(zhuǎn)換為一個具體的查詢子類。您需要使用反斜杠(\
)將 *
、?
等特殊字符進(jìn)行轉(zhuǎn)義。您可以使用運算符 AND
、OR
和 NOT
構(gòu)建文本布爾值查詢。
列表 10. 搜索人工輸入的查詢表達(dá)式
- QueryParser queryParser = new QueryParser("subject",new StandardAnalyzer());
-
- Query query = queryParser.parse("job openings AND .net AND pune");
QueryParser queryParser = new QueryParser("subject",new StandardAnalyzer());
// Search for emails that contain the words 'job openings' and '.net' and 'pune'
Query query = queryParser.parse("job openings AND .net AND pune");
顯示搜索結(jié)果
IndexSearcher
返回一組對分級搜索結(jié)果(如匹配給定查詢的文檔)的引用。您可以使用 IndexSearcher
的搜索方法確定需要檢索的最優(yōu)先搜索結(jié)果數(shù)量??梢栽诖嘶A(chǔ)上構(gòu)建定制分頁。您可以添加定制 Web 應(yīng)用程序或桌面應(yīng)用程序來顯示搜索結(jié)果。檢索搜索結(jié)果涉及的主要類包括 ScoreDoc
和 TopDocs
。
ScoreDoc
- 搜索結(jié)果中包含一個指向文檔的簡單指針。這可以封裝文檔索引中文檔的位置以及 Lucene 計算的分?jǐn)?shù)。
TopDocs
- 封裝搜索結(jié)果以及
ScoreDoc
的總數(shù)。
以下代碼片段展示了如何檢索搜索結(jié)果中包含的文檔。
列表 11. 展示搜索結(jié)果
-
-
- TopDocs topDocs = indexSearcher.search(query,20);
- System.out.println("Total hits "+topDocs.totalHits);
-
-
- ScoreDoc[] scoreDosArray = topDocs.scoreDocs;
- for(ScoreDoc scoredoc: scoreDosArray){
-
- Document doc = indexSearcher.doc(scoredoc.doc);
- System.out.println("\nSender: "+doc.getField("sender").stringValue());
- System.out.println("Subject: "+doc.getField("subject").stringValue());
- System.out.println("Email file location: "
- +doc.getField("emailDoc").stringValue());
- }
/* First parameter is the query to be executed and
second parameter indicates the no of search results to fetch */
TopDocs topDocs = indexSearcher.search(query,20);
System.out.println("Total hits "+topDocs.totalHits);
// Get an array of references to matched documents
ScoreDoc[] scoreDosArray = topDocs.scoreDocs;
for(ScoreDoc scoredoc: scoreDosArray){
//Retrieve the matched document and show relevant details
Document doc = indexSearcher.doc(scoredoc.doc);
System.out.println("\nSender: "+doc.getField("sender").stringValue());
System.out.println("Subject: "+doc.getField("subject").stringValue());
System.out.println("Email file location: "
+doc.getField("emailDoc").stringValue());
}
基本的索引操作
基本的索引操作包括移除和提升文檔。
從索引中移除文檔
應(yīng)用程序常常需要使用最新的數(shù)據(jù)更新索引并移除較舊的數(shù)據(jù)。例如,在 Web 搜索引擎中,索引需要定期更新,因為總是需要添加新網(wǎng)頁,移除不存在的網(wǎng)頁。Lucene 提供了 IndexReader
接口允許您對索引執(zhí)行這些操作。
IndexReader
是一個提供各種方法訪問索引的抽象類。Lucene 內(nèi)部引用文檔時使用文檔編號,該編號可以在向索引添加或從中移除文檔時更改。文檔編號用于訪問索引中的文檔。IndexReader
不得用于更新目錄中的索引,因為已經(jīng)打開了 IndexWriter
。IndexReader
在打開時總是搜索索引的快照。對索引的任何更改都可以看到,直到再次打開 IndexReader
。使用 Lucene 重新打開它們的 IndexReader
可以看到最新的索引更新。
列表 12. 從索引中刪除文檔
-
- IndexReader indexReader = IndexReader.open(indexDirectory);
- indexReader.deleteDocuments(new Term("month","05"));
-
- indexReader.close();
// Delete all the mails from the index received in May 2009.
IndexReader indexReader = IndexReader.open(indexDirectory);
indexReader.deleteDocuments(new Term("month","05"));
//close associate index files and save deletions to disk
indexReader.close();
提升文檔和字段
有時您需要給某些索引數(shù)據(jù)更高的重要級別。您可以通過設(shè)置文檔或字段的提升因子實現(xiàn)這一點。默認(rèn)情況下,所有文檔和字段的默認(rèn)提升因子都是 1.0。
列表 13. 提升字段
- if(subject.toLowerCase().indexOf("pune") != -1){
-
- subjectField.setBoost(2.2F);
- }
-
- if(sender.toLowerCase().indexOf("job")!=-1){
- luceneDocument.setBoost(2.1F);
- }
if(subject.toLowerCase().indexOf("pune") != -1){
// Display search results that contain pune in their subject first by setting boost factor
subjectField.setBoost(2.2F);
}
//Display search results that contain 'job' in their sender email address
if(sender.toLowerCase().indexOf("job")!=-1){
luceneDocument.setBoost(2.1F);
}
擴(kuò)展搜索
Lucene 提供一個稱為排序 的高級功能。您可以根據(jù)指示文檔在索引中相對位置的字段對搜索結(jié)果進(jìn)行排序。用于排序的字段必須編制索引但不得標(biāo)記。搜索字段中可以放入 4 種可能的項值:整數(shù)值、long 值、浮點值和字符串。
還可以通過索引順序排序搜索結(jié)果。Lucene 通過降低相關(guān)度(比如默認(rèn)的計算分?jǐn)?shù))對結(jié)果排序。排序的順序是可以更改的。
列表 14. 排序搜索結(jié)果
-
-
-
- SortField sortField = new SortField("sender", true);
- Sort sortBySender = new Sort(sortField);
- WildcardQuery query = new WildcardQuery(new Term("subject","job*"));
- TopFieldDocs topFieldDocs =
- indexSearcher.search(query,null,20,sortBySender);
-
- topFieldDocs = indexSearcher.search(query,null,20,Sort.INDEXORDER);
/* Search mails having the word 'job' in subject and return results
sorted by sender's email in descending order.
*/
SortField sortField = new SortField("sender", true);
Sort sortBySender = new Sort(sortField);
WildcardQuery query = new WildcardQuery(new Term("subject","job*"));
TopFieldDocs topFieldDocs =
indexSearcher.search(query,null,20,sortBySender);
//Sorting by index order
topFieldDocs = indexSearcher.search(query,null,20,Sort.INDEXORDER);
Filtering 是限制搜索空間,只允許某個文檔子集作為搜索范圍的過程。您可以使用該功能實現(xiàn)對搜索結(jié)果進(jìn)行再次搜索,或者在搜索結(jié)果上實現(xiàn)安全性。Lucene 帶有各種內(nèi)置的過濾器,比如 BooleanFilter、CachingWrapperFilter、ChainedFilter、DuplicateFilter、PrefixFilter、QueryWrapperFilter、RangeFilter、RemoteCachingWrapperFilter、SpanFilter 等。Filter
可以傳遞到 IndexSearcher
的搜索方法,以過濾匹配篩選標(biāo)準(zhǔn)的篩選文檔。
列表 15. 篩選搜索結(jié)果
-
-
- Term prefix = new Term("sender","jobs");
- Filter prefixFilter = new PrefixFilter(prefix);
- WildcardQuery query = new WildcardQuery(new Term("subject","job*"));
- indexSearcher.search(query,prefixFilter,20);
/*Filter the results to show only mails that have sender field
prefixed with 'jobs' */
Term prefix = new Term("sender","jobs");
Filter prefixFilter = new PrefixFilter(prefix);
WildcardQuery query = new WildcardQuery(new Term("subject","job*"));
indexSearcher.search(query,prefixFilter,20);
結(jié)束語
Lucene 是來自 Apache 的一個非常流行的開源搜索庫, 它為應(yīng)用程序提供了強(qiáng)大的索引編制和搜索功能。它提供了一個簡單易用的 API,只需要稍微了解索引編制和搜索的原理即可使用。在本文中,您學(xué)習(xí)了 Lucene 架構(gòu)及其核心 API。
Lucene 為許多知名網(wǎng)站和組織提供了各種強(qiáng)大的搜索功能。它還兼容許多其他編程語言。Lucene 有一個活躍的大型技術(shù)用戶社區(qū)。如果您需要一些易用、可擴(kuò)展以及高性能的開源搜索庫,Apache Lucene 是一個極佳的選擇。