Lucene 軟件包的發布形式是一個 JAR 文件,下面我們分析一下這個 JAR 文件里面的主要的 JAVA 包,使讀者對之有個初步的了解。 Package: org.apache.lucene.document 這個包提供了一些為封裝要索引的文檔所需要的類,比如 Document, Field。這樣,每一個文檔最終被封裝成了一個 Document 對象。 Package: org.apache.lucene.analysis 這個包主要功能是對文檔進行分詞,因為文檔在建立索引之前必須要進行分詞,所以這個包的作用可以看成是為建立索引做準備工作。 Package: org.apache.lucene.index 這個包提供了一些類來協助創建索引以及對創建好的索引進行更新。這里面有兩個基礎的類:IndexWriter 和 IndexReader,其中 IndexWriter 是用來創建索引并添加文檔到索引中的,IndexReader 是用來刪除索引中的文檔的。 Package: org.apache.lucene.search 這個包提供了對在建立好的索引上進行搜索所需要的類。比如 IndexSearcher 和 Hits, IndexSearcher 定義了在指定的索引上進行搜索的方法,Hits 用來保存搜索得到的結果。 |
為了對文檔進行索引,Lucene 提供了五個基礎的類,他們分別是 Document, Field, IndexWriter, Analyzer, Directory。下面我們分別介紹一下這五個類的用途: Document Document 是用來描述文檔的,這里的文檔可以指一個 HTML 頁面,一封電子郵件,或者是一個文本文件。一個 Document 對象由多個 Field 對象組成的。可以把一個 Document 對象想象成數據庫中的一個記錄,而每個 Field 對象就是記錄的一個字段。 Field Field 對象是用來描述一個文檔的某個屬性的,比如一封電子郵件的標題和內容可以用兩個 Field 對象分別描述。 Analyzer 在一個文檔被索引之前,首先需要對文檔內容進行分詞處理,這部分工作就是由 Analyzer 來做的。Analyzer 類是一個抽象類,它有多個實現。針對不同的語言和應用需要選擇適合的 Analyzer。Analyzer 把分詞后的內容交給 IndexWriter 來建立索引。 IndexWriter IndexWriter 是 Lucene 用來創建索引的一個核心的類,他的作用是把一個個的 Document 對象加到索引中來。 Directory 這個類代表了 Lucene 的索引的存儲的位置,這是一個抽象類,它目前有兩個實現,第一個是 FSDirectory,它表示一個存儲在文件系統中的索引的位置。第二個是 RAMDirectory,它表示一個存儲在內存當中的索引的位置。 熟悉了建立索引所需要的這些類后,我們就開始對某個目錄下面的文本文件建立索引了,清單1給出了對某個目錄下的文本文件建立索引的源代碼。 |
package TestLucene; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; /** * This class demonstrate the process of creating index with Lucene * for text files */ public class TxtFileIndexer { public static void main(String[] args) throws Exception{ //indexDir is the directory that hosts Lucene's index files File indexDir = new File("D:\\luceneIndex"); //dataDir is the directory that hosts the text files that to be indexed File dataDir = new File("D:\\luceneData"); Analyzer luceneAnalyzer = new StandardAnalyzer(); File[] dataFiles = dataDir.listFiles(); IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true); long startTime = new Date().getTime(); for(int i = 0; i < dataFiles.length; i++){ if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){ System.out.println("Indexing file " + dataFiles[i].getCanonicalPath()); Document document = new Document(); Reader txtReader = new FileReader(dataFiles[i]); document.add(Field.Text("path",dataFiles[i].getCanonicalPath())); document.add(Field.Text("contents",txtReader)); indexWriter.addDocument(document); } } indexWriter.optimize(); indexWriter.close(); long endTime = new Date().getTime(); System.out.println("It takes " + (endTime - startTime) + " milliseconds to create index for the files in directory " + dataDir.getPath()); } }
|
利用Lucene進行搜索就像建立索引一樣也是非常方便的。在上面一部分中,我們已經為一個目錄下的文本文檔建立好了索引,現在我們就要在這個索引上進行搜索以找到包含某個關鍵詞或短語的文檔。Lucene提供了幾個基礎的類來完成這個過程,它們分別是呢IndexSearcher, Term, Query, TermQuery, Hits. 下面我們分別介紹這幾個類的功能。 Query 這是一個抽象類,他有多個實現,比如TermQuery, BooleanQuery, PrefixQuery. 這個類的目的是把用戶輸入的查詢字符串封裝成Lucene能夠識別的Query。 Term Term是搜索的基本單位,一個Term對象有兩個String類型的域組成。生成一個Term對象可以有如下一條語句來完成:Term term = new Term(“fieldName”,”queryWord”); 其中第一個參數代表了要在文檔的哪一個Field上進行查找,第二個參數代表了要查詢的關鍵詞。 TermQuery TermQuery是抽象類Query的一個子類,它同時也是Lucene支持的最為基本的一個查詢類。生成一個TermQuery對象由如下語句完成: TermQuery termQuery = new TermQuery(new Term(“fieldName”,”queryWord”)); 它的構造函數只接受一個參數,那就是一個Term對象。 IndexSearcher IndexSearcher是用來在建立好的索引上進行搜索的。它只能以只讀的方式打開一個索引,所以可以有多個IndexSearcher的實例在一個索引上進行操作。 Hits Hits是用來保存搜索的結果的。 介紹完這些搜索所必須的類之后,我們就開始在之前所建立的索引上進行搜索了,清單2給出了完成搜索功能所需要的代碼。 清單2 :在建立好的索引上進行搜索
在清單2中,類IndexSearcher的構造函數接受一個類型為Directory的對象,Directory是一個抽象類,它目前有兩個子類:FSDirctory和RAMDirectory. 我們的程序中傳入了一個FSDirctory對象作為其參數,代表了一個存儲在磁盤上的索引的位置。構造函數執行完成后,代表了這個IndexSearcher以只讀的方式打開了一個索引。然后我們程序構造了一個Term對象,通過這個Term對象,我們指定了要在文檔的內容中搜索包含關鍵詞”lucene”的文檔。接著利用這個Term對象構造出TermQuery對象并把這個TermQuery對象傳入到IndexSearcher的search方法中進行查詢,返回的結果保存在Hits對象中。最后我們用了一個循環語句把搜索到的文檔的路徑都打印了出來。好了,我們的搜索應用程序已經開發完畢,怎么樣,利用Lucene開發搜索應用程序是不是很簡單。 |
轉載地址:http://www-128.ibm.com/developerworks/cn/java/j-lo-lucene1/