lucene入门查询索引——(三)

时间:2021-09-10 05:42:34

1.用户接口(lucene不提供)

 

lucene入门查询索引——(三)

lucene入门查询索引——(三)

 

2.创建查询

lucene入门查询索引——(三)

 

3.执行查询

lucene入门查询索引——(三)

lucene入门查询索引——(三)

 

 

4.渲染结果:

lucene入门查询索引——(三)

lucene入门查询索引——(三)

 

5.过程分析

根据关键字查询索引库中的内容:

1)  创建IndexSearcher对象

2)  创建QueryParser对象

3)  创建Query对象来封装关键字

4)  用IndexSearcher对象去索引库中查询符合条件的前100条记录,不足100条记录的以实际为准

5)  获取符合条件的编号

6)  用indexSearcher对象去索引库中查询编号对应的Document对象

7)  将Document对象中的所有属性取出,再封装回JavaBean对象中去,并加入到集合中保存,以备将之用

 

 IndexSearcher对象搜索方法:

lucene入门查询索引——(三)

 

 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
------------