大漠駝鈴

          置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
          Java,Php,Shell,Python,服務器運維,大數據,SEO, 網站開發、運維,云服務技術支持,IM服務供應商, FreeSwitch搭建,技術支持等. 技術討論QQ群:428622099
          隨筆 - 238, 文章 - 3, 評論 - 117, 引用 - 0
          數據加載中……

          Lucene索引查詢分頁實例

          一、輸入關鍵字的lucene.html
          <html>
          <body>
          <form name="form1" method="post" action="search.jsp">
             請輸入關鍵字:<input type="text" name="keyword">
            <input type="submit" name="Submit" value="提交">
          </form>
          </body>
          </html>

           二、進行搜索和顯示結果的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 +doctitle+"");
          }
          HTML = pagination.buildHTML("600");
          out.print(HTML);
          }
          }

          }

          posted on 2009-03-03 15:56 草原上的駱駝 閱讀(1372) 評論(0)  編輯  收藏 所屬分類: 搜索服務

          主站蜘蛛池模板: 沐川县| 施甸县| 西宁市| 朝阳县| 洞头县| 福鼎市| 图木舒克市| 贵阳市| 翼城县| 宜宾市| 麻阳| 临武县| 奎屯市| 体育| 泰顺县| 兴山县| 闽清县| 商城县| 青河县| 新平| 陆良县| 绥宁县| 遵义市| 郧西县| 咸宁市| 涟源市| 陆良县| 永川市| 岱山县| 镶黄旗| 西乡县| 泰兴市| 浪卡子县| 舞钢市| 酒泉市| 鸡东县| 鄂州市| 改则县| 涞源县| 砀山县| 浦县|