Lucene之查询索引
Query子类
TermQuery:根据域和关键词进行搜索
/**
* termQuery根据域和关键词进行搜索
*/
@Test
public void termQuery() throws IOException {
//1.创建Directory对象,指定索引库位置
Directory directory = FSDirectory.open(new File("D:\\Luene资料\\Index").toPath());
//2.创建IndexReader对象,读取索引库内容
IndexReader indexReader= DirectoryReader.open(directory);
//3.创建IndexSearcher对象
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
//4.创建Query查询对象
Query query=new TermQuery(new Term("fieldContent","spring"));
//5.执行查询,获取到文档对象
TopDocs topDocs = indexSearcher.search(query, 10);
System.out.println("共获取:"+topDocs.totalHits+"个文档~~~~~~~~~~~~~~~~~~~~~");
//6.获取文档列表
ScoreDoc[] scoreDocs=topDocs.scoreDocs;
for (ScoreDoc item:scoreDocs) {
//获取文档ID
int docId=item.doc;
//取出文档
Document doc = indexSearcher.doc(docId);
//获取到文档域中数据
System.out.println("fieldName:"+doc.get("fieldName"));
System.out.println("fieldPath:"+doc.get("fieldPath"));
System.out.println("fieldSize:"+doc.get("fieldSize"));
System.out.println("fieldContent:"+doc.get("fieldContent"));
System.out.println("==============================================================");
}
//7.关闭资源
indexReader.close();
}
结果

RangeQuery:范围搜索
/**
* RangeQuery范围搜素
*/
@Test
public void RangeQuery() throws IOException {
//创建Directory对象,指定索引库位置
Directory directory = FSDirectory.open(new File("D:\\Luene资料\\Index").toPath());
//创建IndexReader对象,读取索引库内容
IndexReader indexReader= DirectoryReader.open(directory);
//创建IndexSearcher对象
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
//设置范围搜索的条件 参数一范围所在的域
Query query= LongPoint.newRangeQuery("fieldSize",0,50);
//查询
TopDocs topDocs = indexSearcher.search(query, 10);
System.out.println("返回的文档个数:"+topDocs.totalHits); //获取到文档集合
ScoreDoc [] scoreDocs=topDocs.scoreDocs;
for (ScoreDoc doc:scoreDocs) {
//获取到文档
Document document = indexSearcher.doc(doc.doc);
//获取到文档域中数据
System.out.println("fieldName:"+document.get("fieldName"));
System.out.println("fieldPath:"+document.get("fieldPath"));
System.out.println("fieldSize:"+document.get("fieldSize"));
System.out.println("fieldContent:"+document.get("fieldContent"));
System.out.println("==============================================================");
} //关闭
indexReader.close();
}
结果

QueryParser:匹配一行数据,这一行数据会自动进行分词
/**
* queryparser搜素,会将搜索条件分词
*/
@Test
public void queryparser() throws IOException, ParseException {
//创建Directory对象,指定索引库位置
Directory directory = FSDirectory.open(new File("D:\\Luene资料\\Index").toPath());
//创建IndexReader对象,读取索引库内容
IndexReader indexReader= DirectoryReader.open(directory);
//创建IndexSearcher对象
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
//创建一个QueryParser对象 参数一:查询的域 参数二:使用哪种分析器
QueryParser parser=new QueryParser("fieldContent",new IKAnalyzer());
//设置匹配的数据条件
Query query = parser.parse("Lucene是一个开源的基于Java的搜索库");
//查询
TopDocs topDocs = indexSearcher.search(query, 10);
System.out.println("返回的文档个数:"+topDocs.totalHits); //获取到文档集合
ScoreDoc [] scoreDocs=topDocs.scoreDocs;
for (ScoreDoc doc:scoreDocs) {
//获取到文档
Document document = indexSearcher.doc(doc.doc);
//获取到文档域中数据
System.out.println("fieldName:"+document.get("fieldName"));
System.out.println("fieldPath:"+document.get("fieldPath"));
System.out.println("fieldSize:"+document.get("fieldSize"));
System.out.println("fieldContent:"+document.get("fieldContent"));
System.out.println("==============================================================");
}
//关闭
indexReader.close();
}
结果

