js|全文檢索索引 
全文檢索索引一直都是web方面的關鍵技術,如何在浩如煙海的資訊中找到自己想要的資訊是人們最關心的。鼎鼎大名的GOOGLE就是一個很成功的例子,網路上的人們大部分都用GOOGLE來尋找自己需要的內容。全文檢索索引主要有兩個技術指標:快速和精確。前一段時間做了一個新聞系統,老闆要加上全文檢索索引的功能,想了很久才用一個不太高明的方法實現了。現在分享一下,希望是拋磚引玉吧,如果大家有更好的辦法請跟在後邊:) 
先介紹一下我的新聞系統:資料庫裡存新聞的基本資料,如標題,發布人,發布時間,主體新聞的檔案名稱。新聞主體是html格式的靜態頁(第一是要提高速度,減少資料庫的壓力。第二是資料庫處理大字串的時候會有問題。)。全文檢索索引的思路是:先從資料庫裡把所有的新聞檢索出來,把主體新聞找到,然後通過io操作把主體新聞讀到一個字串中。再去掉多餘的東西,象html標記什麼的,再用Regex對這個字串尋找,如果找到合格資訊,就記錄這條新聞。最後返回所有的合格新聞顯示給使用者。 
下面這段代碼是輸入查詢條件的代碼,查詢關鍵字用”+”隔開:search.jsp 
<html> 
<head> 
<link rel="stylesheet" href="css/style3.css"> 
<title>新聞搜尋</title> 
<script language="javascript"> 
function subform() 
{ 
if (document.zl_form.keyword.value=="") 
{ 
alert("請輸入關鍵字!"); 
document.zl_form.keyword.focus(); 
return false; 
} 
return true; 
} 
</script> 
</head> 
<body bgcolor="#F0F6E2"> 
<form name="zl_form" target="_new" method="post" action="aftsearch.jsp" > 
<table width="600" bgcolor="#F0F6E2"> 
<tr> 
<td colspan="4" height="10">  </td> 
</tr> 
<tr> 
<td width="14%">輸入查詢關鍵字:</td> 
<td align="left" width="65%"> 
<input size="50" type="text" name="keyword" style="font-size: 9pt"> 
<input type="submit" name="submit" value="搜尋" style="font-size: 9pt"> 
</td> 
</tr> 
<tr> 
<td colspan="2" height="9" align="left"> 
      <br> 
