隨筆 - 17  文章 - 84  trackbacks - 0
          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          如非特別說明,所有文章均為原創(chuàng)。如需引用,請注明出處
          Email:liangtianyu@gmail.com
          MSN:terry.liangtianyu@hotmail.com

          常用鏈接

          留言簿(4)

          隨筆分類(12)

          隨筆檔案(17)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 52116
          • 排名 - 961

          最新評論

          閱讀排行榜

          評論排行榜

                平臺:Lucene 2.1.0,JRE 1.4,Oracle 10g,IBM Web Sphere。
                數(shù)據(jù)表:Article。字段:ID(自動增長),Title(String),Content(String)。共有550000條記錄。

                對Article建立索引:
           1import org.apache.lucene.analysis.*;
           2import org.apache.lucene.analysis.cn.*;
           3import org.apache.lucene.document.*;
           4import org.apache.lucene.index.*;
           5import java.sql.*;
           6import oracle.jdbc.pool.*;
           7
           8public class Index {
           9    private String url="jdbc:oracle:thin:@//192.168.0.l:1521/Test";
          10    private String user="terry";
          11    private String password="dev";
          12    private Connection con=null;
          13    private Statement st=null;
          14    private ResultSet rs=null;
          15    private String indexUrl="E:\\ArticleIndex";
          16    
          17    private ResultSet getResult() throws Exception{
          18        OracleDataSource ods=new OracleDataSource();
          19        
          20        ods.setURL(this.url);
          21        ods.setUser(this.user);
          22        ods.setPassword(this.password);
          23        
          24        this.con=ods.getConnection();
          25        this.st=this.con.createStatement();
          26        this.rs=this.st.executeQuery("SELECT * FROM Article");
          27        
          28        return this.rs;
          29    }

          30    
          31    public void createIndex() throws Exception{
          32        ResultSet rs=this.getResult();
          33        
          34        Analyzer chineseAnalyzer=new ChineseAnalyzer();
          35        IndexWriter indexWriter=new IndexWriter(this.indexUrl,chineseAnalyzer,true);
          36        indexWriter.setMergeFactor(100);
          37        indexWriter.setMaxBufferedDocs(100);
          38        
          39        java.util.Date startDate=new java.util.Date();
          40        
          41        System.out.println("開始索引時間:"+startDate);
          42        
          43        executeIndex(rs,indexWriter);
          44        
          45        indexWriter.optimize();
          46        
          47        indexWriter.close();
          48
          49        java.util.Date endDate=new java.util.Date();
          50        
          51        System.out.println("索引結(jié)束時間:"+endDate);
          52        System.out.println("共花費:"+(endDate.getTime()-startDate.getTime())+"ms");
          53    }

          54    
          55    private void executeIndex(ResultSet rs,IndexWriter indexWriter) throws Exception{
          56        int i=0;
          57        
          58        while(rs.next()){
          59            int id=rs.getInt("ID");
          60            String title=rs.getString("TITLE");
          61            String info=rs.getString("CONTENT");
          62            
          63            Document doc=new Document();
          64            
          65            Field idField=new Field("ID",Integer.toString(id),Field.Store.YES,Field.Index.NO,Field.TermVector.NO);
          66            Field titleField=new Field("Title",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES);
          67            Field infoField=new Field("Content",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES);
          68            
          69            doc.add(idField);
          70            doc.add(titleField);
          71            doc.add(infoField);
          72            
          73            indexWriter.addDocument(doc);
          74            
          75            i++;
          76        }

          77        
          78        this.close();
          79        
          80        System.out.println("共處理記錄:"+i);
          81    }

          82    
          83    private void close() throws Exception{
          84        this.rs.close();
          85        this.st.close();
          86        this.con.close();
          87    }

          88}

                查找:
           
           1import java.io.*;
           2import org.apache.lucene.analysis.cn.*;
           3import org.apache.lucene.search.*;
           4import org.apache.lucene.store.*;
           5import org.apache.lucene.document.*;
           6import org.apache.lucene.queryParser.QueryParser;
           7
           8import java.util.*;
           9
          10public class Search {
          11    
          12    private static final String indexUrl="E:\\ArticleIndex";
          13    
          14    public static void main(String[] args) throws Exception {
          15/*建立索引代碼,查找時注釋*/
          16        //Index index=new Index();
          17        
          18        //index.createIndex();
          19        
          20        
          21        
          22        
          23        File indexDir=new File(indexUrl);
          24        FSDirectory fdir=FSDirectory.getDirectory(indexDir);
          25        
          26        IndexSearcher searcher=new IndexSearcher(fdir);
          27
          28//對中文建立解析(必須)
          29        QueryParser parser=new QueryParser("Title",new ChineseAnalyzer());
          30        Query query=parser.parse("李湘");
          31        
          32        Date startDate=new Date();
          33        System.out.println("檢索開始時間:"+startDate);
          34        
          35        Hits result=searcher.search(query);
          36        
          37        for(int i=0;i<result.length();i++){
          38            Document doc=result.doc(i);
          39            
          40            System.out.println("內(nèi)容:"+doc.get("Content"));
          41        }

          42        
          43        Date endDate=new Date();
          44        
          45        System.out.println("共有記錄:"+result.length());
          46        System.out.println("共花費:"+(endDate.getTime()-startDate.getTime()));
          47    }

          48
          49}


                經(jīng)測試,建立索引文件大概花了11分鐘。一般情況下,和用SQL執(zhí)行LIKE查詢差不多。

                當然,這只是我的粗略測試。最近一階段,我會對Lucene進行代碼深入研究。

          posted on 2007-04-30 16:43 Terry Liang 閱讀(2155) 評論(3)  編輯  收藏 所屬分類: Lucene 2.1研究

          FeedBack:
          # re: Lucene數(shù)據(jù)索引搜索示例[未登錄] 2007-05-01 01:05 菜鳥
          嗯 最近工作開始涉及到搜索引擎一塊 一起努力~~!  回復  更多評論
            
          # re: Lucene數(shù)據(jù)索引搜索示例 2007-06-11 11:22 azmo
          加油。。。我也加入lucene的研究軍團,呵呵  回復  更多評論
            
          # re: Lucene數(shù)據(jù)索引搜索示例 2007-07-02 09:41 風舞者
          不錯,我收了啊,哈哈。。。  回復  更多評論
            
          主站蜘蛛池模板: 普兰县| 华坪县| 孝昌县| 交城县| 双辽市| 阿勒泰市| 文化| 扶绥县| 奎屯市| 平谷区| 阿拉善盟| 红原县| 遵义市| 东莞市| 潢川县| 贵阳市| 贵定县| 全州县| 崇阳县| 当阳市| 水富县| 巢湖市| 漠河县| 乌恰县| 射洪县| 镶黄旗| 吴堡县| 连城县| 洛扎县| 松江区| 北碚区| 万全县| 庐江县| 安乡县| 盐源县| 贡嘎县| 荥经县| 仁布县| 苍溪县| 保康县| 鄱阳县|