一个lucene对数据库表做全文搜索的例子

这几天做项目的时候引入了lucene,使用lucene 3.6主要用到了以下两个调用:

//创建索引文件
    private boolean createIndexFile(List list,String path){
        Directory dic=null;
        IndexWriter writer=null;
        Document doc=null;
        ErrorDetails ed=null;
        try{
            dic=new SimpleFSDirectory(new File(path));
            writer = new IndexWriter(dic,new StandardAnalyzer(Version.LUCENE_36),IndexWriter.MaxFieldLength.LIMITED);
            writer.deleteAll();
            writer.commit();
            for(Object o:list){
                String content="";//拼接内容
                ed=(ErrorDetails)o;
                //System.out.println(ed.getDetailsId()+" "+ed.getErrorId()+" "+ed.getErrorTitle()+"\n---------------------------------\n");        
                if(ed.getErrorId()!=null){
                    content+=ed.getErrorId();
                }
                if(ed.getErrorTitle()!=null){
                    content+=" "+ed.getErrorTitle();
                }
                if(ed.getAction()!=null){
                    content+=" "+ed.getAction();
                }
                if(ed.getReasion()!=null){
                    content+=" "+ed.getReasion();
                }
                if(ed.getDescription()!=null){
                    content+=" "+ed.getDescription();
                }
                if(ed.getSolution()!=null){
                    content+=" "+ed.getSolution();
                }
                doc=new Document();
                doc.add(new Field("details_id",ed.getDetailsId()+"",Field.Store.YES,Field.Index.ANALYZED));
                //doc.add(new Field("errorid",ed.getErrorId()+"",Field.Store.YES,Field.Index.ANALYZED));
                doc.add(new Field("content",content,Field.Store.YES,Field.Index.ANALYZED));
                writer.addDocument(doc);
            }
            writer.optimize();//索引优化
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
 
    private List<String> Searcher(String path,String key){
        Directory dir=null;
        IndexSearcher seacher=null;
        Query query=null;
        ArrayList<String> list=null;
        try{
            dir=FSDirectory.open(new File(path));
            seacher=new IndexSearcher(dir);
            seacher.setSimilarity(new IKSimilarity());
            //parser=new QueryParser(Version.LUCENE_36,"content",new StandardAnalyzer(Version.LUCENE_36));
            query=IKQueryParser.parse("content",key);
            TopDocs docus=seacher.search(query, 24);
            ScoreDoc []docs=docus.scoreDocs;
            list=new ArrayList<String>();
            for(int i=0;i<docs.length;i++){
                int id=docs[i].doc;
                Document d=seacher.doc(id);
                //System.out.println(d.get("details_id"));
                //ErrorDetails e=(ErrorDetails)dao.queryDetailByID(Integer.valueOf(d.get("details_id")));
                //System.out.println(e.getDetailsId()+" "+e.getErrorId()+" "+e.getErrorTitle());
                //System.out.println(i+"查询到的结果"+d.get("details_id")+" ");
                list.add(d.get("details_id"));
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(dir!=null){
                    dir.close();
                }
                if(seacher!=null){
                    seacher.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return list;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值