lucene入门查询索引——(三)
1.用户接口(lucene不提供)
2.创建查询
3.执行查询
4.渲染结果:
5.过程分析
根据关键字查询索引库中的内容:
1) 创建IndexSearcher对象
2) 创建QueryParser对象
3) 创建Query对象来封装关键字
4) 用IndexSearcher对象去索引库中查询符合条件的前100条记录,不足100条记录的以实际为准
5) 获取符合条件的编号
6) 用indexSearcher对象去索引库中查询编号对应的Document对象
7) 将Document对象中的所有属性取出,再封装回JavaBean对象中去,并加入到集合中保存,以备将之用
IndexSearcher对象搜索方法:
6.代码实现:
1 // 搜索索引 2 @Test 3 public void testSearch() throws Exception { 4 // 第一步:创建一个Directory对象,也就是索引库存放的位置。 5 Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁盘 6 // 第二步:创建一个indexReader对象,需要指定Directory对象。 7 IndexReader indexReader = DirectoryReader.open(directory); 8 // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象 9 IndexSearcher indexSearcher = new IndexSearcher(indexReader); 10 // 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。 11 Query query = new TermQuery(new Term("fileName", "java")); 12 // 第五步:执行查询。 13 TopDocs topDocs = indexSearcher.search(query, 10); 14 // 第六步:返回查询结果。遍历查询结果并输出。 15 ScoreDoc[] scoreDocs = topDocs.scoreDocs; 16 for (ScoreDoc scoreDoc : scoreDocs) { 17 int doc = scoreDoc.doc; 18 Document document = indexSearcher.doc(doc); 19 // 文件名称 20 String fileName = document.get("fileName"); 21 System.out.println(fileName); 22 // 文件内容 23 String fileContent = document.get("fileContent"); 24 System.out.println(fileContent); 25 // 文件大小 26 String fileSize = document.get("fileSize"); 27 System.out.println(fileSize); 28 // 文件路径 29 String filePath = document.get("filePath"); 30 System.out.println(filePath); 31 System.out.println("------------"); 32 } 33 // 第七步:关闭IndexReader对象 34 indexReader.close(); 35 36 }
结果:
java struts.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:\lucene1\searchfiles\java struts.txt ------------ java struts - .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 struts - .txt ------------ java struts.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 struts.txt ------------ java struts springmvc.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 struts springmvc.txt ------------ java struts spring.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 struts spring.txt ------------
将上面的第五步的
TopDocs topDocs = indexSearcher.search(query, 10); 改为
TopDocs topDocs = indexSearcher.search(query, 2);
结果:只输出查到的两条记录
java struts.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:\lucene1\searchfiles\java struts.txt ------------ java struts - .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 struts - .txt ------------
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
【推荐】国内首个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:你的「微服务管家」又秀新绝活了