馬光軍--------BLOG

          BlogJava 聯系 聚合 管理
            1 Posts :: 25 Stories :: 5 Comments :: 0 Trackbacks

          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給出了對某個目錄下的文本文件建立索引的源代碼。

          清單 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());
                      }
                      }
                      

          在清單1中,我們注意到類 IndexWriter 的構造函數需要三個參數,第一個參數指定了所創建的索引要存放的位置,他可以是一個 File 對象,
          也可以是一個 FSDirectory 對象或者 RAMDirectory 對象。
          第二個參數指定了 Analyzer 類的一個實現,也就是指定這個索引是用哪個分詞器對文擋內容進行分詞。
          第三個參數是一個布爾型的變量,如果為 true 的話就代表創建一個新的索引,為 false 的話就代表在原來索引的基礎上進行操作。
          接著程序遍歷了目錄下面的所有文本文檔,并為每一個文本文檔創建了一個 Document 對象。
          然后把文本文檔的兩個屬性:路徑和內容加入到了兩個 Field 對象中,接著在把這兩個 Field 對象加入到 Document 對象中,
          最后把這個文檔用 IndexWriter 類的 add 方法加入到索引中去。這樣我們便完成了索引的創建。接下來我們進入在建立好的索引上進行搜索的部分。

          搜索文檔

          利用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 :在建立好的索引上進行搜索
          package TestLucene;
                                  import java.io.File;
                                  import org.apache.lucene.document.Document;
                                  import org.apache.lucene.index.Term;
                                  import org.apache.lucene.search.Hits;
                                  import org.apache.lucene.search.IndexSearcher;
                                  import org.apache.lucene.search.TermQuery;
                                  import org.apache.lucene.store.FSDirectory;
                                  /**
                                  * This class is used to demonstrate the
                                  * process of searching on an existing
                                  * Lucene index
                                  *
                                  */
                                  public class TxtFileSearcher {
                                  public static void main(String[] args) throws Exception{
                                  String queryStr = "lucene";
                                  //This is the directory that hosts the Lucene index
                                  File indexDir = new File("D:\\luceneIndex");
                                  FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
                                  IndexSearcher searcher = new IndexSearcher(directory);
                                  if(!indexDir.exists()){
                                  System.out.println("The Lucene index is not exist");
                                  return;
                                  }
                                  Term term = new Term("contents",queryStr.toLowerCase());
                                  TermQuery luceneQuery = new TermQuery(term);
                                  Hits hits = searcher.search(luceneQuery);
                                  for(int i = 0; i < hits.length(); i++){
                                  Document document = hits.doc(i);
                                  System.out.println("File: " + document.get("path"));
                                  }
                                  }
                                  }
                                  

          在清單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/
          posted on 2008-11-01 17:57 馬光軍 閱讀(313) 評論(0)  編輯  收藏 所屬分類: 轉載Lucene

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 崇仁县| 错那县| 南雄市| 道孚县| 延寿县| 都江堰市| 来凤县| 开阳县| 墨江| 鄂尔多斯市| 肃宁县| 平陆县| 杨浦区| 勃利县| 桦南县| 景宁| 六枝特区| 吴旗县| 三门峡市| 荃湾区| 清水县| 濮阳县| 连云港市| 酒泉市| 宣城市| 龙里县| 龙川县| 玉屏| 连云港市| 泽库县| 阜平县| 禹城市| 大石桥市| 佛学| 卓资县| 营山县| 岑巩县| 施秉县| 自贡市| 金阳县| 南部县|