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>
posted on 2008-08-29 13:59
xiaoxinchen 閱讀(212)
評論(0) 編輯 收藏