隨筆 - 3, 文章 - 152, 評(píng)論 - 17, 引用 - 0
          數(shù)據(jù)加載中……

          JSP中的全文檢索

          全文檢索一直都是web方面的關(guān)鍵技術(shù),如何在浩如煙海的信息中找到自己想要的信息是人們最關(guān)心的。鼎鼎大名的GOOGLE就是一個(gè)很成功的例子,網(wǎng)絡(luò)上的人們大部分都用GOOGLE來(lái)查找自己需要的內(nèi)容。全文檢索主要有兩個(gè)技術(shù)指標(biāo):快速和精確。前一段時(shí)間做了一個(gè)新聞系統(tǒng),老板要加上全文檢索的功能,想了很久才用一個(gè)不太高明的方法實(shí)現(xiàn)了?,F(xiàn)在分享一下,希望是拋磚引玉吧,如果大家有更好的辦法請(qǐng)跟在后邊:)

          先介紹一下我的新聞系統(tǒng):數(shù)據(jù)庫(kù)里存新聞的基本信息,如標(biāo)題,發(fā)布人,發(fā)布時(shí)間,主體新聞的文件名。新聞主體是html格式的靜態(tài)頁(yè)(第一是要提高速度,減少數(shù)據(jù)庫(kù)的壓力。第二是數(shù)據(jù)庫(kù)處理大字符串的時(shí)候會(huì)有問(wèn)題。)。全文檢索的思路是:先從數(shù)據(jù)庫(kù)里把所有的新聞檢索出來(lái),把主體新聞?wù)业?,然后通過(guò)io操作把主體新聞讀到一個(gè)字符串中。再去掉多余的東西,象html標(biāo)記什么的,再用正則表達(dá)式對(duì)這個(gè)字符串查找,如果找到符合條件的信息,就記錄這條新聞。最后返回所有的符合條件的新聞顯示給用戶。

          下面這段代碼是輸入查詢條件的代碼,查詢關(guān)鍵字用”+”隔開(kāi)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("請(qǐng)輸入關(guān)鍵字!");

            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" onsubmit="return subform()">

            <table width="600" bgcolor="#F0F6E2">

              <tr>

                <td colspan="4" height="10">&nbsp; </td>

              </tr>

              <tr>

                <td width="14%">輸入查詢關(guān)鍵字:</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">

                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>

                  <font color="red" size="+1">說(shuō)明:如果有多個(gè)查詢條件,中間用</font><font size="+2">+</font><font color="red" size="+1">隔開(kāi)。如: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;//數(shù)據(jù)庫(kù)操作的bean

          public class newsSearch {

            private String filePath=null;//主體新聞存放的目錄

            private String keyWord=null;//查詢關(guān)鍵字

            private Vector news = new Vector();//存放符合條件的結(jié)果

            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() {

            //打開(kāi)數(shù)據(jù)庫(kù)

          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();//這個(gè)bean去掉多余的字符,新聞是自己生成的文件,可以盡量多的刪除多余字符

             resultString=Html.TextEncode(resultString);

             myFileReader.close();

             //取出查詢關(guān)鍵字

             Pattern p=null;

             Matcher m=null;

             p = Pattern.compile("\\+");

             String[] a=p.split(keyWord);//把關(guān)鍵字用+分開(kāi)

             //全文檢索

             String searchResult="1";//檢索結(jié)果

             int i;

             for(i=0;i<a.length;i++)//逐個(gè)按關(guān)鍵字查找,如果所有的關(guān)鍵字都符合,則記錄結(jié)果

            {

             p = Pattern.compile(a[i].toString());

             m = p.matcher(resultString);

             if (!(m.find())) {

              searchResult="0";

                }

               }

            //記錄符合條件的新聞    

            if(searchResult.equals("1")) 

            {

             News resultNews=new News();//存放結(jié)果的類,和數(shù)據(jù)庫(kù)的結(jié)構(gòu)基本一致

             resultNews.content=content;

             resultNews.release_time=release_time;

             resultNews.type=news_type;

             resultNews.man_add=man_add;

             resultNews.title=title;

             news.addElement(resultNews);//最后的結(jié)果集,要返回客戶端

             }

             }

            //關(guān)閉數(shù)據(jù)庫(kù)

            DbaObj.CloseConnection() ; 

              }catch(Exception e){

                  System.out.println(e.toString());

                }

            }

           public class News { //存放結(jié)果的類

              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; }

            }

          }

          下面的代碼是調(diào)用的:aftsearch.jsp

          <%@ page contentType="text/html; charset=gb2312" %>

          <%@ page import="java.util.*" %>

          <%

           request.setCharacterEncoding("GB2312");

           String keyword=request.getParameter("keyword");  //接收關(guān)鍵字

           String trace=getServletContext().getRealPath("/")+"xwxx\\news\\";//主體新聞存放路徑

           NEWS.newsSearch newsSearch=new NEWS.newsSearch();//初始化檢索的bean

           newsSearch.setFilePath(trace);//設(shè)置主體新聞路徑

           newsSearch.setKeyWord(keyword);//設(shè)置關(guān)鍵字

           newsSearch.search();//檢索

           Vector news=newsSearch.getResult();//取到結(jié)果

          %>

          <html>

          <head>

          <title>新聞搜索</title>

          <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">

          <link rel="stylesheet" href="../css/style3.css">

          <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>

                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                 </font>

              </td>

            </tr>

            <tr valign="middle">

              <td width="45%" height="22">

                <div align="center" class = "t_header">標(biāo) </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">發(fā) </div>

              </td>

              <td width="25%" height="22">

                <div align="center" class = "t_header">發(fā) 時(shí) </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="./images/dot.gif" align = "absmiddle">

          <a href="#"  onClick="open_window(<%=myNews.getContent()%>)"> <%=myNews.getTitle()%></a>

                       </td>

                               <td width="15%" height="20" align="center">

          <%=myNews.getType()%>

                       </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("對(duì)不起,沒(méi)有搜索到您要查找的新聞");}  //和最前邊的else對(duì)應(yīng),判斷是否有記錄 %>               

            <tr bgcolor="#B7D79F">

              <td colspan="4" height="2"></td>

            </tr>

                       <tr>

                       <td colspan=4>

          <p align=right>

          &nbsp;&nbsp;

                       </td>

                       </tr>

                 </table>

          <P align=center> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;共搜索到新聞 <%=j%>  

          </body>

          </html>

          這個(gè)方法比較原始,但是時(shí)間有限,也沒(méi)辦法了,先湊合用。我在自己機(jī)器測(cè)試:100條記錄的時(shí)候檢索時(shí)間是9秒,200條是10秒,300條是12秒。也是可以接受的:)。

          這個(gè)是基于數(shù)據(jù)庫(kù)的,大家可以很容易的擴(kuò)展到基于文件夾的,只要把文件夾下的所有文件都遍歷件出來(lái),就可以和上邊的方法一樣做全文檢索了,文件必須是文本格式的才可以。不過(guò)那樣的話速度方面就沒(méi)有保證了。

          全文檢索是一個(gè)大課題,有很多因素要考慮,還有很多技術(shù)。這個(gè)只是為我的新聞系統(tǒng)做的,也許方法很原始,這也是我能想到的最好的辦法了。大家如果有好的全文檢索的辦法請(qǐng)跟在下面,謝謝。

          posted on 2005-03-28 10:02 閱讀(228) 評(píng)論(0)  編輯  收藏 所屬分類: J2ee

          主站蜘蛛池模板: 八宿县| 增城市| 宜君县| 金阳县| 吉隆县| 大关县| 宜良县| 根河市| 宜君县| 凉城县| 全椒县| 双桥区| 鹤峰县| 澄迈县| 三穗县| 罗平县| 花垣县| 泸溪县| 林甸县| 启东市| 马公市| 榆中县| 娄底市| 南皮县| 甘孜| 齐河县| 杭锦旗| 巫溪县| 定襄县| 陆河县| 天镇县| 岳池县| 无极县| 赤壁市| 桑日县| 临猗县| 连云港市| 北碚区| 平远县| 桂林市| 浦北县|