lucene查询索引之QueryParser解析查询——(八)
0.语法介绍:
1.公共部分代码同七中一样
// IndexReader IndexSearcher public IndexSearcher getIndexSearcher() throws Exception { // 第一步:创建一个Directory对象,也就是索引库存放的位置。 Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁盘 // 第二步:创建一个indexReader对象,需要指定Directory对象。 IndexReader indexReader = DirectoryReader.open(directory); // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象 return new IndexSearcher(indexReader); } // 执行查询的结果 public void printResult(IndexSearcher indexSearcher, Query query) throws Exception { // 第五步:执行查询。 TopDocs topDocs = indexSearcher.search(query, 10); // 第六步:返回查询结果。遍历查询结果并输出。 ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { int doc = scoreDoc.doc; Document document = indexSearcher.doc(doc); // 文件名称 String fileName = document.get("fileName"); System.out.println(fileName); // 文件内容 String fileContent = document.get("fileContent"); System.out.println(fileContent); // 文件大小 String fileSize = document.get("fileSize"); System.out.println(fileSize); // 文件路径 String filePath = document.get("filePath"); System.out.println(filePath); System.out.println("------------"); } }
2.查询所有:(分析器会对查询条件进行分词)
语法: *:*
// 条件解释的对象查询 @Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 参数1: 默认查询的域 // 参数2:采用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("* : *"); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
3.使用默认查询的域
查询名字带有computer索引的文档
@Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 参数1: 默认查询的域 // 参数2:采用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("computer"); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
加载扩展词典:ext.dic 加载扩展停止词典:stopword.dic computer.txt ��Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don��t need to leave home to borrow books from a library or to do shopping in a supermarke 336 E:\lucene&solr\searchfiles\computer.txt ------------
4.范围查询
不支持范围查询
5.组合查询(组合查询只用修改语法,+表示必须,-表示必须没有,啥也没有表示可有可无)
查询fileName必须带有Java,且必须不带struts的文档。
语法 +fileName:java -fileName:struts
// 条件解释的对象查询 @Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 参数1: 默认查询的域 // 参数2:采用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("+fileName:java -fileName:struts"); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
加载扩展词典:ext.dic 加载扩展停止词典:stopword.dic java 基础.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java 基础.txt ------------ java高级.txt ����java���̳̣��������������� 32 E:\lucene&solr\searchfiles\java高级.txt ------------
6.复杂查询
查询 fileName 带有javaweb 或者 computer的,或者是fileContent带有javaweb 或者 computer的
IKAnalyzer分析器先对java is apach进行分词,然后对javaweb is computer进行分词,然后根据条件进行查询带有分析器分词后的词的文档。
// 条件解释的对象查询 @Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 参数1: 默认查询的域 // 参数2:采用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("fileName:javaweb is computer OR fileContent:javaweb is computer "); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
加载扩展词典:ext.dic 加载扩展停止词典:stopword.dic computer.txt ��Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don��t need to leave home to borrow books from a library or to do shopping in a supermarke 336 E:\lucene&solr\searchfiles\computer.txt ------------ 1javaweb .txt this is javaweb dsbadfsabjkfsdf njdfndsj njaj spring 53 E:\lucene&solr\searchfiles\1javaweb .txt ------------
--------------------条件解析的对象查询 多个默念域-----------------------
7.解析器用 MultiFieldQueryParser
查询 fileName 带有javaweb 或者 computer的,或者是fileContent带有javaweb 或者 computer的(与6的作用等价)
作用不大,在查询条件中一用上查询域默认查询域就废了。
// 条件解析的对象查询 多个默念域 @Test public void testMultiFieldQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); String[] fields = { "fileName", "fileContent" }; // 参数1: 默认查询的域 // 参数2:采用的分析器 MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("javaweb is computer"); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
总结:
这个可以将查询条件设置为句子,IKAnalyzer分析器会对句子进行分词处理,然后进行查询索引。
// 参数1: 默认查询的域 // 参数2:采用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("fileName:java is apache OR fileContent:Computers are changing our life ");
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了