您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 148_补_尚学堂科技_张志宇_lucene_构建一个简单的WEB搜索程序
148_补_Lucene_构建一个简单的WEB搜索程序1.搜索结果加上链接results.jspString_title=doc.get(title);String_path=doc.get(path);_path=_path.replaceAll(E:\\\\app\\\\develop\\\\java\\\\api\\\\html_zh_CN\\\\html\\\\zh_CN\\\\api\\\\,);_path=_path.replace('\\','/');_path=……tdatarget=_blankhref=%=_path%%=_title%/a/td2.解决摘要中有’’等字符的问题DataFilter.javapackagecom.bjsxt.lucene.util;publicclassDataFilter{publicstaticfinalStringescapeHTMLTags(Stringsourcestr){if(sourcestr==null){return;}sourcestr=sourcestr.replaceAll(\\x26,&);//&sourcestr=sourcestr.replaceAll(\\x3c,<);//sourcestr=sourcestr.replaceAll(\\x3e,>);//sourcestr=sourcestr.replaceAll(\\x09, );//tab键sourcestr=sourcestr.replaceAll(\\x20, );//空格sourcestr=sourcestr.replaceAll(\\x22,");//sourcestr=sourcestr.replaceAll(\r\n,br);//回车换行sourcestr=sourcestr.replaceAll(\r,br);//回车sourcestr=sourcestr.replaceAll(\n,br);//换行returnsourcestr;}}results.jspString_summary=doc.get(summary);……td%=DataFilter.escapeHTMLTags(_summary)%/td3.登陆/注销Index.jsp%@pagelanguage=javaimport=java.util.*pageEncoding=GB18030%htmlheadmetahttp-equiv=Content-Typecontent=text/html;charset=gb2312title搜索系统/titlescriptif(top.location!=self.location){top.location=self.location;}/script/headframesetrows=80,*cols=*frameborder=noborder=0framespacing=0framesrc=top.jspname=topscrolling=Nonoresize=noresizeid=topframesetcols=162,*frameborder=noborder=0framespacing=0framesrc=left.jspname=leftscrolling=Nonoresize=noresizeid=leftframesrc=searchForm.jspname=mainid=main/frameset/framesetnoframesbody/body/noframes/html4.解决缓存问题include.jsp%response.addHeader(Cache-Control,no-cache);response.addHeader(Expires,Thu,01Jan197000:00:01GMT);%5.改变默认布尔逻辑SearchBean.javaparser.setDefaultOperator(QueryParser.AND_OPERATOR);6.按照文档得分进行排序searchForm.jspformname=searchFormaction=results.jsponsubmit=returncheckdata();关键词:inputtype=textname=queryp每页显示inputtype=textname=maxresultsvalue=10size=2条p排序:selectname=fieldSortedoption/optionoptionvalue=RELEVANCE得分/optionoptionvalue=size大小/option/selectpinputtype=submitvalue=提交BRinputtype=hiddenname=startvalue=0/formresults.jsp%@pagelanguage=javaimport=java.util.*pageEncoding=GB18030%%@includefile=include.jsp%scriptfunctioncheckdata(){if(!check_empty(searchForm.query.value)){alert(不能为空);returnfalse;}returntrue;}/script%intstart=0;try{start=Integer.parseInt(request.getParameter(start));}catch(Exceptione){}intmaxresults=10;try{maxresults=Integer.parseInt(request.getParameter(maxresults));}catch(Exceptione){}%formname=searchFormaction=results.jsponsubmit=returncheckdata();关键词:inputtype=textname=queryvalue=%=request.getParameter(query)%inputtype=hiddenname=startvalue=%=request.getParameter(start)%inputtype=hiddenname=maxresultsvalue=%=request.getParameter(maxresults)%selectname=fieldSortedoption/optionoptionvalue=RELEVANCE%if(request.getParameter(fieldSorted).equals(RELEVANCE))out.print(selected);%得分/optionoptionvalue=size%if(request.getParameter(fieldSorted).equals(size))out.print(selected);%大小/option/selectinputtype=submitvalue=提交/form%//Stringindex=D:\\share\\TOOLS\\apache-tomcat-5.5.17\\apache-tomcat-5.5.17\\webapps\\index;Stringindex=C:\\tomcat\\index_cn;StringqueryStr=request.getParameter(query);StringfieldSorted=request.getParameter(fieldSorted);Stringreversed=request.getParameter(reversed);IndexSearchersearcher=newIndexSearcher(index);//TermQueryquery=newTermQuery(newTerm(title,queryStr));QueryParserparser=newQueryParser(title,newMMAnalyzer());parser.setDefaultOperator(QueryParser.AND_OPERATOR);Queryquery=parser.parse(queryStr);Sortsort=null;if(fieldSorted.equals(RELEVANCE)){sort=Sort.RELEVANCE;}Hitshits=searcher.search(query,sort);%总共找到%=hits.length()%条记录divid=gridDatatablecellpadding=5cellspacing=1trtd序号/tdtd路径/tdtd得分/td/tr%if(hits.length()0){System.out.println(hits.length());for(inti=start;istart+maxresults;i++){if(i=hits.length())break;Documentdoc=hits.doc(i);String_summary=doc.get(summary);String_title=doc.get(title);String_path=doc.get(path);_path=_path.replaceAll(E:\\\\app\\\\develop\\\\java\\\\api\\\\html_zh_CN\\\\html\\\\zh_CN\\\\api\\\\,);_path=_path.replace('\\','/');_path==i+1%/tdtdatarget=_blankhref=%=_path%%=_title%/a/tdtd%=hits.score(i)%/td/tr%}}%/table/divahref=javascript:firstPage();首页/a%if(start-maxresults=0){%ahref=javascript:prePage();前一页/a%}else{%前一页%}%%if(start+maxresultshits.length()){%ahref=javascript:nextPage();后一页/a%}else{%后一页%}%ahref=javascript:lastPage();尾页/ascripttype=text/javascriptfunctionfirstPage(){searchForm.start.value=0;searchForm.submit();}functionprePage(){searchForm.start.value=%=start-maxresults%;searchForm.submit();}functionnextPage(){searchForm.start.value=%=start+maxresults%;searchForm.submit();}functionlastPage(){searchForm.start.
本文标题:148_补_尚学堂科技_张志宇_lucene_构建一个简单的WEB搜索程序
链接地址:https://www.777doc.com/doc-4343707 .html