wyl232

          2008年9月19日 #

          用 Lucene 加速 Web 搜索應用程序的開發

          用 Lucene 加速 Web 搜索應用程序的開發

          級別: 中級

          周 登朋 (zhoudengpeng@yahoo.com.cn), 軟件工程師, 上海交通大學

          2006 年 9 月 06 日

          Lucene 是基于 Java 的全文信息檢索包,它目前是 Apache Jakarta 家族下面的一個開源項目。在這篇文章中,我們首先來看如何利用 Lucene 實現高級搜索功能,然后學習如何利用 Lucene 來創建一個健壯的 Web 搜索應用程序。

          在本篇文章中,你會學習到如何利用 Lucene 實現高級搜索功能以及如何利用 Lucene 來創建 Web 搜索應用程序。通過這些學習,你就可以利用 Lucene 來創建自己的搜索應用程序。

          架構概覽

          通常一個 Web 搜索引擎的架構分為前端和后端兩部分,就像圖一中所示。在前端流程中,用戶在搜索引擎提供的界面中輸入要搜索的關鍵詞,這里提到的用戶界面一般是一個帶有輸入框的 Web 頁面,然后應用程序將搜索的關鍵詞解析成搜索引擎可以理解的形式,并在索引文件上進行搜索操作。在排序后,搜索引擎返回搜索結果給用戶。在后端流程中,網絡爬蟲或者機器人從因特網上獲取 Web 頁面,然后索引子系統解析這些 Web 頁面并存入索引文件中。如果你想利用 Lucene 來創建一個 Web 搜索應用程序,那么它的架構也和上面所描述的類似,就如圖一中所示。


          Figure 1. Web 搜索引擎架構
          Web搜索引擎架構

          利用 Lucene 實現高級搜索

          Lucene 支持多種形式的高級搜索,我們在這一部分中會進行探討,然后我會使用 Lucene 的 API 來演示如何實現這些高級搜索功能。

          布爾操作符

          大多數的搜索引擎都會提供布爾操作符讓用戶可以組合查詢,典型的布爾操作符有 AND, OR, NOT。Lucene 支持 5 種布爾操作符,分別是 AND, OR, NOT, 加(+), 減(-)。接下來我會講述每個操作符的用法。

          • OR: 如果你要搜索含有字符 A 或者 B 的文檔,那么就需要使用 OR 操作符。需要記住的是,如果你只是簡單的用空格將兩個關鍵詞分割開,其實在搜索的時候搜索引擎會自動在兩個關鍵詞之間加上 OR 操作符。例如,“Java OR Lucene” 和 “Java Lucene” 都是搜索含有 Java 或者含有 Lucene 的文檔。
          • AND: 如果你需要搜索包含一個以上關鍵詞的文檔,那么就需要使用 AND 操作符。例如,“Java AND Lucene” 返回所有既包含 Java 又包含 Lucene 的文檔。
          • NOT: Not 操作符使得包含緊跟在 NOT 后面的關鍵詞的文檔不會被返回。例如,如果你想搜索所有含有 Java 但不含有 Lucene 的文檔,你可以使用查詢語句 “Java NOT Lucene”。但是你不能只對一個搜索詞使用這個操作符,比如,查詢語句 “NOT Java” 不會返回任何結果。
          • 加號(+): 這個操作符的作用和 AND 差不多,但它只對緊跟著它的一個搜索詞起作用。例如,如果你想搜索一定包含 Java,但不一定包含 Lucene 的文檔,就可以使用查詢語句“+Java Lucene”。
          • 減號(-): 這個操作符的功能和 NOT 一樣,查詢語句 “Java -Lucene” 返回所有包含 Java 但不包含 Lucene 的文檔。

          接下來我們看一下如何利用 Lucene 提供的 API 來實現布爾查詢。清單1 顯示了如果利用布爾操作符進行查詢的過程。


          清單1:使用布爾操作符
            //Test boolean operator
                      public void testOperator(String indexDirectory) throws Exception{
                      Directory dir = FSDirectory.getDirectory(indexDirectory,false);
                      IndexSearcher indexSearcher = new IndexSearcher(dir);
                      String[] searchWords = {"Java AND Lucene", "Java NOT Lucene", "Java OR Lucene",
                      "+Java +Lucene", "+Java -Lucene"};
                      Analyzer language = new StandardAnalyzer();
                      Query query;
                      for(int i = 0; i < searchWords.length; i++){
                      query = QueryParser.parse(searchWords[i], "title", language);
                      Hits results = indexSearcher.search(query);
                      System.out.println(results.length() + "search results for query " + searchWords[i]);
                      }
                      }
                      

          域搜索(Field Search)

          Lucene 支持域搜索,你可以指定一次查詢是在哪些域(Field)上進行。例如,如果索引的文檔包含兩個域,TitleContent,你就可以使用查詢 “Title: Lucene AND Content: Java” 來返回所有在 Title 域上包含 Lucene 并且在 Content 域上包含 Java 的文檔。清單 2 顯示了如何利用 Lucene 的 API 來實現域搜索。


          清單2:實現域搜索
          //Test field search
                      public void testFieldSearch(String indexDirectory) throws Exception{
                      Directory dir = FSDirectory.getDirectory(indexDirectory,false);
                      IndexSearcher indexSearcher = new IndexSearcher(dir);
                      String searchWords = "title:Lucene AND content:Java";
                      Analyzer language = new StandardAnalyzer();
                      Query query = QueryParser.parse(searchWords, "title", language);
                      Hits results = indexSearcher.search(query);
                      System.out.println(results.length() + "search results for query " + searchWords);
                      }
                      

          通配符搜索(Wildcard Search)

          Lucene 支持兩種通配符:問號(?)和星號(*)。你可以使用問號(?)來進行單字符的通配符查詢,或者利用星號(*)進行多字符的通配符查詢。例如,如果你想搜索 tiny 或者 tony,你就可以使用查詢語句 “t?ny”;如果你想查詢 Teach, Teacher 和 Teaching,你就可以使用查詢語句 “Teach*”。清單3 顯示了通配符查詢的過程。


          清單3:進行通配符查詢
          //Test wildcard search
                      public void testWildcardSearch(String indexDirectory)throws Exception{
                      Directory dir = FSDirectory.getDirectory(indexDirectory,false);
                      IndexSearcher indexSearcher = new IndexSearcher(dir);
                      String[] searchWords = {"tex*", "tex?", "?ex*"};
                      Query query;
                      for(int i = 0; i < searchWords.length; i++){
                      query = new WildcardQuery(new Term("title",searchWords[i]));
                      Hits results = indexSearcher.search(query);
                      System.out.println(results.length() + "search results for query " + searchWords[i]);
                      }
                      }
                      

          模糊查詢

          Lucene 提供的模糊查詢基于編輯距離算法(Edit distance algorithm)。你可以在搜索詞的尾部加上字符 ~ 來進行模糊查詢。例如,查詢語句 “think~” 返回所有包含和 think 類似的關鍵詞的文檔。清單 4 顯示了如果利用 Lucene 的 API 進行模糊查詢的代碼。


          清單4:實現模糊查詢
          //Test fuzzy search
                      public void testFuzzySearch(String indexDirectory)throws Exception{
                      Directory dir = FSDirectory.getDirectory(indexDirectory,false);
                      IndexSearcher indexSearcher = new IndexSearcher(dir);
                      String[] searchWords = {"text", "funny"};
                      Query query;
                      for(int i = 0; i < searchWords.length; i++){
                      query = new FuzzyQuery(new Term("title",searchWords[i]));
                      Hits results = indexSearcher.search(query);
                      System.out.println(results.length() + "search results for query " + searchWords[i]);
                      }
                      }
                      

          范圍搜索(Range Search)

          范圍搜索匹配某個域上的值在一定范圍的文檔。例如,查詢 “age:[18 TO 35]” 返回所有 age 域上的值在 18 到 35 之間的文檔。清單5顯示了利用 Lucene 的 API 進行返回搜索的過程。


          清單5:測試范圍搜索
          //Test range search
                      public void testRangeSearch(String indexDirectory)throws Exception{
                      Directory dir = FSDirectory.getDirectory(indexDirectory,false);
                      IndexSearcher indexSearcher = new IndexSearcher(dir);
                      Term begin = new Term("birthDay","20000101");
                      Term end   = new Term("birthDay","20060606");
                      Query query = new RangeQuery(begin,end,true);
                      Hits results = indexSearcher.search(query);
                      System.out.println(results.length() + "search results is returned");
                      }
                      





          回頁首


          在 Web 應用程序中集成 Lucene

          接下來我們開發一個 Web 應用程序利用 Lucene 來檢索存放在文件服務器上的 HTML 文檔。在開始之前,需要準備如下環境:

          1. Eclipse 集成開發環境
          2. Tomcat 5.0
          3. Lucene Library
          4. JDK 1.5

          這個例子使用 Eclipse 進行 Web 應用程序的開發,最終這個 Web 應用程序跑在 Tomcat 5.0 上面。在準備好開發所必需的環境之后,我們接下來進行 Web 應用程序的開發。

          1、創建一個動態 Web 項目

          1. 在 Eclipse 里面,選擇 File > New > Project,然后再彈出的窗口中選擇動態 Web 項目,如圖二所示。

          圖二:創建動態Web項目
          創建動態Web項目
          1. 在創建好動態 Web 項目之后,你會看到創建好的項目的結構,如圖三所示,項目的名稱為 sample.dw.paper.lucene。

          圖三:動態 Web 項目的結構
          動態 Web 項目的結構

          2. 設計 Web 項目的架構

          在我們的設計中,把該系統分成如下四個子系統:

          1. 用戶接口: 這個子系統提供用戶界面使用戶可以向 Web 應用程序服務器提交搜索請求,然后搜索結果通過用戶接口來顯示出來。我們用一個名為 search.jsp 的頁面來實現該子系統。
          2. 請求管理器: 這個子系統管理從客戶端發送過來的搜索請求并把搜索請求分發到搜索子系統中。最后搜索結果從搜索子系統返回并最終發送到用戶接口子系統。我們使用一個 Servlet 來實現這個子系統。
          3. 搜索子系統: 這個子系統負責在索引文件上進行搜索并把搜索結構傳遞給請求管理器。我們使用 Lucene 提供的 API 來實現該子系統。
          4. 索引子系統: 這個子系統用來為 HTML 頁面來創建索引。我們使用 Lucene 的 API 以及 Lucene 提供的一個 HTML 解析器來創建該子系統。

          圖4 顯示了我們設計的詳細信息,我們將用戶接口子系統放到 webContent 目錄下面。你會看到一個名為 search.jsp 的頁面在這個文件夾里面。請求管理子系統在包 sample.dw.paper.lucene.servlet 下面,類 SearchController 負責功能的實現。搜索子系統放在包 sample.dw.paper.lucene.search 當中,它包含了兩個類,SearchManagerSearchResultBean,第一個類用來實現搜索功能,第二個類用來描述搜索結果的結構。索引子系統放在包 sample.dw.paper.lucene.index 當中。類 IndexManager 負責為 HTML 文件創建索引。該子系統利用包 sample.dw.paper.lucene.util 里面的類 HTMLDocParser 提供的方法 getTitlegetContent 來對 HTML 頁面進行解析。


          圖四:項目的架構設計
          項目的架構設計

          3. 子系統的實現

          在分析了系統的架構設計之后,我們接下來看系統實現的詳細信息。

          1. 用戶接口: 這個子系統有一個名為 search.jsp 的 JSP 文件來實現,這個 JSP 頁面包含兩個部分。第一部分提供了一個用戶接口去向 Web 應用程序服務器提交搜索請求,如圖5所示。注意到這里的搜索請求發送到了一個名為 SearchController 的 Servlet 上面。Servlet 的名字和具體實現的類的對應關系在 web.xml 里面指定。

          圖5:向Web服務器提交搜索請求
          向Web服務器提交搜索請求

          這個JSP的第二部分負責顯示搜索結果給用戶,如圖6所示:


          圖6:顯示搜索結果
          顯示搜索結果
          1. 請求管理器: 一個名為 SearchController 的 servlet 用來實現該子系統。清單6給出了這個類的源代碼。

          清單6:請求管理器的實現
          package sample.dw.paper.lucene.servlet;
                      import java.io.IOException;
                      import java.util.List;
                      import javax.servlet.RequestDispatcher;
                      import javax.servlet.ServletException;
                      import javax.servlet.http.HttpServlet;
                      import javax.servlet.http.HttpServletRequest;
                      import javax.servlet.http.HttpServletResponse;
                      import sample.dw.paper.lucene.search.SearchManager;
                      /**
                      * This servlet is used to deal with the search request
                      * and return the search results to the client
                      */
                      public class SearchController extends HttpServlet{
                      private static final long serialVersionUID = 1L;
                      public void doPost(HttpServletRequest request, HttpServletResponse response)
                      throws IOException, ServletException{
                      String searchWord = request.getParameter("searchWord");
                      SearchManager searchManager = new SearchManager(searchWord);
                      List searchResult = null;
                      searchResult = searchManager.search();
                      RequestDispatcher dispatcher = request.getRequestDispatcher("search.jsp");
                      request.setAttribute("searchResult",searchResult);
                      dispatcher.forward(request, response);
                      }
                      public void doGet(HttpServletRequest request, HttpServletResponse response)
                      throws IOException, ServletException{
                      doPost(request, response);
                      }
                      }
                      

          清單6中,doPost 方法從客戶端獲取搜索詞并創建類 SearchManager 的一個實例,其中類 SearchManager 在搜索子系統中進行了定義。然后,SearchManager 的方法 search 會被調用。最后搜索結果被返回到客戶端。

          1. 搜索子系統: 在這個子系統中,我們定義了兩個類:SearchManagerSearchResultBean。第一個類用來實現搜索功能,第二個類是個JavaBean,用來描述搜索結果的結構。清單7給出了類 SearchManager 的源代碼。

          清單7:搜索功能的實現
          package sample.dw.paper.lucene.search;
                      import java.io.IOException;
                      import java.util.ArrayList;
                      import java.util.List;
                      import org.apache.lucene.analysis.Analyzer;
                      import org.apache.lucene.analysis.standard.StandardAnalyzer;
                      import org.apache.lucene.queryParser.ParseException;
                      import org.apache.lucene.queryParser.QueryParser;
                      import org.apache.lucene.search.Hits;
                      import org.apache.lucene.search.IndexSearcher;
                      import org.apache.lucene.search.Query;
                      import sample.dw.paper.lucene.index.IndexManager;
                      /**
                      * This class is used to search the
                      * Lucene index and return search results
                      */
                      public class SearchManager {
                      private String searchWord;
                      private IndexManager indexManager;
                      private Analyzer analyzer;
                      public SearchManager(String searchWord){
                      this.searchWord   =  searchWord;
                      this.indexManager =  new IndexManager();
                      this.analyzer     =  new StandardAnalyzer();
                      }
                      /**
                      * do search
                      */
                      public List search(){
                      List searchResult = new ArrayList();
                      if(false == indexManager.ifIndexExist()){
                      try {
                      if(false == indexManager.createIndex()){
                      return searchResult;
                      }
                      } catch (IOException e) {
                      e.printStackTrace();
                      return searchResult;
                      }
                      }
                      IndexSearcher indexSearcher = null;
                      try{
                      indexSearcher = new IndexSearcher(indexManager.getIndexDir());
                      }catch(IOException ioe){
                      ioe.printStackTrace();
                      }
                      QueryParser queryParser = new QueryParser("content",analyzer);
                      Query query = null;
                      try {
                      query = queryParser.parse(searchWord);
                      } catch (ParseException e) {
                      e.printStackTrace();
                      }
                      if(null != query >> null != indexSearcher){
                      try {
                      Hits hits = indexSearcher.search(query);
                      for(int i = 0; i < hits.length(); i ++){
                      SearchResultBean resultBean = new SearchResultBean();
                      resultBean.setHtmlPath(hits.doc(i).get("path"));
                      resultBean.setHtmlTitle(hits.doc(i).get("title"));
                      searchResult.add(resultBean);
                      }
                      } catch (IOException e) {
                      e.printStackTrace();
                      }
                      }
                      return searchResult;
                      }
                      }
                      

          清單7中,注意到在這個類里面有三個私有屬性。第一個是 searchWord,代表了來自客戶端的搜索詞。第二個是 indexManager,代表了在索引子系統中定義的類 IndexManager 的一個實例。第三個是 analyzer,代表了用來解析搜索詞的解析器。現在我們把注意力放在方法 search 上面。這個方法首先檢查索引文件是否已經存在,如果已經存在,那么就在已經存在的索引上進行檢索,如果不存在,那么首先調用類 IndexManager 提供的方法來創建索引,然后在新創建的索引上進行檢索。搜索結果返回后,這個方法從搜索結果中提取出需要的屬性并為每個搜索結果生成類 SearchResultBean 的一個實例。最后這些 SearchResultBean 的實例被放到一個列表里面并返回給請求管理器。

          在類 SearchResultBean 中,含有兩個屬性,分別是 htmlPathhtmlTitle,以及這個兩個屬性的 get 和 set 方法。這也意味著我們的搜索結果包含兩個屬性:htmlPathhtmlTitle,其中 htmlPath 代表了 HTML 文件的路徑,htmlTitle 代表了 HTML 文件的標題。

          1. 索引子系統: 類 IndexManager 用來實現這個子系統。清單8 給出了這個類的源代碼。

          清單8:索引子系統的實現
          package sample.dw.paper.lucene.index;
                      import java.io.File;
                      import java.io.IOException;
                      import java.io.Reader;
                      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;
                      import org.apache.lucene.store.Directory;
                      import org.apache.lucene.store.FSDirectory;
                      import sample.dw.paper.lucene.util.HTMLDocParser;
                      /**
                      * This class is used to create an index for HTML files
                      *
                      */
                      public class IndexManager {
                      //the directory that stores HTML files
                      private final String dataDir  = "c:\\dataDir";
                      //the directory that is used to store a Lucene index
                      private final String indexDir = "c:\\indexDir";
                      /**
                      * create index
                      */
                      public boolean createIndex() throws IOException{
                      if(true == ifIndexExist()){
                      return true;
                      }
                      File dir = new File(dataDir);
                      if(!dir.exists()){
                      return false;
                      }
                      File[] htmls = dir.listFiles();
                      Directory fsDirectory = FSDirectory.getDirectory(indexDir, true);
                      Analyzer  analyzer    = new StandardAnalyzer();
                      IndexWriter indexWriter = new IndexWriter(fsDirectory, analyzer, true);
                      for(int i = 0; i < htmls.length; i++){
                      String htmlPath = htmls[i].getAbsolutePath();
                      if(htmlPath.endsWith(".html") || htmlPath.endsWith(".htm")){
                      addDocument(htmlPath, indexWriter);
                      }
                      }
                      indexWriter.optimize();
                      indexWriter.close();
                      return true;
                      }
                      /**
                      * Add one document to the Lucene index
                      */
                      public void addDocument(String htmlPath, IndexWriter indexWriter){
                      HTMLDocParser htmlParser = new HTMLDocParser(htmlPath);
                      String path    = htmlParser.getPath();
                      String title   = htmlParser.getTitle();
                      Reader content = htmlParser.getContent();
                      Document document = new Document();
                      document.add(new Field("path",path,Field.Store.YES,Field.Index.NO));
                      document.add(new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED));
                      document.add(new Field("content",content));
                      try {
                      indexWriter.addDocument(document);
                      } catch (IOException e) {
                      e.printStackTrace();
                      }
                      }
                      /**
                      * judge if the index exists already
                      */
                      public boolean ifIndexExist(){
                      File directory = new File(indexDir);
                      if(0 < directory.listFiles().length){
                      return true;
                      }else{
                      return false;
                      }
                      }
                      public String getDataDir(){
                      return this.dataDir;
                      }
                      public String getIndexDir(){
                      return this.indexDir;
                      }
                      }
                      

          這個類包含兩個私有屬性,分別是 dataDirindexDirdataDir 代表存放等待進行索引的 HTML 頁面的路徑,indexDir 代表了存放 Lucene 索引文件的路徑。類 IndexManager 提供了三個方法,分別是 createIndex, addDocumentifIndexExist。如果索引不存在的話,你可以使用方法 createIndex 去創建一個新的索引,用方法 addDocument 去向一個索引上添加文檔。在我們的場景中,一個文檔就是一個 HTML 頁面。方法 addDocument 會調用由類 HTMLDocParser 提供的方法對 HTML 文檔進行解析。你可以使用最后一個方法 ifIndexExist 來判斷 Lucene 的索引是否已經存在。

          現在我們來看一下放在包 sample.dw.paper.lucene.util 里面的類 HTMLDocParser。這個類用來從 HTML 文件中提取出文本信息。這個類包含三個方法,分別是 getContentgetTitlegetPath。第一個方法返回去除了 HTML 標記的文本內容,第二個方法返回 HTML 文件的標題,最后一個方法返回 HTML 文件的路徑。清單9 給出了這個類的源代碼。


          清單9:HTML 解析器
          package sample.dw.paper.lucene.util;
                      import java.io.FileInputStream;
                      import java.io.FileNotFoundException;
                      import java.io.IOException;
                      import java.io.InputStream;
                      import java.io.InputStreamReader;
                      import java.io.Reader;
                      import java.io.UnsupportedEncodingException;
                      import org.apache.lucene.demo.html.HTMLParser;
                      public class HTMLDocParser {
                      private String htmlPath;
                      private HTMLParser htmlParser;
                      public HTMLDocParser(String htmlPath){
                      this.htmlPath = htmlPath;
                      initHtmlParser();
                      }
                      private void initHtmlParser(){
                      InputStream inputStream = null;
                      try {
                      inputStream = new FileInputStream(htmlPath);
                      } catch (FileNotFoundException e) {
                      e.printStackTrace();
                      }
                      if(null != inputStream){
                      try {
                      htmlParser = new HTMLParser(new InputStreamReader(inputStream, "utf-8"));
                      } catch (UnsupportedEncodingException e) {
                      e.printStackTrace();
                      }
                      }
                      }
                      public String getTitle(){
                      if(null != htmlParser){
                      try {
                      return htmlParser.getTitle();
                      } catch (IOException e) {
                      e.printStackTrace();
                      } catch (InterruptedException e) {
                      e.printStackTrace();
                      }
                      }
                      return "";
                      }
                      public Reader getContent(){
                      if(null != htmlParser){
                      try {
                      return htmlParser.getReader();
                      } catch (IOException e) {
                      e.printStackTrace();
                      }
                      }
                      return null;
                      }
                      public String getPath(){
                      return this.htmlPath;
                      }
                      }
                      

          5.在 Tomcat 5.0 上運行應用程序

          現在我們可以在 Tomcat 5.0 上運行開發好的應用程序。

          1. 右鍵單擊 search.jsp,然后選擇 Run as > Run on Server,如圖7所示。

          圖7:配置 Tomcat 5.0
          配置 Tomcat 5.0
          1. 在彈出的窗口中,選擇 Tomcat v5.0 Server 作為目標 Web 應用程序服務器,然后點擊 Next,如圖8 所示:

          圖8:選擇 Tomcat 5.0
          選擇 Tomcat 5.0
          1. 現在需要指定用來運行 Web 應用程序的 Apache Tomcat 5.0 以及 JRE 的路徑。這里你所選擇的 JRE 的版本必須和你用來編譯 Java 文件的 JRE 的版本一致。配置好之后,點擊 Finish。如 圖9 所示。

          圖9:完成Tomcat 5.0的配置
          完成Tomcat 5.0的配置
          1. 配置好之后,Tomcat 會自動運行,并且會對 search.jsp 進行編譯并顯示給用戶。如 圖10 所示。

          圖10:用戶界面
          用戶界面
          1. 在輸入框中輸入關鍵詞 “information” 然后單擊 Search 按鈕。然后這個頁面上會顯示出搜索結果來,如 圖11 所示。

          圖11:搜索結果
          搜索結果
          1. 單擊搜索結果的第一個鏈接,頁面上就會顯示出所鏈接到的頁面的內容。如 圖12 所示.

          圖12:詳細信息
          詳細信息

          現在我們已經成功的完成了示例項目的開發,并成功的用Lucene實現了搜索和索引功能。你可以下載這個項目的源代碼(下載)。





          回頁首


          總結

          Lucene 提供了靈活的接口使我們更加方便的設計我們的 Web 搜索應用程序。如果你想在你的應用程序中加入搜索功能,那么 Lucene 是一個很好的選擇。在設計你的下一個帶有搜索功能的應用程序的時候可以考慮使用 Lucene 來提供搜索功能。

          posted @ 2008-09-19 11:19 wyl 閱讀(80) | 評論 (0)編輯 收藏

          實戰 Lucene,第 1 部分: 初識 Lucene

          級別: 初級

          朋 周登 (zhoudengpeng@yahoo.com.cn), 軟件工程師

          2006 年 4 月 20 日

          本文首先介紹了Lucene的一些基本概念,然后開發了一個應用程序演示了利用Lucene建立索引并在該索引上進行搜索的過程。

          Lucene 簡介

          Lucene 是一個基于 Java 的全文信息檢索工具包,它不是一個完整的搜索應用程序,而是為你的應用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一個開源項目。也是目前最為流行的基于 Java 開源全文檢索工具包。

          目前已經有很多應用程序的搜索功能是基于 Lucene 的,比如 Eclipse 的幫助系統的搜索功能。Lucene 能夠為文本類型的數據建立索引,所以你只要能把你要索引的數據格式轉化的文本的,Lucene 就能對你的文檔進行索引和搜索。比如你要對一些 HTML 文檔,PDF 文檔進行索引的話你就首先需要把 HTML 文檔和 PDF 文檔轉化成文本格式的,然后將轉化后的內容交給 Lucene 進行索引,然后把創建好的索引文件保存到磁盤或者內存中,最后根據用戶輸入的查詢條件在索引文件上進行查詢。不指定要索引的文檔的格式也使 Lucene 能夠幾乎適用于所有的搜索應用程序。

          圖 1 表示了搜索應用程序和 Lucene 之間的關系,也反映了利用 Lucene 構建搜索應用程序的流程:


          圖1. 搜索應用程序和 Lucene 之間的關系
          圖1. 搜索應用程序和 Lucene 之間的關系




          回頁首


          索引和搜索

          索引是現代搜索引擎的核心,建立索引的過程就是把源數據處理成非常方便查詢的索引文件的過程。為什么索引這么重要呢,試想你現在要在大量的文檔中搜索含有某個關鍵詞的文檔,那么如果不建立索引的話你就需要把這些文檔順序的讀入內存,然后檢查這個文章中是不是含有要查找的關鍵詞,這樣的話就會耗費非常多的時間,想想搜索引擎可是在毫秒級的時間內查找出要搜索的結果的。這就是由于建立了索引的原因,你可以把索引想象成這樣一種數據結構,他能夠使你快速的隨機訪問存儲在索引中的關鍵詞,進而找到該關鍵詞所關聯的文檔。Lucene 采用的是一種稱為反向索引(inverted index)的機制。反向索引就是說我們維護了一個詞/短語表,對于這個表中的每個詞/短語,都有一個鏈表描述了有哪些文檔包含了這個詞/短語。這樣在用戶輸入查詢條件的時候,就能非常快的得到搜索結果。我們將在本系列文章的第二部分詳細介紹 Lucene 的索引機制,由于 Lucene 提供了簡單易用的 API,所以即使讀者剛開始對全文本進行索引的機制并不太了解,也可以非常容易的使用 Lucene 對你的文檔實現索引。

          對文檔建立好索引后,就可以在這些索引上面進行搜索了。搜索引擎首先會對搜索的關鍵詞進行解析,然后再在建立好的索引上面進行查找,最終返回和用戶輸入的關鍵詞相關聯的文檔。





          回頁首


          Lucene 軟件包分析

          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 對這個目錄中的文檔建立索引,然后在建立好的索引中搜索我們所要查找的文檔。通過這個例子讀者會對如何利用 Lucene 構建自己的搜索應用程序有個比較清楚的認識。





          回頁首


          建立索引

          為了對文檔進行索引,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開發搜索應用程序是不是很簡單。





          回頁首


          總結

          本文首先介紹了 Lucene 的一些基本概念,然后開發了一個應用程序演示了利用 Lucene 建立索引并在該索引上進行搜索的過程。希望本文能夠為學習 Lucene 的讀者提供幫助。



          關于作者

           

          周登朋,軟件工程師,上海交通大學研究生,對 Java 技術以及信息檢索技術很感興趣。您可以通過 zhoudengpeng@yahoo.com.cn 與他聯系。

          posted @ 2008-09-19 11:16 wyl 閱讀(104) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 喀什市| 芒康县| 班戈县| 呼伦贝尔市| 太原市| 高陵县| 武鸣县| 锦屏县| 兰溪市| 阳泉市| 新郑市| 集贤县| 汝州市| 长岛县| 宜阳县| 常山县| 施甸县| 绥阳县| 合山市| 东兰县| 武宁县| 抚顺市| 且末县| 天祝| 博乐市| 潍坊市| 台中县| 富顺县| 湖南省| 临邑县| 冀州市| 大英县| 丰宁| 集安市| 永新县| 太康县| 宁远县| 咸丰县| 丘北县| 湄潭县| 拉萨市|