Full text search in JSP

xiaoxiao2021-03-05  21

The full-text search has always been a key technology in web, how to find the information you want in the information of the Hao Shihai's information is the most concerned. Ding Ding's Google is a very successful example, people on the Internet use Google to find what they need. The full text search mainly has two technical indicators: fast and accurate. I did a news system for a while, and the boss had a full-text search function, and I thought it was only used for a long time. Now share it, hope is to throw bricks, if you have a better way, please follow :)

Let me introduce my news system: Database's basic information, such as title, publisher, release time, and file name of the main news. The news main body is a static page in the HTML format (the first is to increase the speed, reduce the pressure of the database. The second is that there is a problem when the database handles large strings.). The idea of ​​full text is: first search all the news from the database, find the main news, and then read the main news to a string through the IO operation. Remove the extra thing, like HTML marks what, use the regular expression to find this string, if you find the requirements, you record this news. Finally, returns all eligible news display to the user.

The following code is the code that enters the query condition, query the keyword " " separated: Search.jsp

News Search </ Title></p> <p><script language = "javascript"></p> <p>Function Subform ()</p> <p>{</p> <p>IF (Document.zl_form.keyword.value == "")</p> <p>{</p> <p>Alert ("Please enter the keyword!");</p> <p>Document.zl_form.keyword.focus ();</p> <p>Return False;</p> <p>}</p> <p>Return True;</p> <p>}</p> <p></ script></p> <p></ hEAD></p> <p><body bgcolor = "# f0f6e2"></p> <p><form name = "ZL_FORM" target = "_ new" method = "post" action = "AftSearch.jsp" οnsubmit = "Return Subform ()"></p> <p><Table Width = "600" BGColor = "# f0f6e2"></p> <p><tr></p> <p><td color = "4" height = "10"> </ td></p> <p></ TR></p> <p><tr></p> <p><TD Width = "14%"> Enter Query Keywords: </ TD></p> <p><TD align = "left" width = "65%"></p> <p><input size = "50" type = "text" name = "keyword" style = "font-size: 9pt"></p> <p><Input Type = "Submit" name = "Submit" value = "Search" style = "font-size: 9pt"> </ td></p> <p></ TR></p> <p><tr></p> <p><TD colspan = "2" height = "9" align = "left"></p> <p><br></p> <p><font color = "red" size = " 1"> Description: If there are multiple query conditions, in the middle </ font> <font size = " 2"> <font> <font color = "red" SIZE = " 1"> Separate. Such as: 1 2 3 4 ... </ font> </ td></p> <p></ TR></p> <p></ TABLE></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>The following code is the code of the full text search main JavaBean: NewsSearch.java</p> <p>Package news;</p> <p>Import java.sql. *;</p> <p>Import java.lang. *;</p> <p>Import java.text. *;</p> <p>Import java.util. *;</p> <p>Import java.io. *;</p> <p>Import java.util.regex. *;</p> <p>Import dbstep.idbmanager2000; // database operation bean</p> <p>Public class newssearch {</p> <p>Private string filepath = null; // Sub-body news storage</p> <p>Private string keyword = null; // Query keyword</p> <p>Private vector news = new vector (); // stores the result of the eligible</p> <p>Public newssearch () {}</p> <p>Public void setfilepath (string s) {</p> <p>THIS.FILEPATH = S;</p> <p>}</p> <p>Public void setKeyword (string s) {</p> <p>THIS.KEYWORD = S;</p> <p>}</p> <p>Public Vector getResult () {</p> <p>Return news;</p> <p>}</p> <p>Public void search () {</p> <p>// Open the database</p> <p>ResultSet Result = NULL;</p> <p>String msql = NULL;</p> <p>PreparedStatement PRESTMT = NULL;</p> <p>Dbstep.idbmanager2000 dbaobj = new dbstep.idbManager2000 ();</p> <p>DBAOBJ.OpenConnection ();</p> <p>Try {</p> <p>// Retrieve all news</p> <p>MSQL = "SELECT * from T_News_detail Order by Release_time DESC";</p> <p>Result = DBAOBJ.EXECUTEQUERY (MSQL);</p> <p>While (result.next ())</p> <p>{</p> <p>String id = result.getstring ("id");</p> <p>String title = result.getstring ("Title");</p> <p>String release_time = result.getstring ("release_time");</p> <p>String news_type = result.getstring ("Type"); string content = result.getstring ("content");</p> <p>String man_add = result.getstring ("man_add");</p> <p>// Read files</p> <p>String trace = filepath content ". Html";</p> <p>FileReader myfilereader = new fileReader (trace);</p> <p>BufferedReader myBufferedReader = New BufferedReader (MyFileRead);</p> <p>String mystring = NULL;</p> <p>String resultstring = new string ();</p> <p>While (mystring = mybufferedreader.readline ())! = null)</p> <p>{</p> <p>Resultstring = resultString mystring;</p> <p>}</p> <p>/ / Remove extra characters</p> <p>Htmlencode.htmlencode html = new htmlencode.htmlencode (); // This bean removes excess characters, news is the file you generated, you can delete extra characters as much as possible</p> <p>Resultstring = HTML.TEXTENCODE (ResultString);</p> <p>myfileReader.close ();</p> <p>// Remove the query keyword</p> <p>Pattern P = NULL;</p> <p>Matcher M = NULL;</p> <p>P = pattern.Compile ("/ ");</p> <p>String [] a = p.split (keyword); // Use the keyword separate</p> <p>//Full Text Search</p> <p>String searchResult = "1"; // Retrieve results</p> <p>INT I;</p> <p>For (i = 0; i <a.length; i ) // Press keyword to look up, if all keywords are in line with, record results</p> <p>{</p> <p>P = pattern.Compile (a [i] .tostring ());</p> <p>m = p.matcher (resultString);</p> <p>IF (! (m.find ()))) {</p> <p>SearchResult = "0";</p> <p>}</p> <p>}</p> <p>// Record the news that meets the conditions</p> <p>IF (SearchResult.equals))</p> <p>{</p> <p>News resultnews = new news (); // store the result of the result of the result, and the structure of the database is basically consistent</p> <p>Resultnews.Content = Content;</p> <p>Resultnews.release_time = release_time;</p> <p>Resultnews.Type = news_type;</p> <p>Resultnews.man_add = man_add;</p> <p>Resultnews.title = title;</p> <p>News.addeElement (Resultnews); // Last result set, return to the client</p> <p>}</p> <p>}</p> <p>// Close the database</p> <p>DBAOBJ.CLOSECONNECTION ();</p> <p>} catch (exception e) {</p> <p>System.out.println (E.TOString ());</p> <p>}</p> <p>}</p> <p>Public class news {// store the result of the result</p> <p>String content;</p> <p>String release_time;</p> <p>String Type;</p> <p>String man_add;</p> <p>String Title;</p> <p>Public string getContent () {return this.content;} public string gettitle () {return this.title;</p> <p>Public string gettime () {return this.release_time;}</p> <p>Public string gettype () {return this.type;</p> <p>Public string getman_add () {return this.man_add;}</p> <p>}</p> <p>}</p> <p>The following code is called: AFTSEARCH.JSP</p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p><% @ Page Import = "java.util. *"%></p> <p><%</p> <p>Request.SetCharacterencoding ("GB2312");</p> <p>String Keyword = Request.getParameter ("keyword"); // Receive keyword</p> <p>String trace = getServletContext (). GetRealPath ("/") "xwxx / news /"; // main news storage path</p> <p>News.newssearch newssearch = new news.newss (); // Initialization retrieval bean</p> <p>Newssearch.setFilePath (trace); // Set the main news path</p> <p>Newssearch.setKeyword (keyword); // Set keyword</p> <p>Newssearch.Search (); // Retrieve</p> <p>Vector news = news.getResult (); // Take the result</p> <p>%></p> <p><html></p> <p><HEAD></p> <p><Title> News Search </ Title></p> <p><Meta http-equiv = "cache-control" Content = "no-cache"></p> <p><link rel = "stylesheet" href = "../ CSS / style3.css"></p> <p><script language = "javascript"></p> <p>Function Open_Window (ID)</p> <p>{</p> <p>Locat = "./ news /" ID ". HTML";</p> <p>Window.open (Locat, "New", "Width = 550, Height = 500, Scrollbars = YES")</p> <p>}</p> <p></ script></p> <p></ hEAD></p> <p><Object ID = HH2 ClassID = "CLSID: ADB880A6-D8FF-11CF-9377-00AA003B7A11"></p> <p><param name = "Command" value = "maximize"> </ object></p> <p><body bgcolor = # f5faf3 leftmargin = "0" TopMargin = "0" marginwidth = "0" marginheight = "0"></p> <p><script></p> <p>hh2.click ();</p> <p></ script></p> <p><table width = "621" border = "0"> <TR></p> <p><TD COLSPAN = 5></p> <p></ font></p> <p></ td></p> <p></ TR></p> <p><tr valign = "middle"></p> <p><TD width = "45%" Height = "22"></p> <p><div align = "center" class = "t_header"> Title </ div></p> <p></ td></p> <p><TD Width = "15%" Height = "22"></p> <p><div align = "center" class = "t_header"> category </ div></p> <p></ td></p> <p><TD Width = "15%" Height = "22"></p> <p><div align = "center" class = "t_header"> send people </ div></p> <p></ td></p> <p><td width = "25%" Height = "22"></p> <p><div align = "center" class = "t_header"> Pan time </ div></p> <p></ td></p> <p></ TR></p> <p><tr bgcolor = "# b7d79f" valign = "middle"></p> <p><td color = "4" height = "2"> </ td></p> <p></ TR></p> <p></ TABLE></p> <p><Table Width = "624" border = "0" bordercolor = "# 99ccff"></p> <p><%</p> <p>String color = NULL;</p> <p>INT j = 0;</p> <p>IF (! (news.size () == 0)) {</p> <p>For (int I = 0; i <news.size (); i ) {</p> <p>J ;</p> <p>News.newssearch.news mynews = (news.newssearch.news) news.get (i);</p> <p>IF (i% 2 == 0)</p> <p>{color = "# f5faf3";</p> <p>Else {color = "# dbf7ed";</p> <p>%></p> <p><tr bgcolor = "<% = color%>"></p> <p><td width = "45%" Height = "20"></p> <p><img src = "./ images / dot.gif" align = "absmiddle"></p> <p><a href="#" onclick="open_window(< %=MYNEWS.GETCONTENT ()%)> <% = mynews.gettitle ()%> </a> </ td></p> <p><td width = "15%" height = "20" align = "center"></p> <p><% = mynews.gettype ()%></p> <p></ td></p> <p><td width = "15%" height = "20" align = "center"></p> <p><% = mynews.getman_add ()%></p> <p></ td></p> <p><TD width = "25%" height = "20" align = "center"></p> <p><% = mynews.gettime ()%></p> <p></ td></p> <p></ TR></p> <p><%}} else {Out.println ("Sorry, no news you want to find");} // and the forefront ELSE corresponds to whether there is record%></p> <p><tr bgcolor = "# b7d79f"></p> <p><td color = "4" height = "2"> </ td></p> <p></ TR></p> <p><tr></p> <p><TD COLSPAN = 4></p> <p><p align = right></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p><P align = center> A total of news <% = j%> strip</p> <p></ body></p> <p></ html></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-36441.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="36441" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.080</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'V_2BbkUvMhTYLu3GyETeLtrfq_2Fy9_2Bc4nyAerEighI45SOM_2B9_2FtsAsUOPtCSlth12MZ2UIrpFwLCyjTAnne9P7WkA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>