我在 JSP 中构建了一个 Web 界面,用户可以在其中键入她的查询,并且该界面记录用户的查询以及她的当前位置,并将这些作为参数传递给 Lucene 索引搜索器 SearchDB。现在我想在同一个 JSP 中按分页显示搜索结果。我在其他帖子的帮助下编写的JSP和Lucene索引搜索器的代码如下:
结果.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,java.io.*,parser.SearchDB,org.apache.lucene.analysis.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%! String myQuery; String city; String latitude; String longitude; %>
<%
myQuery=request.getParameter("myQuery");
city=request.getParameter("city");
latitude=request.getParameter("latitude");
longitude=request.getParameter("longitude");
Cookie cookies [] = request.getCookies();
Cookie myCity=null;
Cookie myLat=null;
Cookie myLong=null;
if(city!=null)
{
myCity=new Cookie("city",city);
myCity.setMaxAge(365*24*60*60);
response.addCookie(myCity);
myLat=new Cookie("latitude",latitude);
myLat.setMaxAge(365*24*60*60);
response.addCookie(myLat);
myLong=new Cookie("longitude",longitude);
myLong.setMaxAge(365*24*60*60);
response.addCookie(myLong);
}
else
{
for (int i = 0; i < cookies.length; i++)
{
if (cookies[i].getName().equals("city"))
{
myCity=cookies[i];
city=myCity.getValue();
}
else
if(cookies[i].getName().equals("latitude"))
{
myLat=cookies[i];
latitude=myLat.getValue();
}
else
if(cookies[i].getName().equals("longitude"))
{
myLong=cookies[i];
longitude=myLong.getValue();
}
}
}
SearchDB s = new SearchDB();
System.out.println("Query="+myQuery+" and City="+city);
s.searchdb(myQuery, city);
%>
<form name="frm" method="post" action="result.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td> </td>
<td><input type="text" name="myQuery" placeholder="Type here"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>Query phrase is : <%=myQuery%></p>
<p>User's city : <%=city%></p>
<p>User's latitude : <%=latitude%></p>
<p>User's longitude : <%=longitude%></p>
</body>
</html>
SearchDB.java
package parser;
public class SearchDB {
public void searchdb(String myQuery, String myCity) throws Exception
{
System.out.println("Searching in the database ...");
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
QueryParser cityQP = new QueryParser(Version.LUCENE_CURRENT, "city", analyzer);
Query cityQuery = cityQP.parse(myCity);
QueryParser titleQP = new QueryParser(Version.LUCENE_CURRENT, "title", analyzer);
Query titleQuery = titleQP.parse(myQuery);
BooleanQuery finalQuery = new BooleanQuery();
finalQuery.add(cityQuery, Occur.MUST); must occur.
finalQuery.add(titleQuery, Occur.MUST);
System.out.println("Final Query="+finalQuery.toString());
Directory dir=FSDirectory.open(new File(path of index directory"));
IndexSearcher searcher = new IndexSearcher(dir, true);
TopDocs hits = searcher.search(finalQuery, 20);
System.out.println("Number of hits="+hits.totalHits);
for(ScoreDoc match : hits.scoreDocs)
{
System.out.println(searcher.doc(match.doc).get("title")+" with score="+match.score);
System.out.println("_______");
}
searcher.close();
dir.close();
}
}
我对 Lucene 和 JSP 都是新手。我不知道应该对上面的 JAVA 和 JSP 代码进行哪些更改,以便按分页方式显示结果。所以任何帮助表示赞赏。谢谢你。