把Lucene的查询当成sql的查询,也许会笼统的明白些query的真相了。

查询分为大致两类,1:精准查询。2,模糊查询。

创建测试数据。

private Directory directory;
private IndexReader reader;
private String[] ids = {"1","2","3","4","5","6"};
private String[] emails = {"aa@itat.org","bb@itat.org","cc@cc.org","dd@sina.org","ee@zttc.edu","ff@itat.org"};
private String[] contents = {
"welcome to visited the space,I like book",
"hello boy, I like pingpeng ball",
"my name is cc I like game",
"I like football",
"I like football and I like basketball too",
"I like movie and swim"
};
private int[] attachs = {2,3,1,4,5,5};
private String[] names = {"zhangsan","lisi","john","jetty","lisi","jake"};

先建立索引。

 private Map<String,Float> scores = new HashMap<String,Float>();

 public SearchUtil(){
try {
directory = FSDirectory.open(Paths.get("D://lucene//index"));
scores.put("itat.org", 1.5f);
scores.put("cc.org", 2.0f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建索引
*/
@SuppressWarnings("deprecation")
public void index(){
IndexWriter writer = null;
try {
directory = FSDirectory.open(Paths.get("D://lucene//index"));
writer = getWriter();
Document doc = null;
for(int i=0;i<ids.length;i++){
doc = new Document();
doc.add(new Field("id", ids[i], Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("name", names[i], Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("content", contents[i], Field.Store.NO,Field.Index.ANALYZED));
//存储数字
doc.add(new IntField("attach", attachs[i], Field.Store.YES)); // 加权操作
TextField field = new TextField("email", emails[i], Field.Store.YES);
String et = emails[i].substring(emails[i].lastIndexOf("@")+1);
if (scores.containsKey(et)) {
field.setBoost(scores.get(et));
}
doc.add(field);
// 添加文档
writer.addDocument(doc);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

索引建立完毕。

构造方法。

/**
* getSearcher
* @return
*/
public IndexSearcher getSearcher(){
try {
directory = FSDirectory.open(Paths.get("D://lucene//index"));
if(reader==null){
reader = DirectoryReader.open(directory);
}else{
reader.close();
}
return new IndexSearcher(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

一、精准匹配。

1,精准查询

就是查什么给什么。

 /**
* 精准匹配
*/
public void search(String searchField,String field){
// 得到读取索引文件的路径
IndexReader reader = null;
try {
directory = FSDirectory.open(Paths.get("D://lucene//index"));
reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
// 运用term来查找
Term t = new Term(searchField, field);
Query q = new TermQuery(t);
// 获得查询的hits
TopDocs hits = searcher.search(q, 10);
// 显示结果
System.out.println("匹配 '" + q + "',总共查询到" + hits.totalHits + "个文档");
for (ScoreDoc scoreDoc : hits.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println("id:"+doc.get("id")+":"+doc.get("name")+",email:"+doc.get("email"));
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

2,区间查询。

/**
* between
* @param field
* @param start
* @param end
* @param num
*/
public void searchByTermRange(String field,String start,String end,int num) {
try {
IndexSearcher searcher = getSearcher();
BytesRef lowerTerm = new BytesRef(start.getBytes()) ;
BytesRef upperTerm = new BytesRef(end.getBytes()) ; Query query = new TermRangeQuery(field, lowerTerm , upperTerm, true, true);
TopDocs tds = searcher.search(query, num); System.out.println("一共查询了:"+tds.totalHits);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach"));
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

3、匹配其索引开始以指定的字符串的文档

 /**
* 匹配其索引开始以指定的字符串的文档
* @param field
* @param value
* @param num
*/
public void searchByPrefix(String field,String value,int num) {
try {
IndexSearcher searcher = getSearcher();
Query query = new PrefixQuery(new Term(field,value));
TopDocs tds = searcher.search(query, num);
System.out.println("一共查到:"+tds.totalHits);
for(ScoreDoc scoreDoc:tds.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach"));
}
} catch (Exception e) {
e.printStackTrace();
}
}

4、数字搜索

/**
* 数字搜索
* @param field
* @param start
* @param end
* @param num
*/
public void searchByNums(String field,int start,int end,int num){
try {
IndexSearcher searcher = getSearcher();
Query query = NumericRangeQuery.newIntRange(field, start, end, true, true);
TopDocs tds = searcher.search(query, num);
System.out.println("一共查到:"+tds.totalHits);
for(ScoreDoc scoreDoc:tds.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach"));
}
} catch (Exception e) {
e.printStackTrace();
}
}

二、模糊匹配

/**
* 通配符
* @param field
* @param value
* @param num
*/
public void searchByWildcard(String field,String value,int num){
try {
IndexSearcher searcher = getSearcher();
WildcardQuery query = new WildcardQuery(new Term(field,value));
TopDocs tds = searcher.search(query, num);
System.out.println("一共查到:"+tds.totalHits);
for(ScoreDoc scoreDoc:tds.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* BooleanQuery可以连接多个子查询
* Occur.MUST表示必须出现
* Occur.SHOULD表示可以出现
* Occur.MUSE_NOT表示不能出现
* @param field
* @param value
* @param num
*/
@SuppressWarnings("deprecation")
public void searchByBoolean(String[] field,String[] value,int num){
try {
if(field.length!=value.length){
System.out.println("field的长度需要与value的长度相等!");
System.exit(0);
}
IndexSearcher searcher = getSearcher();
BooleanQuery query = null;
TopDocs tds = null;
for(int i = 0;i<field.length;i++){
query = new BooleanQuery();
query.add(new TermQuery(new Term(field[i],value[i])),Occur.SHOULD);
tds = searcher.search(query, num);
}
System.out.println("一共查询:"+tds.totalHits);
for(ScoreDoc doc:tds.scoreDocs){
Document document = searcher.doc(doc.doc);
System.out.println(document.get("id")+"---->"+
document.get("name")+"["+document.get("email")+"]-->"+document.get("id")+","+
document.get("attach"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void searchByPhrase(int num){
try {
IndexSearcher searcher = getSearcher();
PhraseQuery query = new PhraseQuery();
query.setSlop(3);
query.add(new Term("content","like"));
// //第一个Term
query.add(new Term("content","football"));
TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:"+tds.totalHits);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 相似度匹配查询
* @param num
*/
public void searchByFuzzy(int num) {
try {
IndexSearcher searcher = getSearcher();
FuzzyQuery query = new FuzzyQuery(new Term("name","jake"));
TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:"+tds.totalHits);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach")+","+doc.get("date"));
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void searchByQueryParse(Query query,int num) {
try {
IndexSearcher searcher = getSearcher();
TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:"+tds.totalHits);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach")+","+doc.get("date")+"=="+sd.score);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Lucene的Query类介绍的更多相关文章

  1. ArcGIS API for JavaScript 4.2学习笔记[21] 对3D场景上的3D要素进行点击查询【Query类学习】

    有人问我怎么这个系列没有写自己做的东西呢? 大哥大姐,这是"学习笔记"啊!当然主要以解读和笔记为主咯. 也有人找我要实例代码(不是示例),我表示AJS尚未成熟,现在数据编辑功能才简 ...

  2. 并发编程--Concurrent-工具类介绍

    并发编程--Concurrent-工具类介绍 并发编程--Concurrent-工具类介绍 CountDownLatch CylicBarrier Semaphore Condition 对象监视器下 ...

  3. CYQ.Data.Orm.DBFast 新增类介绍(含类的源码及新版本配置工具源码)

    前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...

  4. MediaRecorder类介绍

    audiocallbackvideojavadescriptorencoding 目录(?)[+] 找到个MediaRecorder类介绍和大家分享一下. Mediarecorder类在官网的介绍和在 ...

  5. Object类介绍

    一.Object类介绍

  6. istringstream、ostringstream、stringstream 类介绍 .

    istringstream.ostringstream.stringstream 类介绍 . 转自:http://www.cnblogs.com/gamesky/archive/2013/01/09/ ...

  7. C#中的Dictionary字典类介绍

      Dictionary字典类介绍 必须包含名空间System.Collection.Generic    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)    键必须是 ...

  8. POI 导出导入工具类介绍

    介绍: Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...

  9. Android Paint类介绍以及浮雕和阴影效果的设置

    Paint类介绍 Paint即画笔,在绘制文本和图形用它来设置图形颜色, 样式等绘制信息. 1.图形绘制 setARGB(int a,int r,int g,int b); 设置绘制的颜色,a代表透明 ...

随机推荐

  1. 通过udl文件得到连接字符串

    1.新建一个文件,文件名任意,扩展名为udl 2.双击打开这个udl文件 3.点下一步: 4.测试连接成功后点击确定 5.用记事本打开这个udl文件: 连接字符串就出来了

  2. Unity3d 实现顶点动画

    在今年GDC上发现一个非常有趣的演讲,叫做Animating With Math,遂实现之,是讲述顶点shader动画的,举了几个经典的例子,但是讲者并没有给代码,而是像虚幻引擎那样的节点,这样更加清 ...

  3. poj 3237 Tree(树链剖分,线段树)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7268   Accepted: 1969 Description ...

  4. [LeetCode] Decode Ways 解题思路

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. 机器学习-review-1 线性回归

    发现隔一段时间,忘记了好多知识点,这里认为重要的知识点记录下来,作为笔记,方便以后回顾. From “李航- 统计学习方法” 统计学习方法的三要素: 模型,策略, 算法 对于线性回归 -------- ...

  6. openstack libtray

    OpenStack packages¶ Distributions release OpenStack packages as part of the distribution or using ot ...

  7. tar打包和解压命令

    如果你有一个大文件 想下载下来 但是又太大 可以压缩一下 然后打包 #压缩 tar -czvf ***.tar.gz tar -cjvf ***.tar.bz2 #解压缩 tar -xzvf ***. ...

  8. java.sql.SQLException: Before start of result set解决方法

    java.sql.SQLException: Before start of result set解决方法 今天做东西的时候发现这个错误,查了查,特地记下来,以后开始积累了 哈哈 解决发法是: 使用r ...

  9. C#目录文件复制、创建操作

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  10. abap中的弹出窗体函数

    POPUP_TO_CONFIRM_WITH_MESSAGE     会话框确实处理步骤; 用识别正文POPUP_TO_SELECT_MONTH             日历:弹出 POPUP_TO_C ...