Full-text search in JSP
Full-text search has always been a key WEB technology. People are most concerned about how to find the information they want in the vast amount of information. 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 major technical indicators: fast and accurate. Some time ago, I made a news system. The boss had to add the full-text retrieval function and thought about it for a long time before using a very clever method. Now let's share it with you. I hope you can introduce it to others. If you have a better solution, please stay behind :)
First, let's introduce my news system: the basic information of news stored in the database, such as the title, publisher, release time, and name of the main news file. 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 problem occurs when the database processes large strings .). The idea of full-text search is: first retrieve all the news from the database, find the news of the subject, and then read the news of the subject into a string through Io operations. Remove unnecessary things, such as HTML tags, and use regular expressions to search for the string. If the matching information is found, record the news. Finally, all qualified news is returned to the user.
The following section Code Is the code that inputs the query conditions. The query keywords are separated by "+": search. jsp
<HTML>
<Head>
<LINK rel = "stylesheet" href = "CSS/style3.css">
<Title> news search </title>
<Script language = "JavaScript">
Function subform ()
{
If (document. zl_form.keyword.value = "")
{
Alert ("enter a keyword! ");
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%"> enter the query Keyword: </TD>
& Lt; TD align = "Left" width = "65%" & gt;
<Input size = "50" type = "text" name = "keyword" style = "font-size: 9pt">
<Input type = "Submit" name = "Submit" value = "Search" style = "font-size: 9pt">
</TD>
</Tr>
<Tr>
<TD colspan = "2" Height = "9" align = "Left">
<Br>
<Font color = "red" size = "+ 1"> Note: if multiple query conditions exist, separated by </font> <font size = "+ 2"> + </font> <font color = "red" size = "+ 1">. Example: 1 + 2 + 3 + 4... </font> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
The following code is the full-text retrieval of the main JavaBean code: 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 for database operations
Public class newssearch {
Private string filepath = NULL; // The directory where the main news is stored
Private string keyword = NULL; // query the keyword
Private vector news = new vector (); // store the qualified 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 the database
Resultset result = NULL;
String msql = NULL;
Preparedstatement prestmt = NULL;
Dbstep. idbmanager2000 dbaobj = new dbstep. idbmanager2000 ();
Dbaobj. openconnection ();
Try {
// Retrieve all 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 (); // remove unnecessary characters from this bean. News is a self-generated file. You can delete as many redundant characters as possible.
Resultstring = html. textencode (resultstring );
Myfilereader. Close ();
// Retrieve the query keyword
Pattern P = NULL;
Matcher M = NULL;
P = pattern. Compile ("\\+ ");
String [] A = P. Split (keyword); // separate keywords with +
// Full-text search
String searchresult = "1"; // search result
Int I;
For (I = 0; I <A. length; I ++) // search by keywords one by one. If all the keywords match, the results are recorded.
{
P = pattern. Compile (A [I]. tostring ());
M = P. matcher (resultstring );
If (! (M. Find ())){
Searchresult = "0 ";
}
}
// Record qualified news
If (searchresult. Equals ("1 "))
{
News resultnews = new news (); // class for storing results, which is basically consistent with the database structure
Resultnews. content = content;
Resultnews. release_time = release_time;
Resultnews. type = news_type;
Resultnews. man_add = man_add;
Resultnews. Title = title;
News. addelement (resultnews); // The final result set. The client is returned.
}
}
// Close the database
Dbaobj. closeconnection ();
} Catch (exception e ){
System. Out. println (E. tostring ());
}
}
Public class news {// class for storing 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"); // receives the keyword
String trace = getservletcontext (). getrealpath ("/") + "xwxx \ news \"; // subject news storage path
News. newssearch = new news. newssearch (); // initialize the retrieved bean.
Newssearch. setfilepath (TRACE); // sets the subject news path.
Newssearch. setkeyword (keyword); // you can specify a keyword.
Newssearch. Search (); // search
Vector news = newssearch. getresult (); // obtain the result
%>
<HTML>
<Head>
<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>
</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">
& Lt; TD width = "45%" Height = "22" & gt;
<Div align = "center" class = "t_header"> subject </div>
</TD>
& Lt; TD width = "15%" Height = "22" & gt;
<Div align = "center" class = "t_header"> class </div>
</TD>
& Lt; TD width = "15%" Height = "22" & gt;
<Div align = "center" class = "t_header"> publisher </div>
</TD>
& Lt; TD width = "25%" Height = "22" & gt;
<Div align = "center" class = "t_header"> distribution time </div>
</TD>
</Tr>
<Tr bgcolor = "# b7d79f" valign = "Middle">
<TD colspan = "4" Height = "2"> </TD>
</Tr>
</Table>
<Table width = "624" border = "0" bordercolor = "#99 CCFF">
<%
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 %>">
& Lt; TD width = "45%" Height = "20" & gt;
<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 ("sorry, no news is found");} // corresponds to the else on the frontend to determine whether a record exists. %>
<Tr bgcolor = "# b7d79f">
<TD colspan = "4" Height = "2"> </TD>
</Tr>
<Tr>
<TD colspan = 4>
<P align = right>
</TD>
</Tr>
</Table>
<P align = center> News <% = J %> found in total
</Body>
</Html>
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)