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

来源:互联网 发布:河北天然气 知乎 编辑:程序博客网 时间:2024/04/28 17:27

这几天做项目的时候引入了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;    }


原创粉丝点击