我們簡單的講解一下什麼是全文檢索。
比如,我們一個文件夾中,或者一個磁盤中有很多的文件,記事本、world、Excel、pdf,我們想根據其中的關鍵詞搜索包含的文件。例如,我們輸入Lucene,所有內容含有Lucene的文件就會被檢查出來。這就是所謂的全文檢索。
因此,很容易的我們想到,應該建立一個關鍵字與文件的相關映射,下圖很明白的解釋了這種映射如何實現。
在Lucene中,就是使用這種“倒排索引”的技術,來實現相關映射。
有了這種映射關係,我們就來看看Lucene的架構設計。
下面是Lucene的資料必出現的一張圖,但也是其精髓的概括。
我們可以看到,Lucene的使用主要體現在兩個步驟:
1 創建索引,通過IndexWriter對不同的文件進行索引的創建,並將其保存在索引相關文件存儲的位置中。
2 通過索引查尋關鍵字相關文檔。
下面針對官網上面給出的一個例子,進行分析:
1 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); 2 3 // Store the index in memory: 4 Directory directory = new RAMDirectory(); 5 // To store an index on disk, use this instead: 6 //Directory directory = FSDirectory.open("/tmp/testindex"); 7 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); 8 IndexWriter iwriter = new IndexWriter(directory, config); 9 Document doc = new Document(); 10 String text = "This is the text to be indexed."; 11 doc.add(new Field("fieldname", text, TextField.TYPE_STORED)); 12 iwriter.addDocument(doc); 13 iwriter.close(); 14 15 // Now search the index: 16 DirectoryReader ireader = DirectoryReader.open(directory); 17 IndexSearcher isearcher = new IndexSearcher(ireader); 18 // Parse a simple query that searches for "text": 19 QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer); 20 Query query = parser.parse("text"); 21 ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; 22 assertEquals(1, hits.length); 23 // Iterate through the results: 24 for (int i = 0; i < hits.length; i++) { 25 Document hitDoc = isearcher.doc(hits[i].doc); 26 assertEquals("This is the text to be indexed.", hitDoc.get("fieldname")); 27 } 28 ireader.close(); 29 directory.close();
索引的創建
首先,我們需要定義一個詞法分析器。
比如一句話,“我愛我們的中國!”,如何對他拆分,扣掉停頓詞“的”,提取關鍵字“我”“我們”“中國”等等。這就要藉助的詞法分析器Analyzer來實現。這裏面使用的是標準的詞法分析器,如果專門針對漢語,還可以搭配paoding,進行使用。
1 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
參數中的Version.LUCENE_CURRENT,代表使用當前的Lucene版本,本文環境中也可以寫成Version.LUCENE_40。
第二步,確定索引文件存儲的位置,Lucene提供給我們兩種方式:
1 本地文件存儲
Directory directory = FSDirectory.open("/tmp/testindex");
2 內存存儲
Directory directory = new RAMDirectory();
可以根據自己的需要進行設定。
第三步,創建IndexWriter,進行索引文件的寫入。
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); IndexWriter iwriter = new IndexWriter(directory, config);
這裏的IndexWriterConfig,據官方文檔介紹,是對indexWriter的配置,其中包含了兩個參數,第一個是目前的版本,第二個是詞法分析器Analyzer。
第四步,內容提取,進行索引的存儲。
Document doc = new Document(); String text = "This is the text to be indexed."; doc.add(new Field("fieldname", text, TextField.TYPE_STORED)); iwriter.addDocument(doc); iwriter.close();
第一行,申請了一個document對象,這個類似於數據庫中的表中的一行。
第二行,是我們即將索引的字符串。
第三行,把字符串存儲起來(因爲設置了TextField.TYPE_STORED,如果不想存儲,可以使用其他參數,詳情參考官方文檔),並存儲“字段”爲"fieldname".
第四行,把doc對象加入到索引創建中。
第五行,關閉IndexWriter,提交創建內容。
這就是索引創建的過程。
關鍵字查詢:
第一步,打開存儲位置
DirectoryReader ireader = DirectoryReader.open(directory);
第二步,創建搜索器
IndexSearcher isearcher = new IndexSearcher(ireader);
第三步,類似SQL,進行關鍵字查詢
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer); Query query = parser.parse("text"); ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; assertEquals(1, hits.length); for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits[i].doc); assertEquals("This is the text to be indexed.",hitDoc.get("fieldname")); }
這裏,我們創建了一個查詢器,並設置其詞法分析器,以及查詢的“字段“爲”fieldname“。查詢結果會返回一個集合,類似SQL的ResultSet,我們可以提取其中存儲的內容。
關於各種不同的查詢方式,可以參考官方手冊。
第四步,關閉查詢器等。
ireader.close();
directory.close();
這是其中最常用的五個文件:
第一個,也是最重要的,Lucene-core-4.0.0.jar,其中包括了常用的文檔,索引,搜索,存儲等相關核心代碼。
第二個,Lucene-analyzers-common-4.0.0.jar,這裏麪包含了各種語言的詞法分析器,用於對文件內容進行關鍵字切分,提取。
第三個,Lucene-highlighter-4.0.0.jar,這個jar包主要用於搜索出的內容高亮顯示。
第四個和第五個,Lucene-queryparser-4.0.0.jar,提供了搜索相關的代碼,用於各種搜索,比如模糊搜索,範圍搜索