我们自己的lusecn搜索引擎

日志 >  技术交流
设置置顶 | 编辑 | 删除

我们自己的lusecn搜索引擎

发表于:2008年2月26日 14时32分36秒阅读(5)评论(0) 本文链接:http://user.qzone.qq.com/592433424/blog/1204007556
package com.shzscq.searchpatent.search;
import java.util.StringTokenizer;
import org.apache.lucene.search.*;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.document.Document;

/**
* Created by IntelliJ IDEA.
* User: qiao
* Date: 2008-1-23
* Time: 15:51:29
* To change this template use File | Settings | File Templates.
*/
public class Search
{
   private int iHits = 0;
   private String sMultiHits = "";
   private String sDatabase = "";
   private String sQuery = "";
   private String sSortField = "";
   private boolean bSort = false;
   private String sResult = "";
   private String sPath = "";
   private String sFieldList = "";
   public int getHits()
   {
        return this.iHits;
   }
   public String getArrHits()
   {
       return this.sMultiHits;
   }
   public String getResult()
   {
       return this.sResult;
   }
   public void initial(String sPath,String sIndexDatabase,String sQueryInput,String sSortField,boolean bSort,String sFieldList)
   {
       this.sPath = sPath;
       this.sDatabase =  sIndexDatabase;
       this.sQuery = sQueryInput;
       this.sSortField = sSortField;
       this.bSort = bSort;
       this.sFieldList = sFieldList;
   }
   public void searchAllWithResult(int iPage, int iPageSize) {
       String sErrorMessage = "";
       boolean bError = false;
       this.iHits = 0;
       StringTokenizer sDbToken = new StringTokenizer(this.sDatabase, ";");        //锟矫碉拷锟斤拷锟叫碉拷锟斤拷菘锟?
       int iDbNum = sDbToken.countTokens();                        //锟斤拷菘锟斤拷锟侥?
       IndexSearcher[] _arrSearchers = new IndexSearcher[iDbNum];
       Query _query = null;                     //the Query created by the QueryParser
       Hits _hits = null;
       int iDocIndex = (iPage-1) * iPageSize;
       try {
           for (int i = 0; i < iDbNum; i++) {
               String sDbName = sDbToken.nextToken();
               String sIndexName = this.sPath + "//" + sDbName;
               _arrSearchers[i] = new IndexSearcher(IndexReader.open(sIndexName));     //create an indexSearcher for our page
           }
       } catch (Exception e) {                         //any error that happens is probably due
           //to a permission problem or non-existant
           //or otherwise corrupt index
           bError = true;                                  //don't do anything up to the footer
           sErrorMessage = "ERROR opening the Index - contact sysadmin!" + e.getMessage();
           System.out.println(sErrorMessage);
       }
       if (this.sQuery == null) {
           bError = true;
           sErrorMessage = "Query string is null!";
       }
       if (bError == false) {
           try {
               Analyzer _analyzer = new StandardAnalyzer();        //construct our usual analyzer
               QueryParser _parser = new QueryParser("AB", _analyzer);
               _query = _parser.parse(this.sQuery);
           } catch (ParseException e) {
               bError = true;
               sErrorMessage = "Error While parsing query:" + e.getMessage();
           }
       }
       if (bError == false) {
           try {
               MultiSearcher _multiSearcher = new MultiSearcher(_arrSearchers);
               if (sSortField.equalsIgnoreCase("")) {
                   _hits = _multiSearcher.search(_query);
               } else {
                   Sort _st = new Sort();
                   _st.setSort(sSortField, bSort);   //锟斤拷锟斤拷锟斤拷锟斤拷
                   _hits = _multiSearcher.search(_query, _st);
               }
               this.iHits = _hits.length();
               if (_hits.length() == 0) {
                   bError = true;
                   sErrorMessage = "I'm sorry I couldn't find what you were looking for.";
               } else {
                   int iBegin = (iPage - 1) * iPageSize;
                   int iEnd = 0;
                   if (iPage * iPageSize > this.iHits) {
                       iEnd = _hits.length();
                   } else {
                       iEnd = iPage * iPageSize;
                   }
                   String[] sArrFields = this.sFieldList.split(";");
                   this.sResult = "<docs>";
                   for (int i = iBegin; i < iEnd; i++) {
                       Document doc = _hits.doc(i);
                       this.sResult = this.sResult + "<doc>";
                       sResult = sResult + "<id>" + (iDocIndex+1)  + "</id>";
                       for (int j = 0; j < sArrFields.length; j++) {
                           this.sResult = this.sResult + "<fld name=/"" + sArrFields[j] + "/">";
                           this.sResult = this.sResult + doc.get(sArrFields[j]);
                           this.sResult = this.sResult + "</fld>";
                       }
                       this.sResult = this.sResult + "</doc>";
                       iDocIndex++;
                   }
               }
               _multiSearcher.close();
           } catch (Exception ex) {
               System.out.println(ex.getMessage());
               bError = true;
               sErrorMessage = "I'm sorry I couldn't find what you were looking for.";
           }
       }
       for(int i=0;i<_arrSearchers.length;i++)
       {
          try
          {
           _arrSearchers[i].close();
           _arrSearchers[i] = null;
          }
          catch(Exception ex)
          {
              System.out.print(ex.getMessage());
          }
       }
       _hits = null;
   }
   public void searchEvyWithResult(int iPage,int iPageSize)
   {
       this.iHits = 0;
       this.sMultiHits = "";
       StringTokenizer sDbToken = new StringTokenizer(this.sDatabase, ";");        //锟矫碉拷锟斤拷锟叫碉拷锟斤拷菘锟?
       int iDbNum = sDbToken.countTokens();
       int iBegin = (iPage-1) * iPageSize;
       int iEnd = iPage * iPageSize;
       int iDocIndex = (iPage-1) * iPageSize;
       String [] sArrFields = this.sFieldList.split(";");
       this.sResult = "<docs>";
       for(int i=0;i<iDbNum;i++)
       {
           String sDbName = sDbToken.nextToken();
           String sIndexName = this.sPath + "//" + sDbName;
           IndexReader _reader = null;
           Searcher _searcher = null;
           Analyzer _analyzer = new StandardAnalyzer();
           QueryParser _parser = new QueryParser("AB", _analyzer);
           try
           {
               _reader = IndexReader.open(sIndexName);
               _searcher = new IndexSearcher(_reader);
                Query _query = _parser.parse(sQuery);
                Hits _hits = _searcher.search(_query);
                int iLen = _hits.length();
                if (iLen != 0)
                {
                    int iBIn = iBegin;
                    int iEIn = iEnd;
                    if (iBegin == 0)
                    {
                        if (iLen <= iEnd)
                        {
                            iEIn = iLen;
                            iBegin = iLen;
                        }
                    } else
                    {
                        if (iBegin >= this.iHits && iBegin < (this.iHits + iLen))
                        {
                            iBIn = (iBegin - this.iHits);
                            if ((this.iHits + iLen) <= iEnd)
                            {
                               iEIn = iLen;
                               iBegin = this.iHits + iLen;
                            }
                            else
                            {
                                iEIn = iEnd - this.iHits;
                            }
                        }
                    }
                   if(iLen > iBIn)
                   {
                   for (int j = iBIn; j < iEIn; j++) {
                            Document doc = _hits.doc(j);
                            sResult = sResult + "<doc>";
                            sResult = sResult + "<id>" + (iDocIndex + 1)  + "</id>";
                            for (int m = 0; m < sArrFields.length; m++) {
                                sResult = sResult + "<fld name=/"" + sArrFields[m] + "/">";
                                sResult = sResult + doc.get(sArrFields[m]);
                                sResult = sResult + "</fld>";
                            }
                            sResult = sResult + "</doc>";
                            iDocIndex ++ ;
                        }
                   }
                }
                this.iHits = this.iHits + iLen;
                this.sMultiHits = this.sMultiHits + ";" + iLen;
                _searcher.close();
                _reader.close();
                _searcher = null;
                _reader = null;
           }
           catch(Exception ex)
           {
               System.out.println(ex.getMessage());
           }

       }
       this.sMultiHits = this.sMultiHits.substring(1);
       this.sResult = this.sResult + "</docs>";
   }
   public void searchEvyWithoutResult()
   {
       this.iHits = 0;
       this.sMultiHits = "";
       StringTokenizer sDbToken = new StringTokenizer(this.sDatabase, ";");        //锟矫碉拷锟斤拷锟叫碉拷锟斤拷菘锟?
       int iDbNum = sDbToken.countTokens();
       for(int i=0;i<iDbNum;i++)
       {
           String sDbName = sDbToken.nextToken();
           String sIndexName = this.sPath + "//" + sDbName;
           IndexReader _reader = null;
           Searcher _searcher = null;
           Analyzer _analyzer = new StandardAnalyzer();
           QueryParser _parser = new QueryParser("AB", _analyzer);
           try
           {
               _reader = IndexReader.open(sIndexName);
               _searcher = new IndexSearcher(_reader);
                Query _query = _parser.parse(sQuery);
                Hits _hits = _searcher.search(_query);
                int iLen = _hits.length();
                this.sMultiHits = this.sMultiHits + ";" + iLen;
                this.iHits = this.iHits + iLen;
                _searcher.close();
                _reader.close();
                _searcher = null;
                _reader = null;
           }
           catch(Exception ex)
           {
               System.out.println(ex.getMessage());
           }

       }
       this.sMultiHits = this.sMultiHits.substring(1);
   }
   public void search(int iPage,int iPageSize)
   {
       if(this.sSortField.equalsIgnoreCase(""))
       {
           searchEvyWithResult(iPage,iPageSize);
       }
       else
       {
           searchAllWithResult(iPage,iPageSize);
           searchEvyWithoutResult();
       }
   }
    public static void main(String[] args)
    {
        Search _sch = new Search();
        _sch.initial("D://TestShanghai//server//data","MedicineStandardCN","TI:锟斤拷血压 OR TI:hypertension","PN",false,"PN;TI;PA");
        _sch.search(1,20);
        //_sch.searchEvyWithoutResult();//(8,100);
        System.out.println(_sch.getHits());
        System.out.println(_sch.getArrHits());
        System.out.print(_sch.getResult());
    }
}
 
