Lucene 全文检索 Lucene的使用
2024-09-28 06:35:13
Lucene 全文检索 Lucene的使用
一.简介:
参考百度百科:
http://baike.baidu.com/link?url=eBcEVuUL3TbUivRvtgRnMr1s44nTE70odpjF8VbUpg8z3o8u1mt2PLpP-WnLBZY7ifUNLHDUtKSAQDthiiIhIa
二.使用:
1.必备包:
lucene有7个包需要导入:analysis,document,index,queryParser,search,store,util
2.建立索引:
主要使用类:IndexWriter
主要使用函数:writer.AddDocument(doc);
IndexWriter writer = new IndexWriter("E:/index", new StandardAnalyze(),true,MaxFieldLength.UNLIMITED); //true代表覆盖原先数据,maxFieldLength用来限制Field的大小 Document doc = new Document(); doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); doc.add(new Field("time", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); writer.addDocument(doc); writer.optimize(); //优化 writer.close();
3.检索:
主要类:IndexSearcher
主要函数:searcher.search(query);
IndexSearcher searcher= new IndexSearcher("E:/index"); Query query = new TermQuery(new Term("title", "lucene"));//单个字节查询 //Query query = new FuzzyQuery(new Term("title", "lucene"));//模糊查询 //Query query = new WildcardQuery(new Term("title", "lu*"));//通配符查询 ?代表一个字符,*代表0到多个字符 //BooleanQuery query = new BooleanQuery();//条件查询 //BooleanQuery qson1 = new BooleanQuery(); //Query q1 = new TermQuery(new Term("title", "lucene")); //qson1.add(q1, Occur.MUST);//MUST是必须满足的 //BooleanQuery qson2 = new BooleanQuery(); //Query q2= new TermQuery(new Term("sex", "woman")); //qson2 .add(q2, Occur.MUST_NOT);//MUST_NOT是必须不满足 //query.add(qson1, Occur.SHOULD); //query.add(qson2, Occur.SHOULD);//SHOULD代表满足qson1或者满足qson2都可以 //PhraseQuery query = new PhraseQuery();//近距离查询 //query.setSlop(5);//距离设置为5 //query.add(new Term("title", "lucene")); //query.add(new Term("title", "introduction"));//查询出title中lucene和introduction距离不超过5个字符的结果 //Query query = new PrefixQuery(new Term("title", "lu"));//WildcardQuery的lu*一样 //RangeQuery query = new RangeQuery(new Term("time", "50"),new Term("time", "60"), true); //true代表[50,60],false代表(50,60) Hits hits = searcher.search(query); for (int i = ; i < hits.length(); i++) { Document d = hits.doc(i); String title= d.get("title"); System.out.print(title+ " "); }
这样,基本上就可以使用了
注:以上代码为lucene早些版本的写法。lucene3.02的写法有所改变。
最新文章
- python写红包的原理流程包含random,lambda其中的使用和见简单介绍
- Java—字符串小结
- TWRP基于omnirom 6.0.1编译教程
- struts2环境配置
- centos6.6安装配置jboss7.1.1
- [vijos P1035] 贪婪的送礼者
- CGContextRef 绘图
- PHP--字符串处理函数
- August 3rd, 2016, Week 32nd, Wednesday
- quartz中关键类
- 《Java数据结构与算法》笔记-CH4-1栈的实现
- [改善Java代码]避免在构造函数中初始化其他类
- poj 2586 Y2K Accounting Bug
- DJANGO中获取登陆用名及别名
- linux串口驱动分析——打开设备
- 当向计算机中存入一个float类型的数值2.2 后,在从计算机中读出输出,这时2.2 的值已经发生了变化(转)
- Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题
- 【腾讯Bugly干货分享】你为什么需要 Kotlin
- 超文本传送协议HTTP
- R语言︱缺失值处理