欢迎光临
我们一直在努力

JSP中实现全文检索-JSP教程,Application/Applet

建站超值云服务器,限时71元/月

全文检索一直都是web方面的关键技术,如何在浩如烟海的信息中找到自己想要的信息是人们最关心的。鼎鼎大名的google就是一个很成功的例子,网络上的人们大部分都用google来查找自己需要的内容。全文检索主要有两个技术指标:快速和精确。前一段时间做了一个新闻系统,老板要加上全文检索的功能,想了很久才用一个不太高明的方法实现了。现在分享一下,希望是抛砖引玉吧,如果大家有更好的办法请跟在后边:)

先介绍一下我的新闻系统:数据库里存新闻的基本信息,如标题,发布人,发布时间,主体新闻的文件名。新闻主体是html格式的静态页(第一是要提高速度,减少数据库的压力。第二是数据库处理大字符串的时候会有问题。)。全文检索的思路是:先从数据库里把所有的新闻检索出来,把主体新闻找到,然后通过io操作把主体新闻读到一个字符串中。再去掉多余的东西,象html标记什么的,再用正则表达式对这个字符串查找,如果找到符合条件的信息,就记录这条新闻。最后返回所有的符合条件的新闻显示给用户。

下面这段代码是输入查询条件的代码,查询关键字用”+”隔开: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" onsubmit="return subform()">

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

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="./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()%>

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » JSP中实现全文检索-JSP教程,Application/Applet
分享到: 更多 (0)

相关推荐

  • 暂无文章