一个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; }
- 一个lucene对数据库表做全文搜索的例子
- lucene(全文搜索)_luceneweb例子
- 基于全文检索引擎Lucene的一个小例子
- lucene对整个数据库建立全文索引
- 适宜做简单搜索的MySQL数据库全文索引
- 使用Lucene对数据库搜索
- 一个lucene搜索高亮显示的例子。
- lucene.net 如何实现全文的搜索
- lucene:全文搜索的原理分析
- [搜索] Lucene全文检索的基本原理
- 用Lucene做一个简单的Java搜索工具
- Lucene-搜索的入门例子
- 全文搜索lucene简介
- lucene全文搜索
- 全文搜索-Lucene
- Lucene全文搜索框架
- Lucene全文搜索学习
- Lucene全文搜索工具
- 新好耶笔试题
- Linux shell开发第一讲
- org.hibernate.MappingException: Could not determine type for: String
- public,protected,private访问
- js ajax跨域js代码
- 一个lucene对数据库表做全文搜索的例子
- Composite(组合)模式
- Java基础知识
- VC++之使用CFile类打开文件
- 自定义 Notification中 布局界面
- 一张图片由A、B两部分组成,当鼠标移动上去时显示B部分,当鼠标移出时显示A部分。
- Windows2003服务器IIS启用Gzip压缩的设置方法
- 获取文件属性SHGetFileInfo
- ORA-01722: invalid number ————Oracle Spatial