148补尚学堂科技张志宇lucene构建一个简单的WEB搜索程序文档格式.docx
《148补尚学堂科技张志宇lucene构建一个简单的WEB搜索程序文档格式.docx》由会员分享,可在线阅读,更多相关《148补尚学堂科技张志宇lucene构建一个简单的WEB搜索程序文档格式.docx(39页珍藏版)》请在冰豆网上搜索。
%=_title%>
/a>
/td>
2.解决摘要中有’<
’等字符的问题
DataFilter.java
packagecom.bjsxt.lucene.util;
publicclassDataFilter{
publicstaticfinalStringescapeHTMLTags(Stringsourcestr){
if(sourcestr==null){
return"
;
}
sourcestr=sourcestr.replaceAll("
\\x26"
"
&
amp;
//&
\\x3c"
lt;
//<
\\x3e"
gt;
//>
\\x09"
nbsp;
//tab键
\\x20"
//空格
\\x22"
quot;
//"
\r\n"
br>
//回车换行
\r"
//回车
\n"
//换行
returnsourcestr;
}
String_summary=doc.get("
summary"
%=DataFilter.escapeHTMLTags(_summary)%>
3.登陆/注销
Index.jsp
%@pagelanguage="
java"
import="
java.util.*"
pageEncoding="
GB18030"
%>
html>
head>
metahttp-equiv="
Content-Type"
content="
text/html;
charset=gb2312"
title>
搜索系统<
/title>
script>
if(top.location!
=self.location){
top.location=self.location;
/script>
/head>
framesetrows="
80,*"
cols="
*"
frameborder="
no"
border="
0"
framespacing="
<
framesrc="
top.jsp"
name="
top"
scrolling="
No"
noresize="
noresize"
id="
framesetcols="
162,*"
left.jsp"
left"
searchForm.jsp"
main"
/frameset>
noframes>
body>
/body>
/noframes>
/html>
4.解决缓存问题
include.jsp
%
response.addHeader("
Cache-Control"
no-cache"
Expires"
Thu,01Jan197000:
00:
01GMT"
5.改变默认布尔逻辑
SearchBean.java
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
6.按照文档得分进行排序
searchForm.jsp
formname="
searchForm"
action="
results.jsp"
onsubmit="
returncheckdata();
关键词:
inputtype=textname=query>
p>
每页显示<
inputtype=textname=maxresultsvalue=10size=2>
条
排序:
selectname=fieldSorted>
<
option>
/option>
optionvalue="
RELEVANCE"
得分<
size"
大小<
/select>
inputtype=submitvalue="
提交"
BR>
inputtype=hiddenname=startvalue=0>
/form>
%@includefile="
include.jsp"
%>
functioncheckdata(){
if(!
check_empty(searchForm.query.value)){
alert("
不能为空"
returnfalse;
returntrue;
%
intstart=0;
try{
start=Integer.parseInt(request.getParameter("
start"
));
}catch(Exceptione){}
intmaxresults=10;
maxresults=Integer.parseInt(request.getParameter("
maxresults"
inputtype=textname=queryvalue="
%=request.getParameter("
query"
)%>
>
inputtype=hiddenname=startvalue=<
inputtype=hiddenname=maxresultsvalue="
%if(request.getParameter("
fieldSorted"
).equals("
))out.print("
selected"
//Stringindex="
D:
\\share\\TOOLS\\apache-tomcat-5.5.17\\apache-tomcat-5.5.17\\webapps\\index"
Stringindex="
C:
\\tomcat\\index_cn"
StringqueryStr=request.getParameter("
StringfieldSorted=request.getParameter("
Stringreversed=request.getParameter("
reversed"
IndexSearchersearcher=newIndexSearcher(index);
//TermQueryquery=newTermQuery(newTerm("
queryStr));
QueryParserparser=newQueryParser("
newMMAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
Queryquery=parser.parse(queryStr);
Sortsort=null;
if(fieldSorted.equals("
)){
sort=Sort.RELEVANCE;
Hitshits=searcher.search(query,sort);
总共找到<
%=hits.length()%>
条记录
divid=gridData>
tablecellpadding="
5"
cellspacing="
1"
tr>
序号<
路径<
/tr>
if(hits.length()>
0){
System.out.println(hits.length());
for(inti=start;
i<
start+maxresults;
i++){
if(i>
=hits.length())break;
Documentdoc=hits.doc(i);
String_summary=doc.get("
%>
%=i+1%>
%=hits.score(i)%>
%}
/table>
/div>
ahref="
javascript:
firstPage();
首页<
%if(start-maxresults>
=0){%>
prePage();
前一页<
%}else{%>
前一页
%}%>
%if(start+maxresults<
hits.length()){%>
nextPage();
后一页<
后一页
lastPage();
尾页<
scripttype="
text/javascript"
functionfirstPage(){
searchForm.start.value="
searchForm.submit();
functionprePage(){
%=start-maxresults%>
functionnextPage(){
%=start+maxresults%>
functionlastPage(){
%=hits.length()/maxresults*maxresults%>
7.加入排序功能(size)
确认建立索引的时候,size字段已经加入!
selectname=reversed>
true"
降序<
false"
升序<
functiondoSubmit(){
searchForm.start.value=0;
inputtype=textname=startvalue=<
inputtype=buttononclick="
doSubmit()"
value="
)){
if(reversed.equals("
)){
sort=newSort("
true);
}else{
false);
}
elseif(fieldSorted.equals("
//127.0.0.1/a