Lucene 索引數據庫(轉載)

  Lucene,作爲一種全文搜索的輔助工具,爲我們進行條件搜索,無論是像Google,Baidu之類的搜索引擎,還是論壇中的搜索功能,還是其它C/S架構的搜索,都帶來了極大的便利和比較高的效率。本文主要是利用Lucene對MS Sql Server 2000進行建立索引,然後進行全文索引。至於數據庫的內容,可以是網頁的內容,還是其它的。本文中數據庫的內容是圖書館管理系統中的某個作者表-Authors表。

  因爲考慮到篇幅的問題,所以該文不會講的很詳細,也不可能講的很深。

  本文以這樣的結構進行:

  1.介紹數據庫中Authors表的結構

  2.爲數據庫建立索引

  3.爲數據庫建立查詢功能

  4.在web界面下進行查詢並顯示結果

  1.介紹數據庫中Authors表的結構

字段名稱         字段類型         字段含義

Au_id                Varchar(11)    作者號
Au_name        Varchar(60)     作者名
Phone             Char(12)           電話號碼
Address          Varchar(40)      地址
City                   Varchar(20)     城市
State                Char(2)             省份
Zip                    Char(5)             郵編
contract            Bit(1)                外鍵(關係不大)


表中的部分內容:
 

  2.爲數據庫建立索引

  首先建立一個類TestLucene.java。這個類就是對數據庫進行建立索引,編寫查詢條件等。

  當然,最開始就是建立數據庫連接。連接代碼這裏就省略了。^_^

  接着,新建一個方法getResutl(String),它返回的是數據庫表Authors的內容。具體代碼如下:


    public ResultSet getResult(String sql){
      try{
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        return rs;
      }
      catch(SQLException e){
        System.out.println(e);
      }
      return null;
    }
 


  然後,爲數據庫建立索引。

  首先要定義一個IndexWriter(),它是將索引寫進Lucene自己的數據庫中,它存放的位置是有你自己定義的。在定義IndexWriter是需要指定它的分析器。Lucene自己自帶有幾個分析器,例如:StandarAnalyzer(),SimpleAnalyzer(),StopAnalyzer()等。它作用是對文本進行分析,判斷如何進行切詞。
接着,要定義一個Document。Document相當於二維表中一行數據一樣。Document裏包含的是Field字段,Field相當於數據庫中一列,也就是一個屬性,一個字段。
最後應該對IndexWriter進行優化,方法很簡單,就是writer.optimize().
具體代碼如下:


  public void Index(ResultSet rs){
      try{
        IndexWriter writer = new IndexWriter("d:/index/", getAnalyzer(), true);
        while(rs.next()){
            Document doc=new Document();
            doc.add(Field.Keyword("id",rs.getString("au_id")));
            doc.add(Field.Text("name",rs.getString("au_name")));
            doc.add(Field.UnIndexed("address",rs.getString("address")));
            doc.add(Field.UnIndexed("phone",rs.getString("phone")));
            doc.add(Field.Text("City",rs.getString("city")));
            writer.addDocument(doc);
          }
        writer.optimize();
        writer.close();
      }
      catch(IOException e){
        System.out.println(e);
      }
      catch(SQLException e){
        System.out.println(e);
      }
    }

    public Analyzer getAnalyzer(){
      return new StandardAnalyzer();
    }

 


  3.爲數據庫建立查詢功能

  在類TestLucene中建立一個新的方法searcher(String),它返回的是一個搜索的結構集,相當於數據庫中的ResultSet一樣。它代的參數是你要查詢的內容。這裏,我把要查詢的字段寫死了。你可以在添加一個參數表示要查詢的字段。
這裏主要有兩個對象IndexSearcher和Query。IndexSearcher是找到索引數據庫,Query是處理搜索,它包含了三個參數:查詢內容,查詢字段,分析器。
具體代碼如下:


  public Hits seacher(String queryString){
      Hits hits=null;;
      try{
        IndexSearcher is = new IndexSearcher("D:/index/");
        Query query=QueryParser.parse(queryString,"City",getAnalyzer());
        hits=is.search(query);
      }catch(Exception e){
        System.out.print(e);
      }
      return hits;
    }
 


  4.在web界面下進行查詢並顯示結果

  這裏建立一個Jsp頁面TestLucene.jsp進行搜索。

  在TestLucene.jsp頁面中首先引入類


<%@ page import="lucenetest.LucentTest"%>
<%@ page import="org.apache.lucene.search.*,org.apache.lucene.document.*" %>
 


  然後定義一個LuceneTest對象,獲取查詢結果集:


  LucentTest lucent=new LucentTest();
  Hits hits=lucent.seacher(request.getParameter("queryString"));
 


  定義一個Form,建立一個查詢環境:


<form action="TestLucene.jsp">
  <input  type="text" name="queryString"/>
  <input type="submit" value="搜索"/>
</form>
 


  顯示查詢結果:


<table>
  <%if(hits!=null){%>
  <tr>
    <td>作者號</td>
    <td>作者名</td>
    <td>地址</td>
    <td>電話號碼</td>
  </tr>

 <% for(int i=0;i<hits.length();i++){
    Document doc=hits.doc(i);
   %>
    <tr>
    <td><%=doc.get("id") %></td>
    <td><%=doc.get("name") %></td>
    <td><%=doc.get("address") %></td>
    <td><%=doc.get("phone") %></td>
  </tr>
 <% }}%>
</table>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章