|
本文要介紹一下如何利用IndexReader獲取信息。為什么要讀索引呢?因為我需要實現(xiàn)這些功能:
(1) 統(tǒng)計term在整個collection中的文檔頻度(document frequency, DF); (2) 統(tǒng)計term在整個collection中出現(xiàn)的詞次(term frequency in whole collection); (3) 統(tǒng)計term在某個文檔中出現(xiàn)的頻度(term frequency, TF); (4) 列出term在某文檔中出現(xiàn)的位置(position); (5) 整個collection中文檔的個數(shù); 那么為什么要用到這些數(shù)據(jù)呢?這些數(shù)據(jù)是實現(xiàn)TR(Text Retrieval,文本檢索)的必備的“原料”,而且是經(jīng)過加工的。在檢索之前,只有原始文本(raw da
聰明的讀者您可能會說,這看起來似乎很好做,不過就是計數(shù)(count)么。不錯,就是計數(shù),或者說是統(tǒng)計。但是看似簡單的過程,如果加上空間(內(nèi)存容量)的限制,就顯得不那么簡單了。假設(shè)如果每篇文檔有100個term,每個term需要存儲10字節(jié)信息,存1,000,000篇文檔需要 10x100x10^6=10^9=2^30字節(jié),也就是1GB。雖然現(xiàn)在1G內(nèi)存不算什么,可是總不能把1GB的數(shù)據(jù)時時刻刻都放入內(nèi)存吧。那么放入硬盤好了,現(xiàn)在需要用數(shù)據(jù)的時候,再把1GB數(shù)據(jù)從硬盤搬到內(nèi)存。OK,可以先去沖杯咖啡,回來在繼續(xù)下面的操作。這是1,000,000的文檔,如果更多一點呢,現(xiàn)在沒有任何輔助數(shù)據(jù)結(jié)構(gòu)的方式,會導(dǎo)致很差的效率。
Lucene的索引會把數(shù)據(jù)分成段,并且在需要的時候才讀,不需要的時候就讓數(shù)據(jù)乖乖地呆在硬盤上。Lucene本身是一個優(yōu)秀的索引引擎,能夠提供有效的索引和檢索機制。文本的目的是,介紹如用利用Lucene的API,如何從已經(jīng)建好的索引的數(shù)據(jù)中讀取需要的信息。至于Lucene如何使用,我會在后續(xù)的文章中逐漸介紹。
我們一步一步來看。這里建設(shè)已經(jīng)有實現(xiàn)建好索引,存放在index目錄下。好,要讀索引,總得先生成一個讀索引器(即Lucene中IndexReader的實例)。好,寫下面的程序(程序為C#程序,本文使用DotLucene)。
IndexReader reader; 問題出來了,IndexReader是一個abstract類,不能實例化。那好,換派生類試試看。找到IndexReader的兩個孩子——SegmentReader和MultiReader。用哪個呢?無論是哪個都需要一大堆參數(shù)(我是頗費了周折才搞清楚它們的用途,后面再解釋),似乎想用Lucene的索引數(shù)據(jù)不是那么容易啊。通過跟蹤代碼和查閱文檔,我終于找到使用IndexReader的鑰匙。原來IndexReader有一個“工廠模式”的static interface——IndexReader.Open。定義如下: #0001 public static IndexReader Open(System.String path) #0002 public static IndexReader Open(System.IO.FileInfo path) #0003 public static IndexReader Open(Directory directory) #0004 private static IndexReader Open(Directory directory, bool closeDirectory) 其中有三個是public的接口,可供調(diào)用。打開一個索引,就是這么簡單: #0001 IndexReader reader = IndexReader.Open(index); 實際上,這個打開索引經(jīng)歷了這樣的一個過程:
#0001 SegmentInfos infos = new SegmentInfos(); #0002 Directory directory = FSDirectory.GetDirectory(index, false); #0003 infos.Read(directory); #0004 bool closeDirectory = false; #0005 if (infos.Count == 1) #0006 { #0007 // index is optimized #0008 return new SegmentReader(infos, infos.Info(0), closeDirectory); #0009 } #0010 else #0011 { #0012 IndexReader[] readers = new IndexReader[infos.Count]; #0013 for (int i = 0; i < infos.Count; i++) #0014 readers[i] = new SegmentReader(infos.Info(i)); #0015 return new MultiReader(directory, infos, closeDirectory, readers); #0016 } 首先要讀入索引的段信息(segment information, #0001~#0003),然后看一下有幾個段:如果只有一個,那么可能是優(yōu)化過的,直接讀取這一個段就可以(#0008);否則需要一次讀入各個段(#0013~#0014),然后再拼成一個MultiReader(#0015)。打開索引文件的過程就是這樣。
接下來我們要看看如何讀取信息了。用下面這段代碼來說明。
#0001 public static void PrintIndex(IndexReader reader) #0002 { #0003 //顯示有多少個document #0004 System.Console.WriteLine(reader + "\tNumDocs = " + reader.NumDocs()); #0005 for (int i = 0; i < reader.NumDocs(); i++) #0006 { #0007 System.Console.WriteLine(reader.Document(i)); #0008 } #0009 #0010 //枚舉term,獲得<document, term freq, position* >信息 #0011 TermEnum termEnum = reader.Terms(); #0012 while (termEnum.Next()) #0013 { #0014 System.Console.Write(termEnum.Term()); #0015 System.Console.WriteLine("\tDocFreq=" + termEnum.DocFreq()); #0016 #0017 TermPositions termPositions = reader.TermPositions(termEnum.Term()); #0018 int i = 0; #0019 int j = 0; #0020 while (termPositions.Next()) #0021 { #0022 System.Console.WriteLine((i++) + "->" + " DocNo:" + termPositions.Doc() + ", Freq:" + termPositions.Freq()); #0023 for (j = 0; j < termPositions.Freq(); j++) #0024 System.Console.Write("[" + termPositions.NextPosition() + "]"); #0025 System.Console.WriteLine(); #0026 } #0027 #0028 //直接獲取 <term freq, document> 的信息 #0029 TermDocs termDocs = reader.TermDocs(termEnum.Term()); #0030 while (termDocs.Next()) #0031 { #0032 System.Console.WriteLine((i++) + "->" + " DocNo:" + termDocs.Doc() + ", Freq:" + termDocs.Freq()); #0033 } #0034 } #0035 #0036 // FieldInfos fieldInfos = reader.fieldInfos; #0037 // FieldInfo pathFieldInfo = fieldInfos.FieldInfo("path"); #0038 #0039 //顯示 term frequency vector #0040 for (int i = 0; i < reader.NumDocs(); i++) #0041 { #0042 //對contents的token之后的term存于了TermFreqVector #0043 TermFreqVector termFreqVector = reader.GetTermFreqVector(i, "contents"); #0044 #0045 if (termFreqVector == null) #0046 { #0047 System.Console.WriteLine("termFreqVector is null."); #0048 continue; #0049 } #0050 #0051 String fieldName = termFreqVector.GetField(); #0052 String[] terms = termFreqVector.GetTerms(); #0053 int[] frequences = termFreqVector.GetTermFrequencies(); #0054 #0055 System.Console.Write("FieldName:" + fieldName); #0056 for (int j = 0; j < terms.Length; j++) #0057 { #0058 System.Console.Write("[" + terms[j] + ":" + frequences[j] + "]"); #0059 } #0060 System.Console.WriteLine(); #0061 } #0062 System.Console.WriteLine(); #0063 } #0004 計算document的個數(shù)
#0012~#0034 枚舉collection中所有的term 其中#0017~#0026 枚舉每個term在出現(xiàn)的document中的所有位置(第幾個詞,從1開始計數(shù));#0029~#0033 計算每個term出現(xiàn)在哪些文檔和相應(yīng)的出現(xiàn)頻度(即DF和TF)。 #0036~#0037在reader是SegmentReader類型的情況下有效。 #0040~#0061可以快速的讀取某篇文檔中出現(xiàn)的term和相應(yīng)的頻度。但是這部分需要在建索引時,設(shè)置storeTermVector為true。比如 doc.Add(Field.Text("contents", reader, true)); 其中的第三項即是。默認為false。 有了這些數(shù)據(jù),就可以統(tǒng)計我需要的數(shù)據(jù)了。以后我會介紹如何建立索引,如何應(yīng)用Lucene。 from:http://lqgao.spaces./?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3dInside%2520Lucene http://hi.baidu.com/lewutian推薦文章: 1. Lucene(Nutch)距離商業(yè)文本搜索引擎還有多遠? 2. Lucene學(xué)習(xí)筆記 - 5 3. lucene.net 高級應(yīng)用之排序、設(shè)置權(quán)重、優(yōu)化、分布式搜索 4. lucene 的簡單實現(xiàn) 數(shù)據(jù)庫索引 demo by Alan 5. beta技術(shù)沙龍:大型網(wǎng)站的Lucene應(yīng)用 6. Lucene中文分詞 “庖丁解牛”使用指南 7. Lucene應(yīng)用-實現(xiàn)多重標準搜索 8. Lucene.Net Research 9. lucene學(xué)習(xí)3——詞條字典[Term Dictionary]文件(.tis和.tii)與詞條頻率文件(.frq)、詞條位置文件(.prx) 10. Lucene Payload 的研究與應(yīng)用 11. Lucene多字段搜索 12. lucene缺點匯總 13. 一個實例包含lucene所有檢索核心用法(多域檢索、多索引檢索) 14. Lucene 高亮顯示搜索結(jié)果 C#代碼 15. lucene 建立索引和簡單搜索 |
|
|