<font color="red" size="+1">說明:如果有多個查詢條件,中間用</font><font size="+2">+</font><font color="red" size="+1">隔開。如:1+2+3+4...</font></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
下面的代碼是全文檢索索引主體javabean的代碼:newsSearch.java 
package NEWS; 
import java.sql.*; 
import java.lang.*; 
import java.text.*; 
import java.util.*; 
import java.io.*; 
import java.util.regex.*; 
import DBstep.iDBManager2000;//資料庫操作的bean 
public class newsSearch { 
private String filePath=null;//主體新聞存放的目錄 
private String keyWord=null;//查詢關鍵字 
private Vector news = new Vector();//存放合格結果 
public newsSearch() { } 
public void setFilePath(String s) { 
this.filePath=s; 
} 
public void setKeyWord(String s) { 
this.keyWord=s; 
} 
public Vector getResult() { 
return news; 
} 
public void search() { 
//開啟資料庫 
ResultSet result=null; 
String mSql=null; 
PreparedStatement prestmt=null; 
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000(); 
DbaObj.OpenConnection(); 
try { 
//檢索所有的新聞 
mSql="select * from t_news_detail order by release_time desc"; 
result=DbaObj.ExecuteQuery(mSql); 
while(result.next()) 
{ 
String id=result.getString("id"); 
String title=result.getString("title"); 
String release_time=result.getString("release_time"); 
String news_type=result.getString("type"); 
String content=result.getString("content"); 
String man_add=result.getString("man_add"); 
//按行讀檔案 
String trace=filePath+content+".html"; 
FileReader myFileReader=new FileReader(trace); 
BufferedReader myBufferedReader=new BufferedReader(myFileReader); 
String myString=null; 
String resultString=new String(); 
while((myString=myBufferedReader.readLine())!=null) 
{ 
resultString=resultString+myString; 
} 
//去掉多餘字元 
HtmlEncode.HtmlEncode Html=new HtmlEncode.HtmlEncode();//這個bean去掉多餘的字元,新聞是自己產生的檔案,可以盡量多的刪除多餘字元 
resultString=Html.TextEncode(resultString); 
myFileReader.close(); 
//取出查詢關鍵字 
Pattern p=null; 
Matcher m=null; 
p = Pattern.compile("\\\\\\\\+"); 
String[] a=p.split(keyWord);//把關鍵字用+分開 
//全文檢索索引 
String searchResult="1";//檢索結果 
int i; 
for(i=0;i<a.length;i++)//逐個按關鍵字尋找,如果所有的關鍵字都符合,則記錄結果 
{ 
p = Pattern.compile(a[i].toString()); 
m = p.matcher(resultString); 
if (!(m.find())) { 
searchResult="0"; 
} 
} 
//記錄合格新聞 
if(searchResult.equals("1")) 
{ 
News resultNews=new News();//存放結果的類,和資料庫的結構基本一致 
resultNews.content=content; 
resultNews.release_time=release_time; 
resultNews.type=news_type; 
resultNews.man_add=man_add; 
resultNews.title=title; 
news.addElement(resultNews);//最後的結果集,要返回用戶端 
} 
} 
//關閉資料庫 
DbaObj.CloseConnection() ; 
/SPAN>}catch(Exception e){ 
System.out.println(e.toString()); 
} 
} 
public class News { //存放結果的類 
String content; 
String release_time; 
String type; 
String man_add; 
String title; 
public String getContent() { return this.content; } 
public String getTitle() { return this.title; } 
public String getTime() { return this.release_time; } 
public String getType() { return this.type; } 
public String getMan_add() { return this.man_add; } 
} 
} 
下面的代碼是調用的:aftsearch.jsp 
<%@ page contentType="text/html; charset=gb2312" %> 
<%@ page import="java.util.*" %> 
<% 
request.setCharacterEncoding("GB2312"); 
String keyword=request.getParameter("keyword"); //接收關鍵字 
String trace=getServletContext().getRealPath("/")+"xwxx\\\\\\\\news\\\\\\\\";//主體新聞存放路徑 
NEWS.newsSearch newsSearch=new NEWS.newsSearch();//初始化檢索的bean 
newsSearch.setFilePath(trace);//設定主體新聞路徑 
newsSearch.setKeyWord(keyword);//設定關鍵字 
newsSearch.search();//檢索 
Vector news=newsSearch.getResult();//取到結果 
%> 
<html> 
<head> 
<title>新聞搜尋</title> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
<link rel="stylesheet" href="../css/style3.css"> 
&l;script LANGUAGE="javascript"> 
function open_window(id) 
{ 
locat="./news/"+id+".html"; 
window.open(locat,"new","width=550,height=500 ,scrollbars=yes") 
} 
</script> 
</head> 
<object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"> 
<param name="Command" value="Maximize"></object> 
<body bgcolor=#F5FAF3 leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> 
<script> 
hh2.Click(); 
</script> 
<table width="621" border="0"> 
<tr> 
<td colspan=5> 
         
         
         
</font> 
</td> 
</tr> 
<tr valign="middle"> 
<td width="45%" height="22"> 
<div align="center" class = "t_header">標 題</div> 
</td> 
<td width="15%" height="22"> 
<div align="center" class = "t_header">類 別</div> 
</td> 
<td width="15%" height="22"> 
<div align="center" class = "t_header">發 布 人</div> 
</td> 
<td width="25%" height="22"> 
<div align="center" class = "t_header">發 布 時 間</div> 
</td> 
</tr> 
<tr bgcolor="#B7D79F" valign="middle"> 
<td colspan="4" height="2"></td> 
</tr> 
</table> 
<table width="624" border="0" bordercolor="#99CCFF"> 
<% 
String color=null; 
int j=0; 
if(!(news.size()==0)) { 
for (int i = 0; i < news.size(); i++) { 
j++; 
NEWS.newsSearch.News myNews=(NEWS.newsSearch.News)news.get(i); 
if(i%2==0) 
{ color="#F5FAF3"; } 
else { color="#DBF7ED"; } 
%> 
<tr bgcolor = "<%=color%>"> 
<td width="45%" height="20"> 
<img src="http://edu.cnzz.cn/NewsInfo/images/dot.gif" align = "absmiddle"> 
<a href="#" > <%=myNews.getTitle()%></a> 
</td> 
<td width="15%" height="20" align="center"> 
<%=myNews.getType()%> 
&nbs; </td> 
<td width="15%" height="20" align="center"> 
<%=myNews.getMan_add()%> 
</td> 
<td width="25%" height="20" align="center"> 
<%=myNews.getTime()%> 
</td> 
</tr> 
<% } } else{ out.println("對不起,沒有搜尋到您要尋找的新聞");} //和最前邊的else對應,判斷是否有記錄 %> 
<tr bgcolor="#B7D79F"> 
<td colspan="4" height="2"></td> 
</tr> 
<tr> 
<td colspan=4> 
<p align=right> 
   
</td> 
</tr> 
</table> 
<P align=center>                共搜尋到新聞 <%=j%> 條 
</body> 
</html>