站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题Lucene索引查询分页实例

Lucene索引查询分页实例

摘要: Lucene索引查询分页实例

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="269" height="86" align="center" valign="top"> </td> <td width="415" valign="top">一、输入关键字的lucene.html

<body>
<form name="form1" method="post" action="search.jsp">
请输入关键字:<input type="text" name="keyword">
<input type="submit" name="Submit" value="提交">
</form>
</body>

效果图:


</td> </tr> <tr> <td height="20" colspan="2">
二、进行搜索和显示结果的search.jsp <%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import = "org.apache.lucene.analysis.standard.StandardAnalyzer" %>
<%@ page import="org.apache.lucene.index.IndexReader" %>
<%@ page import="org.apache.lucene.document.Document" %>
<%@ page import="org.apache.lucene.search.IndexSearcher" %>
<%@ page import="org.apache.lucene.search.Hits" %>
<%@ page import="org.apache.lucene.search.Query" %>
<%@ page import="page.Pagination" %> <%@ page import="org.apache.lucene.queryParser.QueryParser" %>
<%@ page import ="org.apache.lucene.analysis.Analyzer" %>
<%
  String queryString = request.getParameter("keyword"); 

   

     if (queryString == null||queryString.length()==0){

               out.println("搜索关键字不能为空");

                       

     }else{

         queryString=new String(queryString.getBytes("ISO8859_1"));

        String indexPath=getServletContext().getRealPath("/")+"index";   

        boolean error = false;   

        Document doc;           

       

        IndexSearcher searcher = null;        

        Query query = null;                   

        Hits hits = null;                     

       

        try {

        searcher = new IndexSearcher(IndexReader.open(indexPath));

        } catch (Exception e) {                        

                out.print("没有找到索引文件!");

                out.print(e.getMessage());  

                error = true;                               

        }

       if (error == false) {                                         

               Analyzer analyzer = new StandardAnalyzer();

               try {

                        query = QueryParser.parse(queryString, "Article_name", analyzer); 

                } catch (Exception e) {                        

                        out.print(e.getMessage());

                        error = true;                                

                                                                     

                }

        }

        if (error == false && searcher != null) {                    

                                                                   

                hits = searcher.search(query);                       

                if (hits.length() == 0) {

           out.print("对不起!没有找到你所需要的资源. ");

                   error = true;                                       

                }

        }

        if (error == false && searcher != null) {  

              out.print("搜索关键字:"+ queryString+ ""); 

              //Pagination类是网上下载的,需要传递一个向量,你可以改,这样就不用做二遍事 

              Vector list=new Vector();

              for(int i=0;i< hits.length();i++){

                doc = hits.doc(i);    

                list.add(doc);

              }   

             

              out.print("找到的资源");

              Pagination pagination = null;

              String pageNumber = request.getParameter("pageNumber");

      

              int showItemNumber = 10;

              if (pageNumber == null) {

                  pageNumber = "1";

              }

              String HTML = "";

              if (list != null && list.size() > 0) {

                 pagination = new Pagination();

                 pagination.setPageNumber(Integer.parseInt(pageNumber));

                 pagination.setShowItemNumber(showItemNumber);

                 pagination.setVisitPageURL("search.jsp?keyword="+queryString);

                 list =(Vector) pagination.interceptListByStarItemNumber(list);

                 for(int i=0;i< list.size();i++)

                 {

                   doc =(Document) list.get(i);    

                   String A_id=doc.get("Article_id");              

                   String doctitle = doc.get("Article_name");           

                   String url = doc.get("File_name")+"?id="+A_id;                  

                      

                   out.print("< a href='http://127.0.0.1:8080/cwbwebhome/"+url+"'>(★) "+doctitle+"");

                  }

                 HTML = pagination.buildHTML("600");

                 out.print(HTML);

               }

        }      

            

 }

效果如图:

三、处理分页的类Pagination.java(请下载)

</td> </tr> </table> </td> </tr> <tr>


↑返回目录
前一篇: 如何同时启动多个Tomcat服务器
后一篇: 一个学习编写连接池的好例子