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

原创 2012年03月28日 12:22:29

<%@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>

相关文章推荐

OpenCms9学习笔记(一)——OpenCms9简介

以前没接触过opencms,接触时发现版本已经9.5了,网上大多是7.0的版本,但9.5版本有了较大的变化,一时很难找到相关的资料...

【Lucene】Apache Lucene全文检索引擎架构之搜索功能

上一节主要总结了一下Lucene是如何构建索引的,这一节简单总结一下Lucene中的搜索功能。主要分为几个部分,对特定项的搜索;查询表达式QueryParser的使用;指定数字范围内搜索;指定字符串开...
  • eson_15
  • eson_15
  • 2016年07月11日 18:08
  • 10316

Lucene5 学习笔记(2) —— 简单介绍 Lucene 搜索功能和索引的修改、删除

孔浩老师Lucene学习笔记 第3讲 Lucene索引 这一节课孔老师为我们写了一个用户创建索引的工具类IndexUtil.java。 其实我们的Lucene就是创建索引,分词,然后搜索,我们就...

Eclipse中集成webDAV进行OpenCms开发

OpenCms现在已经支持webDAV了,具体可查看opencms的web.xml中的片段,集成webDAV的目的是远程更新、同步OpenCms VFS中的文件到Eclipse中,以便于使用Eclip...

解决opencms集成的demo以及doc加载缓慢问题

刚刚安装opencms的同学可能都会遇到一个
  • logyang
  • logyang
  • 2014年09月26日 23:55
  • 657
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OpenCms集成Lucene引擎,搜索功能简单范例
举报原因:
原因补充:

(最多只允许输入30个字)