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
------------
时间: 2024-11-04 13:14:53

lucene入门查询索引——(三)的相关文章

lucene入门创建索引——(一)

1.程序宏观结构图 2.创建索引过程 3.代码实现 创建索引库: 1)  创建JavaBean对象 2)  创建Docment对象 3)  将JavaBean对象所有的属性值,均放到Document对象中去,属性名可以和JavaBean相同或不同 4)  创建IndexWriter对象 5)  将Document对象通过IndexWriter对象写入索引库中 6)  关闭IndexWriter对象 Jar包: 代码: 1 // 创建索引 2 @Test 3 public void testInd

lucene查询索引之Query子类查询——(七)

0.文档名字:(根据名字索引查询文档) 1. 提取获取InsexSearch 与 处理结果的公共代码 //IndexReader IndexSearcher public IndexSearcher getIndexSearcher() throws Exception{ // 第一步:创建一个Directory对象,也就是索引库存放的位置. Directory directory = FSDirectory.open(new File("D:\\temp\\index"));// 磁

lucene查询索引之解析查询——(八)

0.语法介绍: 1.公共部分代码同七中一样 // IndexReader IndexSearcher public IndexSearcher getIndexSearcher() throws Exception { // 第一步:创建一个Directory对象,也就是索引库存放的位置. Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁盘 // 第二步:创建一个ind

Lucene查询索引

索引创建 以新闻文档为例,每条新闻是一个document,新闻有news_id.news_title.news_source.news_url.news_abstract.news_keywords这6个域,添加两个news document到索引中,下面再贴一下创建索引的代码: package ucas.ir.lucene; import java.io.File; import java.io.IOException; import org.apache.lucene.analysis.An

Lucene的数值索引以及范围查询

对文本搜索引擎的倒排索引(数据结构和算法).评分系统.分词系统都清楚掌握之后,本人对数值索引和搜索一直有很大的兴趣,最近对Lucene对数值索引和范围搜索做了些学习,并将主要内容整理如下: 1. Lucene不直接支持数值(以及范围)的搜索,数值必须转换为字符(串): 2. Lucene搜索数值的初步方案: 3. Lucene如何索引数值,并支持范围查询. 1. Lucene不直接支持数值搜索 Lucene不直接支持数值(以及范围)的搜索,数值必须转换为字符(串)——这是由倒排索引这个核心所决定

Lucene入门程序-Java API的简单使用

Lucene入门程序 准备环境 JDK: 1.8.0_162 IDE: Eclipse Neon.3 数据库: MySQL 5.7.20 Lucene: 4.10.4(已经很稳定了,高版本对部分分词器支持不好) 准备数据 SET FOREIGN_KEY_CHECKS=0; -------------------------------- Table structure for `book` -------------------------------- DROP TABLE IF EXISTS

Lucene入门案例一

1. 配置开发环境 官方网站:http://lucene.apache.org/ Jdk要求:1.7以上 创建索引库必须的jar包(lucene-core-4.10.3.jar,lucene-analyzers-common-4.10.3.jar) 其他jar包(commons-io-2.4.jar , junit-4.9.jar) 2. 创建索引库 第一步:创建一个java工程,并导入jar包. 第二步:创建一个indexwriter对象. 1)指定索引库的存放位置Directory对象 2)

Lucene底层原理和优化经验分享(1)-Lucene简介和索引原理

基于Lucene检索引擎我们开发了自己的全文检索系统,承担起后台PB级.万亿条数据记录的检索工作,这里向大家分享下Lucene底层原理研究和一些优化经验. 从两个方面介绍: 1. Lucene简介和索引原理 2. Lucene优化经验总结 1. Lucene简介和索引原理 该部分从三方面展开:Lucene简介.索引原理.Lucene索引实现. 1.1 Lucene简介 Lucene最初由鼎鼎大名Doug Cutting开发,2000年开源,现在也是开源全文检索方案的不二选择,它的特点概述起来就是

Lucene入门的基本知识(四)

刚才在写创建索引和搜索类的时候发现非常多类的概念还不是非常清楚,这里我总结了一下. 1 lucene简单介绍 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来就能用,它仅仅是提供了一种工具让你能实现这些产品. 1.2 lucene能做什么 要回答这个问题,先要了解lucene的本质.实际上lucene的功能非常单一.说究竟,就是你给它若干个字符串.然后它为你提供一个全文搜索服务,告诉你你要