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的写法有所改变。

最新文章

  1. python写红包的原理流程包含random,lambda其中的使用和见简单介绍
  2. Java—字符串小结
  3. TWRP基于omnirom 6.0.1编译教程
  4. struts2环境配置
  5. centos6.6安装配置jboss7.1.1
  6. [vijos P1035] 贪婪的送礼者
  7. CGContextRef 绘图
  8. PHP--字符串处理函数
  9. August 3rd, 2016, Week 32nd, Wednesday
  10. quartz中关键类
  11. 《Java数据结构与算法》笔记-CH4-1栈的实现
  12. [改善Java代码]避免在构造函数中初始化其他类
  13. poj 2586 Y2K Accounting Bug
  14. DJANGO中获取登陆用名及别名
  15. linux串口驱动分析——打开设备
  16. 当向计算机中存入一个float类型的数值2.2 后,在从计算机中读出输出,这时2.2 的值已经发生了变化(转)
  17. Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题
  18. 【腾讯Bugly干货分享】你为什么需要 Kotlin
  19. 超文本传送协议HTTP
  20. R语言︱缺失值处理

热门文章

  1. Hibernate检索方式(转载)
  2. 如何禁用 a 标签的点击事件
  3. 差分IO标准
  4. U盘操作
  5. java restful response 万能类
  6. 如何在C#中读写INI文件
  7. 微信小程序基础语法总结
  8. python 函数和方法的区别
  9. C++ 学习之---C++基础理论知识(也适合面试回顾)
  10. java飞机大战之子弹的自动生成