Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 296|回复: 0

[Java框架学习]Lucene数据索引搜索示例

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:56:55 | 显示全部楼层 |阅读模式
    平台:Lucene 2.1.0,JRE 1.4,Oracle 10g,IBM Web Sphere。
           数据表:Article。字段:ID(自动增长),Title(String),Content(String)。共有550000条记录。

           对Article建立索引:
       
         1
       
        import
         org.apache.lucene.analysis.
        *
        ;

         2
       
        import
         org.apache.lucene.analysis.cn.
        *
        ;

         3
       
        import
         org.apache.lucene.document.
        *
        ;

         4
       
        import
         org.apache.lucene.index.
        *
        ;

         5
       
        import
         java.sql.
        *
        ;

         6
       
        import
         oracle.jdbc.pool.
        *
        ;

         7
       

         8
       
        public
         
        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}
       
           查找:
       
       
         1
       
        import
         java.io.
        *
        ;

         2
       
        import
         org.apache.lucene.analysis.cn.
        *
        ;

         3
       
        import
         org.apache.lucene.search.
        *
        ;

         4
       
        import
         org.apache.lucene.store.
        *
        ;

         5
       
        import
         org.apache.lucene.document.
        *
        ;

         6
       
        import
         org.apache.lucene.queryParser.QueryParser;

         7
       

         8
       
        import
         java.util.
        *
        ;

         9
       

        10
       
        public
         
        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进行代码深入研究。
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:56 , Processed in 0.416890 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表