理解Lucene索引与搜索过程中的核心类

简介: 理解索引过程中的核心类 执行简单索引的时候需要用的类有 IndexWriter、Directory、Analyzer、Document、Field 1、IndexWriter IndexWriter写索引是索引过程的核心组件这个类负责创建新的索引或者打开已有的索引以及向索引中添加、删除或

理解索引过程中的核心类

欢迎访问我的个人网站:http://wuyudong.com/

执行简单索引的时候需要用的类有:

IndexWriter、ƒDirectory、ƒAnalyzer、ƒDocument、ƒField

1、IndexWriter

IndexWriter(写索引)是索引过程的核心组件,这个类负责创建新的索引,或者打开已有的索引,以及向索引中添加、删除或更新被索引文档的信息,但不能读取或搜索索引。IndexWriter需要开辟一定的空间来存储索引,该功能由Directory完成

2、Directory

/** A Directory is a flat list of files. Files may be written once, when they
* are created. Once a file is created it may only be opened for read, or
* deleted. Random access is permitted both when reading and writing.
*
* <p> Java's i/o APIs not used directly, but rather all i/o is
* through this API. This permits things such as: <ul>
* <li> implementation of RAM-based indices;
* <li> implementation indices stored in a database, via JDBC;
* <li> implementation of an index as a single file;
* </ul>
*
* Directory locking is implemented by an instance of {@link
* LockFactory}, and can be changed for each Directory
* instance using {@link #setLockFactory}.
*
*/

Directory描述了索引的存放位置,是一个抽象类,其子类负责具体指定索引的存放路径

3、Analyzer

Analyzer由IndexWriter构造方法指定,负责从被索引的文本文件中提取词汇,Analyzer是一个抽象类,由其子类完成相关的功能

4、Document

代表一些域(Field)的集合,Lucene只能从二进制文档中提取以Field实例形式的文本

5、Field

一篇文档包含不同类型的信息,可以分开索引,比如标题,时间,正文,作者等,都可以保存在不同的域里。

理解索引与搜索过程中的核心类

Lucene提供的搜素接口一样很简单易懂:

IndexSearcher、Term、Query、TermQuery、TopDocs

1、IndexSearcher

IndexSearcher用于搜索由IndexWriter类创建的索引,它需要Directory实例来掌控前期创建的索引,然后才能提供大量 的搜索方法。最简单的搜索方法是将单个的Query对象和int topN计数作为该方法的参数,并返回一个TopDocs对象,该方法的一个典型应用如下:

Directory dir = FSDirectory.open(new File("/tmp/index"));
IndexSearcher searcher = new IndexSearcher(dir);
Query q = new TermQuery(new Term("contents", "lucene"));
TopDocs hits = searcher.search(q, 10);
searcher.close();

2、Term

Term对象是搜索功能的基本单元。在搜索过程中可以创建Term对象,和TermQuery对象一起使用:

Query q = new TermQuery(new Term("contents", "lucene"));
TopDocs hits = searcher.search(q, 10);

上面代码的含义是寻找content域中包含lucene的前10个文档,并按照降序排列

3、Query

lucene中包含很多具体的Query(查询)子类。TermQuery、BooleanQuery、PhraseQuery、 PrefixQuery、 PhrasePrefixQuery、TermRangeQuery、NumericRangeQuery、 FilteredQuery、SpanQuery
4、TermQuery

TermQuery是lucene中最基本的查询类型,用来匹配指定域中包含特定项的文档

5、TopDocs

TopDocs类是一个简单的指针容器,指针一般指向前N个排名的搜索结果,搜索结果即匹配查询条件的文档

目录
相关文章
|
C语言 Python
【python中break、continue 、pass终止循环的区别】
【python中break、continue 、pass终止循环的区别】
1452 0
【python中break、continue 、pass终止循环的区别】
|
消息中间件 NoSQL 固态存储
Spring boot集成plumelog日志系统
近几日闲来无事,工作摸鱼之时在码云上发现一个更加轻量级的分布式日志系统 PlumeLog ,就研究了一下,写了一个demo,做个记录
|
运维 安全 Linux
使用docker自建rustdesk服务器
docker自建rustdesk服务器
16512 0
|
存储 缓存 监控
JVM学习(五):JVM运行时参数
JVM学习(五):JVM运行时参数
504 0
JVM学习(五):JVM运行时参数
|
SQL 存储 SpringCloudAlibaba
处理分布式事务(SpringCloud Alibaba Seata)
处理分布式事务(SpringCloud Alibaba Seata)
处理分布式事务(SpringCloud Alibaba Seata)
|
开发框架 人工智能 自然语言处理
【深入理解CLR 二】CLR的执行模型(下)
【深入理解CLR 二】CLR的执行模型(下)
280 0
|
JavaScript 前端开发 Java
2020你应该知道的TypeScript学习路线【联合类型-接口】
2020你应该知道的TypeScript学习路线【联合类型-接口】
176 0
2020你应该知道的TypeScript学习路线【联合类型-接口】
|
机器学习/深度学习 TensorFlow 算法框架/工具
遇到Python中文目录名问题,未解决
遇到Python中文目录名问题,未解决
182 0
|
存储 机器学习/深度学习 负载均衡
Nginx && FastDFS实现分布式文件服务器
Nginx && FastDFS实现分布式文件服务器
Nginx && FastDFS实现分布式文件服务器
彩铅练习,路飞
海贼王是我特别喜欢的一部动漫。 这一张路飞画的实在是不怎么像,练习中。 image.png
962 0