隨筆 - 11  文章 - 2  trackbacks - 0
          <2007年8月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(1)

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          平臺:Lucene 2.1.0,JRE 1.4,Oracle 10g,IBM Web Sphere。
                數據表: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("索引結束時間:"+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("內容:"+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}

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

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

          posted @ 2007-08-16 10:57 jacksontoto 閱讀(251) | 評論 (0)編輯 收藏
               摘要: 搜索流程中的第二步就是構建一個Query。下面就來介紹Query及其構建。 當用戶輸入一個關鍵字,搜索引擎接收到后,并不是立刻就將它放入后臺開始進行關鍵字的檢索,而應當首先對這個關鍵字進行一定的分析和處理,使之成為一種后臺可以理解的形式,只有這樣,才能提高檢索的效率,同時檢索出更加有效的結果。那么,在Lucene中,這種處理,其實就是構建一個Query對象。 就Query對象本身言,它只是Luce...  閱讀全文
          posted @ 2007-08-16 10:56 jacksontoto 閱讀(697) | 評論 (0)編輯 收藏
          主站蜘蛛池模板: 隆回县| 赤水市| 元阳县| 皋兰县| 博客| 双辽市| 屏南县| 东阿县| 界首市| 普陀区| 肃南| 宝坻区| 彰化县| 华坪县| 晋江市| 塘沽区| 云南省| 文成县| 绥德县| 开封市| 乐清市| 大竹县| 榆树市| 苍山县| 武安市| 江都市| 鄂州市| 河池市| 册亨县| 邢台县| 辽中县| 庆安县| 抚顺市| 曲沃县| 保亭| 黄大仙区| 孟村| 鄯善县| 雷山县| 蕉岭县| 桦南县|