【Lucene】具体解释Lucene全文检索的信息写入与读取
Lucene的大致结构图:
信息写入索引库的过程:
读取信息的过程:
以下是一个向索引库写入信息与读取信息的样例:
public void testCreateIndex() throws Exception{
/**
* 1、创建一个student对象,而且把信息存放进去
* 2、调用indexWriter的API把数据存放在索引库中
* 3、关闭indexWriter
*/
// 创建一个Student对象。而且把信息存放进去
Student student = new Student();
student.setId(1L);
student.setName("张三");
// 调用indexWriter的API把数据存放在索引库中
/**
* 创建一个IndexWriter
* 參数三个 1、索引库, 指向索引库的位置 2、分词器
*/
// 创建索引库
Directory directory = FSDirectory.open(new File("./indexDir"));
// 创建分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
// 把一个student对象转化成document
Document document = new Document();
Field idField = new Field("id",student.getId().toString(),Store.YES,Index.NOT_ANALYZED);
Field nameField = new Field("name",student.getName(),Store.YES,Index.ANALYZED);
document.add(idField);
document.add(nameField);
indexWriter.addDocument(document);
// 关闭indexWriter
indexWriter.close();
}
public void testSearchIndex() throws Exception{
/**
* 1、创建一个IndexSearch对象
* 2、调用search方法进行检索
* 3、输出内容
*/
// 创建一个 IndexSearch对象
Directory directory = FSDirectory.open(new File("./indexDir"));
IndexSearcher indexSearcher = new IndexSearcher(directory);
// 调用search方法进行检索
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30,"name",analyzer);
Query query = queryParser.parse("张"); // 要查找的关键词
TopDocs topDocs = indexSearcher.search(query, 2); // 前两条
int count = topDocs.totalHits; // 依据关键词查询出来的总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Student> studentList = new ArrayList<Student>();
for(ScoreDoc scoreDoc:scoreDocs){
float score = scoreDoc.score; // 关键词得分
int index = scoreDoc.doc; // 索引的下标
Document document = indexSearcher.doc(index);
// 把document转化成Student
Student student = new Student();
student.setId(Long.parseLong(document.get("id"))); // document.getField("id").stringValue()
student.setTitle(document.get("name"));
studentList.add(student);
}
for(Student student:studentList){
System.out.println(student.getId());
System.out.println(student.getName());
}
}
Author:顾故
Sign:别输给以前的自己
【Lucene】具体解释Lucene全文检索的信息写入与读取的更多相关文章
- Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程
2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程 2014-12-07 23:39 2623人阅读 评论(0) ...
- Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息
Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息 在此回复牛妞的关于程序中分词器的问题,其实可以直接很简单的在词库中配置就好了,Lucene中分词的所有信息我们都可以从 ...
- Lucene 01 - 初步认识全文检索和Lucene
目录 1 搜索简介 1.1 搜索实现方案 1.2 数据查询方法 1.2.1 顺序扫描法 1.2.2 倒排索引法(反向索引) 1.3 搜索技术应用场景 2 Lucene简介 2.1 Lucene是什么 ...
- Lucene核心--构建Lucene搜索(上篇,理论篇)
2.1构建Lucene搜索 2.1.1 Lucene内容模型 一个文档(document)就是Lucene建立索引和搜索的原子单元,它由一个或者多个字段(field)组成,字段才是Lucene的真实内 ...
- Lucene4.6 把时间信息写入倒排索引的Offset偏移量中,并实现按时间位置查询
有个新的技术需求,需要对Lucene4.x的源码进行扩展,把如下的有时间位置的文本写入倒排索引,为此,我扩展了一个TimeTokenizer分词器,在这个分词器里将时间信息写入 偏移量Offset中. ...
- Lucene系列二:Lucene(Lucene介绍、Lucene架构、Lucene集成)
一.Lucene介绍 1. Lucene简介 最受欢迎的java开源全文搜索引擎开发工具包.提供了完整的查询引擎和索引引擎,部分文本分词引擎(英文与德文两种西方语言).Lucene的目的是为软件开发人 ...
- java将错误信息写入文件
第一种办法可以通过字符串,也就是先把错误信息写入字符串,再将字符串写入文件 import java.io.*; public class Demo { public static void main( ...
- ZH奶酪:PHP error_log()将错误信息写入日志文件
error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段. bool error_log ( string $message [, int $messag ...
- 将不同级别的logging 日志信息写入不同文件
将不同级别的logging 日志信息写入不同文件 # -*- coding: utf-8 -*- import os import time from logging.handlers import ...
随机推荐
- syslog命令
更多请关注 Linux命令大全 syslog 介绍 syslog是Linux系统默认的日志守护进程.默认的syslog配置文件是/etc/syslog.conf文件.程序,守护进程和内核提供了访问系统 ...
- TB平台搭建之二
主要想记录关于debug问题: 一般我会1.定位问题所在位置比如使能信号错误.地址读写错误.数据流pipeline错误.... 2.首先看问题的源头(对应信号)是否还正确,比如出现XX要查看她的第一级 ...
- 解决windows管理员已阻止你运行此应用问题
按WIN+R键,打开“运行”,然后输入“gpedit.msc",就是打开组策略,这个在控制面板中也可以打开. 在组策略里找到“计算机配置”-“Windows设置”-“安全设置”-“本地策略” ...
- wordpress配置SMTP服务发送邮件(qq邮箱)
wordpress有一个注册功能,填了用户名和邮箱后,会收到一封邮件,邮件里有一个链接,点击该链接可以获得密码和修改密码.但是,最开始,你会发现,等半天都没有收到邮件,再等到猴年马月也不会收到. 但是 ...
- 关于php使用xpath解析html中文乱码问题
$str2 = '<div id="content">我很好 </div>'; $dom = new DOMDocument(); //load之前强转字符 ...
- IS-IS IGP
is-is 是igp的一种 属于osi的协议 OSI的三层是网络层 包含两种服务 一种是面向连接服务CONS 另一种是无连接服务CLNS CLNS中包含CLNP ...
- 2017 Multi-University Training Contest - Team 4
日常绝望系列 Questionnaire HDU - 6075 In order to get better results in official ACM/ICPC contests, the te ...
- shrio注解的方式进行权限控制
除了通过API方式外,Shiro 提供Java 5+注解的集合,以注解为基础的授权控制.在你可以使用Java 注释之前,你需要在你的应用程序中启用AOP 支持. Shiro注解支持AspectJ.sp ...
- OpenJ_Bailian——4115鸣人和佐助(带状态的A*)
鸣人和佐助 Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status Desc ...
- BZOJ 4824 [Cqoi2017]老C的键盘 ——树形DP
每一个限制条件相当于一条有向边, 忽略边的方向,就成了一道裸的树形DP题 同BZOJ3167 唯一的区别就是这个$O(n^3)$能过 #include <map> #include < ...