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
------------
复制代码

 

posted @   QiaoZhi  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示