Development example: JSP in the implementation of full-text Search _ Server

Source: Internet
Author: User

Full-text Search in JSP

Full-Text Search is always the key technology in the Web, how to find the information you want is the most concerned. The famous Google is a very successful example, most people on the Internet use Google to find what they need. Full-Text search has two main technical indicators: fast and accurate. Some time ago to do a news system, the boss to add the full text search function, think for a long time before using a less sophisticated method to achieve. Now share, hope is a good idea, if we have a better way please follow:

Let me introduce my news system: The basic information of the database, such as title, publisher, release time, main news file name. The news subject is a static page in HTML format (the first is to increase the speed and reduce the pressure on the database.) The second is that the database handles large strings with problems. )。 The idea of full text search is: first from the database to retrieve all the news, the main news found, and then through IO operation of the main news read into a string. Then remove the extra stuff, like HTML tags, and then use regular expressions to find the string, and if you find the right information, record the news. The final return of all eligible news is displayed to the user.

The following code is the code to enter the query criteria, the query keyword separated by "+": search.jsp
<link rel= "stylesheet" href= "Css/style3.css" >
<title> News Search </title>
<script language= "JavaScript" >
Function Subform ()
{
if (document.zl_form.keyword.value== "")
{
Alert ("Please enter the keyword!") ");
Document.zl_form.keyword.focus ();
return false;
}
return true;
}
</script>
<body bgcolor= "#F0F6E2" >
<form name= "Zl_form" target= "_new" method= "post" action= "aftsearch.jsp" onsubmit= "Return Subform ()" >
<table width= "bgcolor=" "#F0F6E2" >
<tr>
&LT;TD colspan= "4" height= "ten" > </td>
</tr>
<tr>
&LT;TD width= "14%" > Input query Keywords:</td>
&LT;TD align= "left" width= "65%" >
<input size= "type=" "text" name= "keyword" style= "font-size:9pt" >
<input type= "Submit" name= "submit" value= "search" style= "font-size:9pt" >
</td>
</tr>
<tr>
&LT;TD colspan= "2" height= "9" align= "left" >
<br>
<font color= "Red" size= "+1" > Description: If there are multiple query conditions, the middle with </font><font size= "+2" >+</font><font color= "Red" size= "+1" > separated. such as:1+2+3+4...</font></td>
</tr> </table>
</form>
</body>
The following code is the code for the Full-text retrieval principal 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;//Database Operations Bean
public class Newssearch {
Private String filepath=null;//The contents of the main news store
Private String keyword=null;//Query keyword
Private vector news = new vector ()//Hold eligible results
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 () {
Open Database
ResultSet Result=null;
String Msql=null;
PreparedStatement Prestmt=null;
dbstep.idbmanager2000 dbaobj=new dbstep.idbmanager2000 ();
Dbaobj.openconnection ();
try {
Retrieve all the News
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");
Read files by row
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;
}
Remove extra characters
Htmlencode.htmlencode html=new Htmlencode.htmlencode ();//This bean removes the extra characters, the news is the file that you generate, and you can remove as many extra characters as possible.
Resultstring=html.textencode (resultstring);
Myfilereader.close ();
Remove Query Keywords
Pattern P=null;
Matcher M=null;
p = pattern.compile ("\\+");
String[] A=p.split (KeyWord)//To separate the key words with +
Full-Text Search
String searchresult= "1";//Search results
int i;
for (i=0;i<a.length;i++)//Search by keyword, record the result if all the keywords are met
{
p = pattern.compile (a[i].tostring ());
m = P.matcher (resultstring);
if (!) ( M.find ()) {
searchresult= "0";
}
}
Keep track of eligible news
if (Searchresult.equals ("1"))
{
News Resultnews=new News ()//The class that holds the result is basically consistent with the structure of the database
Resultnews.content=content;
Resultnews.release_time=release_time;
Resultnews.type=news_type;
Resultnews.man_add=man_add;
Resultnews.title=title;
News.addelement (resultnews);//final result set, to return to the client
}
}
Close Database
Dbaobj.closeconnection ();
}catch (Exception e) {
System.out.println (E.tostring ());
}
}
public class News {//class that holds results
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;}
}
}
The following code is called: aftsearch.jsp
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "java.util.*"%>
<%
Request.setcharacterencoding ("GB2312");
String keyword=request.getparameter ("keyword"); Receive key words
String Trace=getservletcontext (). Getrealpath ("/") + "xwxx\\news\\";//main News store path
News.newssearch newssearch=new News.newssearch ();//Initialize the retrieved bean
Newssearch.setfilepath (Trace)//Set principal news path
Newssearch.setkeyword (keyword);//Setting keywords
Newssearch.search ()//Search
Vector News=newssearch.getresult ()//Get Results
%>
<title> News Search </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>
<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>
&LT;TD colspan=5>



</font>
</td>
</tr>
<tr valign= "Middle" >
&LT;TD width= "45%" height= ">"
<div align= "center" class = "T_header" > title </div>
</td>
&LT;TD width= "15%" height= ">"
<div align= "center" class = "T_header" > Category </div>
</td>
&LT;TD width= "15%" height= ">"
<div align= "center" class = "T_header" > Publisher </div>
</td>
&LT;TD width= "25%" height= ">"
<div align= "center" class = "T_header" > Release time </div>
</td>
</tr>
<tr bgcolor= "#B7D79F" valign= "Middle" >
&LT;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"; }
%>
&LT;TR bgcolor = "<%=color%>" >
&LT;TD width= "45%" height= ">"

<a href= "#" onclick= "Open_window (<%=mynews.getcontent ()%>)" > <%=mynews.gettitle ()%></a>
</td>
&LT;TD width= "15%" height= "align=" "Center" >
<%=mynews.gettype ()%>
</td>
&LT;TD width= "15%" height= "align=" "Center" >
<%=mynews.getman_add ()%>
</td>
&LT;TD width= "25%" height= "align=" "Center" >
<%=mynews.gettime ()%>
</td>
</tr>
<%}} else{out.println ("Sorry, no search to the news you are looking for");} corresponding to the top else, to determine whether there are records%>
<tr bgcolor= "#B7D79F" >
&LT;TD colspan= "4" height= "2" ></td>
</tr>
<tr>
&LT;TD colspan=4>
<p align=right>

</td>
</tr>
</table>
<p align=center> A total of search news <%=j%> articles
</body>
0 xmlscript.src= "http://guide.pconline.com.cn/comment/commentService_js.jsp?" + (new Date ()); 0 xmlscript.src= "http://guide.pconline.com.cn/comment/commentService_js.jsp?" + (new Date ()); 0 xmlscript.src= "http://guide.pconline.com.cn/comment/commentService_js.jsp?" + (new Date ()); 0 xmlscript.src= "http://guide.pconline.com.cn/comment/commentService_js.jsp?" + (new Date ()); 0 xmlscript.src= "http://guide.pconline.com.cn/comment/commentService_js.jsp?" + (new Date ()); (Source: CSDN)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.