Lucene之查询索引的更多相关文章
- 全文检索Lucene框架---查询索引
一. Lucene索引库查询 对要搜索的信息创建Query查询对象,Lucene会根据Query查询对象生成最终的查询语法,类似关系数据库Sql语法一样Lucene也有自己的查询语法,比如:“name ...
- lucene入门查询索引——(三)
1.用户接口(lucene不提供)
- 搜索引擎学习(三)Lucene查询索引
一.查询理论 创建查询:构建一个包含了文档域和语汇单元的文档查询对象.(例:fileName:lucene) 查询过程:根据查询对象的条件,在索引中找出相应的term,然后根据term找到对应的文档i ...
- Lucene查询索引(分页)
分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...
- Lucene查询索引
索引创建 以新闻文档为例,每条新闻是一个document,新闻有news_id.news_title.news_source.news_url.news_abstract.news_keywords这 ...
- lucene查询索引库、分页、过滤、排序、高亮
2.查询索引库 插入测试数据 xx.xx. index. ArticleIndex @Test public void testCreateIndexBatch() throws Exception{ ...
- 学习笔记CB011:lucene搜索引擎库、IKAnalyzer中文切词工具、检索服务、查询索引、导流、word2vec
影视剧字幕聊天语料库特点,把影视剧说话内容一句一句以回车换行罗列三千多万条中国话,相邻第二句很可能是第一句最好回答.一个问句有很多种回答,可以根据相关程度以及历史聊天记录所有回答排序,找到最优,是一个 ...
- lucene查询索引之QueryParser解析查询——(八)
0.语法介绍:
- lucene查询索引之Query子类查询——(七)
0.文档名字:(根据名字索引查询文档)
随机推荐
- hge引擎使用技巧
图片周围最好留出一像素,即上下左右都多出一像素.然后使用pngopt.exe处理一下.这样可以减少图片拉伸.旋转时边界模糊的情况 图片宽高最好是 2的N次方
- 聊一聊 MySQL 数据库中的那些锁
在软件开发中,程序在高并发的情况下,为了保证一致性或者说安全性,我们通常都会通过加锁的方式来解决,在 MySQL 数据库中同样有这样的问题,一方面为了最大程度的利用数据库的并发访问,另一方面又需要保证 ...
- python之路:day3
内容 变量的创建过程 身份运算和None 数据类型 一. 变量创建过程 首先,当我们定义了一个变量name = ‘oldboy’的时候,在内存中其实是做了这样一件事: 程序开辟了一块内存空间,将‘ol ...
- Docker底层架构之联合文件系统
联合文件系统(UnionFS)是一种分层.轻量级并且高性能的文件系统,它支持对文件系统的 修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several di ...
- JavaScript面向对象:创建对象
1.初级创建对象 var oCar=new Object; oCar.color='red'; oCar.door=4; oCar.map=3; oCar.showColor=function () ...
- centos7下oracle11g详细的安装与建表操作
一.oracle的安装,在官网下载oracle11g R2 1.在桌面单击右键,选择“在终端中打开”,进入终端 输入命令:su 输入ROOT密码: 创建用户组oinstall:groupadd oin ...
- LIBCMTD.lib与libcpmtd冲突的解决方法。
error: 1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int) ...
- python学习方法
python学习体系 python相关书籍若干本 了解python基础数据类型 熟悉各种类型的操作方法 理解函数与类的概念 练习练习再练习 前期以被动学习为主,把每个知识点都认真的学过去,后期慢慢再把 ...
- HDU_2084_DP
http://acm.hdu.edu.cn/showproblem.php?pid=2084 简单dp,从下到上,从左到右,依次更新每个位置最大值. #include<iostream> ...
- 林大妈的JavaScript进阶知识(二):JS异步行为
JavaScript 是单线程执行的 JavaScript运行在浏览器中.浏览器是多线程的,但只分配了其中一条给JavaScript,作为它的主线程.对于编码者来说,JavaScript是单线程的.因 ...