《Lucene in action》中推荐的Lucene分页方式

 方法一:

private List processHits(Hits hits,int startIndex,int endIndex)throws Exception{
  
if(endIndex>=hits.length())
   endIndex
=hits.length()-1;
  List docs
=new ArrayList();
  
for(int i=startIndex;i<=endIndex;i++){
   Document doc
=hits.doc(i);
   Map docMap
=new HashMap();
   docMap.put(
"id",doc.getField("id").stringValue());
   docMap.put(
"name",doc.getField("name").stringValue());
   docMap.put(
"price",doc.getField("price").stringValue());
   docs.add(docMap);
  }
  
return docs;
 }

 

 方法二:

做一个搜索引擎就要像个样子,做的专业一些才好,因此在分页处理时也采用类似百度和google的分页方式。

       对于结果集的处理,大家各有办法,总结起来就是缓存和重新查询两种方式,lucene推荐第二种,如果使用缓存的方式感觉控制资源的释放比较困难,在并发性较多的情况下系统可能资源耗尽,因此我们这里采用lucene推荐的做法实现类似百度和google的分页效果。

      终于调试通过了,大家可以直接使用了,哪里不清楚可以给我留言。

    <%@ page contentType="text/html; charset=gb2312" buffer="none"%>
<%@ page import="java.util.*"%>

<%
//初始化全局变量
   int totalNum = 0;    //总共记录条数
   int numPerPage = 0;    //每页显示记录条数
   int maxPageNum = 0;   //本次搜索最大页数
   int curPage = 1;      //当前页
   int startLocation = 0;    //起始位置
  
   Vector searchresult = (Vector) session.getAttribute("searchresult");
   
    numPerPage = ((Integer)session.getAttribute("setPageNum")).intValue();

   totalNum = searchresult.size();
   maxPageNum=((totalNum%numPerPage) == 0)?totalNum/numPerPage:(totalNum/numPerPage+1);
   curPage=startLocation/numPerPage + 1;
  
%>

<html>
    <head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">  
   <title>查询结果页面</title>
    <STYLE>
    BODY { MARGIN: 6px 0px 0px; COLOR: #000; BACKGROUND-COLOR: #fff; FONT-FAMILY: arial }
    TABLE { BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px }
    TD { FONT-SIZE: 11pt; LINE-HEIGHT: 18px; FONT-FAMILY: arial; text-align:center}
    .p { PADDING-LEFT: 18px; FONT-SIZE: 14px; WORD-SPACING: 4px }
    #ft { CLEAR: both; BACKGROUND: #e6e6e6; LINE-HEIGHT: 20px; TEXT-ALIGN: center; FONT-SIZE: 12px; COLOR: #77c; FONT-FAMILY: Arial }
   </STYLE>
</head>
<%
  
   if(request.getParameter("startLocation")!=null)
       {
          startLocation = Integer.parseInt(request.getParameter("startLocation"));     //取当前页码
          System.out.println("startLocation ="+startLocation);
          curPage = ((startLocation%numPerPage) == 0)?(startLocation/numPerPage):(startLocation/numPerPage+1);
          curPage=curPage+1;
          System.out.println("curPage ="+curPage);
    }
  
%>
<body>
<form method="POST" >
   <table border="0.5" width="100%" >
     <tr bgcolor="#72963D">
      <td align="center" class="colsTitle" nowrap height="19">
       查询结果
      </td>
     </tr>
   <%
      if(curPage<maxPageNum){
      System.out.println("startLocation="+startLocation);
         for(int i=startLocation; i<startLocation+numPerPage; i++){
             String result = (String) searchresult.elementAt(i);
             out.println("<tr bgcolor=\"#F1F0ED\">");
             out.println("<td align=\"center\" nowrap height=\"19\">");
             out.println(result);
       out.println("</td>");
       out.println("</tr>");
         }
      }else{
         System.out.println("totalNum="+totalNum);
         for(int i=startLocation; i<totalNum; i++){
             String result = (String) searchresult.elementAt(i);
             out.println("<tr bgcolor=\"#F1F0ED\">");
             out.println("<td align=\"center\" nowrap height=\"19\">");
             out.println(result);
       out.println("</td>");
       out.println("</tr>");
         }
      }
    //searchresult.clear();
   %>
   <tr><td>
   <hr align="center" color="#990066" width="500">
   </td>
   <tr>
   <td>
   <%
   if (curPage > 1 ) {
            out.println("<a href='searchresult.jsp?startLocation="+(startLocation-numPerPage));
            out.println("'>上一页</a>");
        }
        if(maxPageNum > 10) {
            int totalNumShow = curPage + 9;
            int startLocationPage = 0;
            if( totalNumShow > maxPageNum) {
                totalNumShow = maxPageNum;
            }
            if( maxPageNum > 11 && curPage > 11) {
                startLocationPage = curPage - 11;
            }
            for(int i=startLocationPage;i<totalNumShow;i++) {
                  if(i+1 == curPage) {
                      %>&nbsp;[<%=i+1%>]&nbsp;<%
                  } else {
                      %><a href="searchresult.jsp?startLocation=<%=i*numPerPage%>">&nbsp;<%=i+1 %>&nbsp;</a><%
                  }
            }
       }
       else {
           for( int i=0;i<maxPageNum;i++) {
                if( i+1 == curPage) {
                    %>&nbsp;[<%=i+1 %>]&nbsp;<%
                } else {
                    %><a href="searchresult.jsp?startLocation=<%=i*numPerPage%>">&nbsp;<%=i+1 %>&nbsp;</a><%
                }
          }
      }

      if ( curPage < maxPageNum) {
           out.println("<a href='searchresult.jsp?startLocation="+(startLocation+numPerPage));
           out.println("'>下一页</a>");
      }
   %>
   </td>
   </tr>
  
   </table>
   </form>
</body>
</html>

你可能感兴趣的:(jsp,搜索引擎,百度,Google,Lucene)