评论列表
请选择道具
温馨提示:点击验证码输入框,以获取验证码
请输入验证码:
      
<script type="text/javascript"> //$1  于 $2 发表的评论
/x02").replace(//[//quote/]/g,"/x01"); for(var i=0;i<2;i++) s=s.replace(//x03([^/x03/x01/x02]*?)/x02([^/x03/x01/x02]*?)/x01/g, function(a,b,c){ return '
'+b+'引用内容:

'+c+'
'; }); return s.replace(/[/x03/x02/x01]/g,""); } var bLoaded = false; function checkMsgReply(obj) { if(!bLoaded) top.includeJS('/qzone/blog/script/common.js', function(){bLoaded=true;checkMsgReply(obj)}, document); else checkReply(obj); if(obj.checked){ MAX_COMMENT_LEN = 500; } else { MAX_COMMENT_LEN = 4500; } _fontCount = MAX_COMMENT_LEN; //字数限制 if(!window.sendCommentEditor) return; if(sendCommentEditor.editorArea.editMode == 1) toCountFont(sendCommentEditor.id, "html"); else toCountFont(sendCommentEditor.id, "text"); } function showMsgLeftCnt() { if(!bLoaded) top.includeJS('/qzone/blog/script/common.js', function(){bLoaded=true;showMsgLeftCnt();}, document); else showLeftSMS(); } function selectBlogPaper() { if(checkLogin() <= 10000) { top.showLoginBox("mall"); return; } if(!!top.g_JData["blogContent"]) { if(parent.g_iLoginUin == parent.g_iUin) { location.href="/qzone/newblog/blogeditor.html?paperid=" + parent.g_JData["blogContent"].data.lp_id + "&paperstyle=" + parent.g_JData["blogContent"].data.lp_style + "&paperdialog=1"; } else { parent.location.href="http://user.qzone.qq.com/" + parent.g_iLoginUin + "/addNewBlog?paperid=" + parent.g_JData["blogContent"].data.lp_id + "&paperstyle=" + parent.g_JData["blogContent"].data.lp_style; } } else { top.showMsgBox("抱歉,暂时无法获取该信纸信息!", 1, 2000); } } /** * 批量删除中选择全选 */ function selectAllComments(bChecked) { var oList = document.getElementsByName("commentCheckBox"); if(oList.length==0) return; for(var i=0; i 0){ dalert(null, parent.g_XDoc["delBatchReply"].xml, 2000); delete parent.g_XDoc["delBatchReply"]; return; } dalert(null, parent.g_XDoc["delBatchReply"].xml, 2000, 2); contentProperty.totalCommentNumber -= nDeleteCnt; //清理cache with(contentProperty){ delete parent.g_XDoc["blogRoot"].contentHSList[currentBlogid]; pageList = {}; pageIndexMap = []; currentCommentPage = lastCommentPage = (!contentProperty.nowaPage)?0:nowaPage[3]; parent.g_XDoc["blogRoot"].replyNumUpdateHSmap[currentBlogid] = totalCommentNumber; parent.isRefreshTop = true; if(currentCommentPage == 0) { setTimeout(contentInit, 1000); } else{ var tp = Math.ceil(totalCommentNumber/PAGE_COMMENT_NUM); var num = totalCommentNumber%PAGE_COMMENT_NUM; if(num==0 || currentCommentPage10000 && top.g_iLoginUin!=top.g_iUin) { $("msgboardSelfReply").style.display = ""; $("blogSelPaper").title = "我也要使用此信纸写日志"; } setTimeout(contentInit,50); //]]> </script>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值