OpenCms集成Lucene引擎,搜索功能简单范例

<%@page language="java" pageEncoding="UTF-8"%>
<%@page import="org.opencms.jsp.CmsJspActionElement"%>
<%@page import="org.opencms.main.OpenCms"%>
<%@page import="org.opencms.search.CmsSearchManager"%>
<%@page import="java.util.ListIterator"%>
<%@page import="java.util.List"%>
<%@page import="org.opencms.jsp.CmsJspXmlContentBean"%>
<%@page import="org.opencms.search.CmsSearchResult"%>
<%@page import="org.opencms.file.CmsObject"%>
<%@page import="com.oc.OCUtil"%>
<%@page import="org.opencms.jsp.I_CmsXmlContentContainer"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.opencms.file.CmsResource"%>
<%@page import="org.opencms.xml.content.CmsXmlContent"%>
<%@page import="org.opencms.xml.content.CmsXmlContentFactory"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Map"%>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻搜索页</title>
<link href="css/map_slice.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/picture.js"></script>
<!--IE6 png Transparent-->
<!--[if IE 6]>
<script src="js/DD_belatedPNG_0.0.8a.js"></script>
<script>
  /*fix png */
  DD_belatedPNG.fix('*');
/*add png class*/
</script>
<![endif]-->
</head>

<%
request.getSession().setAttribute("navStr","新闻");
%>
<body>
<cms:include page="/system/modules/com.hsinghai/pages/common/second/top.jsp" />
<div class="contant">
   <cms:include page="/system/modules/com.hsinghai/pages/common/second/left.jsp" />
   <%
org.opencms.jsp.CmsJspActionElement cms = new CmsJspActionElement(
   pageContext, request, response);
CmsSearchManager searchManager = OpenCms.getSearchManager();
%>

<jsp:useBean id="search" scope="request"
class="org.opencms.search.CmsSearch">
<jsp:setProperty name="search" property="*" />
<%
  search.init(cms.getCmsObject());
%>
</jsp:useBean>
<%
  String searchKeys="";
  if (request.getParameter("searchKeys") != null) {
   request.getSession().setAttribute("intoal2",null);
   searchKeys = new String(request.getParameter("searchKeys").getBytes("ISO-8859-1"),"UTF-8");
   search.setQuery(searchKeys);
  }
%>
<%
  int pagesize=12;
  search.setIndex("hsinghai_search_news");
 
  int intoal=0;
  if(request.getSession().getAttribute("intoal2")!=null){
   intoal=Integer.parseInt(request.getSession().getAttribute("intoal2").toString());
  }else{
   search.setMatchesPerPage(1000000);
   intoal=search.getSearchResult().size();
   request.getSession().setAttribute("intoal2",intoal);
  }
  int intoalPage=intoal%pagesize==0?intoal/pagesize:intoal/pagesize+1;
%>
   <div class="news_search">
     <div class="week_info">
  <div class="map_list"><a>位置:首页 > 新闻 > 搜索结果页</a></div>
       <ul>
        <%
            search.setMatchesPerPage(pagesize);
      List result = search.getSearchResult();
   if (result == null) {
    if (search.getLastException() != null) {
    %>
    <%=search.getLastException().toString()%>
    <%
    }
   } else {
    ListIterator iterator = result.listIterator();
    CmsJspXmlContentBean cmsContent = new CmsJspXmlContentBean(
       pageContext, request, response);
    while (iterator.hasNext()) {
     CmsSearchResult entry = (CmsSearchResult) iterator.next();
     CmsObject cmsobj = cms.getCmsObject();
     String pageName = OCUtil.delStr(cms
       .getRequestContext()
       .removeSiteRoot(entry.getPath()), "/sites/Hsinghai");
     I_CmsXmlContentContainer contentContainer = cmsContent
       .contentload("singleFile", pageName, true);
     Iterator it = contentContainer.getCollectorResult()
       .iterator();
     if (it.hasNext()) {
      CmsResource resource = (CmsResource) it.next();
      CmsXmlContent content = CmsXmlContentFactory.unmarshal(
        cmsobj, resource, request);
      String Name = content.getStringValue(cmsobj, "Name", cms
        .getRequestContext().getLocale());
      String EditTime = content.getStringValue(cmsobj, "EditTime",
        cms.getRequestContext().getLocale());

      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Date d = new Date(Long.parseLong(EditTime));
      %>
       <li><a href="<cms:link><%=pageName %></cms:link>"><h1><%=OCUtil.subStr(Name,27) %><br /></h1></a><p>&nbsp;&nbsp;<%=df.format(d) %></p></li>
      <%
     }
    }
   }
  %>
       </ul>
        <%
        if(intoal>pagesize){
        %>
          <div class="search_page">
        <%
   if (search.getPreviousUrl() != null) {
   %>
   <a href="<%=cms.link(search.getPreviousUrl())%>"><img src="images/jt1.jpg" width="7" height="8" /></a>
   <%
   }
   %>
   <%=search.getSearchPage() %>/<%=intoalPage %>
   <%
   if (search.getNextUrl() != null) {
   %>
   <a href="<%=cms.link(search.getNextUrl())%>"><img src="images/jt2.jpg" width="7" height="8" /></a>
   <%
   }
   %>
        </div>
  <%
        }
        %>
     </div>
   </div>
   <div class="map_right">
    <cms:include page="/system/modules/com.hsinghai/pages/common/second/rili.jsp" />
     <div class="map_classic">
       <cms:include page="/system/modules/com.hsinghai/pages/common/first/nav_two.jsp" />
     </div>
   </div>
     <div class="map_icon"><img src="images/map01.png" width="74" height="54" /></div>
</div>
<div style="height:30px;"></div>
</body>
</html>

你可能感兴趣的:(OpenCMS)