`
Tonyguxu
  • 浏览: 272179 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【Lucene】搜索的核心类简介

 
阅读更多

注:Lucene版本为3.4

 

IndexReader

IndexSearcher

Term

QueryParser

Query

TermQuery

TopDocs

ScoreDoc

 

搜索的基本类:Directory IndexReader IndexSearcher


图1 搜索使用到的各个类的相互关系

 

QueryParser

QueryParser负责将用户输入的查询表达式(见 Apache Lucene - Query Parser Syntax)转换成对应的Query实例。

过程中需要一个分析器将表达式分割成多个项(分析器 is used to find terms in the query text)。

注意: QueryParser 是搜索过程中用到分析器的唯一类

 

简单的使用:

 

import org.apache.lucene.queryParser.QueryParser;

//content为defaultField默认搜索域
QueryParser parser = new QueryParser(Version matchVersion, "content", Analyzer a);

//默认搜索域为上面构造中指定的defaultField,在表达式中也可以指定field,如//field:expression
Query query = parser.parse(String queryExpression);

 

 

Query

 

TermQuery

 

 

IndexReader

IndexReader is an abstract class, providing an interface for accessing an index. Search of an index is done entirely through this abstract interface, so that any subclass which implements it is searchable.

 

1. 打开一个reader需要较大的系统开销,建议重复使用同一个IndexReader实例(将某目录的IndexReader实例缓存),

只有在必要的时候才打开新的(见第2条)。

2. 何时打开一个新的reader:在创建IndexReader时,会搜索已有的索引快照,如果在这(生成了reader实例)之后又有线程往索引中增加或者删除了,若要看到这些更改,必须打开一个新的reader。

 

boolean isCurrent() 
          Check whether any new changes have occurred to the index since this reader was opened.

 

IndexReader reader = ..
IndexSearcher searcher = ..
IndexReader newReader = reader.reopen();
if(newReader != null){
    reader.close();
    reader = newReader;
    searcher = new IndexSearcher(reader);
}

 注意:实际应用中,多线程可能还在使用这个旧的reader在搜索,必须保证上面这段代码是线程安全的。完善上面代码

 

3. 通过IndexWriter来获得IndexReader,参见 near real time search

 

//IndexReader类
public static IndexReader open(IndexWriter writer,
                               boolean applyAllDeletes)
                        throws CorruptIndexException,
                               IOException

 applyAllDeletes - If true, all buffered deletes will be applied (made visible) in the returned reader. If false, the deletes are not applied but remain buffered (in IndexWriter) so that they will be applied in the future. Applying deletes can be costly, so if your app can tolerate deleted documents being returned you might gain some performance by passing false.

 

4.通过Directory来获得IndexReader

 

//IndexReader类
public static IndexReader open(Directory directory,
                                                   boolean readOnly)
                        throws CorruptIndexException,
                               IOException

 

获得IndexReader实例时设置ReadOnly:should pass readOnly=true, since it gives much better concurrent performance, unless you intend to do write operations (delete documents or change norms) with the reader.

实践中,我们使用IndexWriter里的deleteDocuments来删除索引中documents,

 

 

 

代码示例:IndexFileManagerImpl

 

下面代码中从如下顺序获取IndexReader实例,首先从reader的cache里获取,如果没有则尝试通过该索引目录的IndexWriter获取,再没有则直接Directory获得。

 

public IndexReader getIndexReader(String name) throws IOException {
        Lock lock = rwLock.readLock();
        lock.lock();
        try {
            IndexReader reader = readerCache.get(name);
            if (reader == null) {
                lock.unlock();
                lock = rwLock.writeLock();
                lock.lock();
                IndexWriter writer = writerCache.get(name);
                if (writer != null) {
                    reader = IndexReader.open(writer, false); // get parallel reader, deletes would not apply at this moment
                } else {
                    File dir = new File(rootDir, name);
                    if (dir.exists()) {
                        if(isWinOS)
                            reader = IndexReader.open(FSDirectory.open(dir), true); // read only reader
                        else
                            reader = IndexReader.open(NIOFSDirectory.open(dir), true); // read only reader
                    } else {
                        reader = null; // if the specific index files are not existing yet
                    }
                }
                if (reader != null) {
                    readerCache.put(name, reader);
                }
            }
            return reader;
        } finally {
            lock.unlock();
        }
    }
 

以上为IndexReader的基本用法,对其他更深入的知识点后续研究


IndexSearcher

根据查询条件(Query对象)进行搜索的模块。

 

 API 写道
For performance reasons, if your index is unchanging, you should share a single IndexSearcher instance across multiple searches instead of creating a new one per-search.
当能确保索引是不可变的话,出于性能考虑,建议将IndexSearcher缓存起来

             写道

If your index has changed and you wish to see the changes reflected in searching, you should use IndexReader.reopen() to obtain a new reader and then create a new IndexSearcher from that.

问题1:程序是怎么得知索引是否变化的? 

 

 

 

获得 IndexSearcher 实例

1.Directory --> IndexReader --> IndexSearcher

2.Directory -->  IndexSearcher

 

 

Term

 

 

TopDocs

搜索结果排序:相关性评分(默认):每个结果文档与查询条件的匹配程度进行排序。

其他评分策略

ScoreDoc

  • 大小: 13.1 KB
分享到:
评论

相关推荐

    Lucene 3.6 学习笔记

    1.3 搜索部分的核心类 2 第二章 索引建立 3 2.1 创建Directory 3 2.2 创建Writer 3 2.3 创建文档并且添加索引 4 2.4 查询索引的基本信息 5 2.5 删除和更新索引 5 (1) 使用writer删除 5 (2) 使用reader删除 5 (3) ...

    基于lucene的搜索引擎总结

    Lucene搜索过程的核心类 IndexSearcher:用于搜索IndexWriter创建的索引 Term:用于搜索的一个基本单元包括了一对字符串元素,与Field相对应 Query :抽象的查询类 TermQuery:最基本的查询类型,用来匹配特定Field...

    lucene学习笔记

    lucene 学习笔记,学习lucene的调研及一些资料。lucene核心类及lucene的帮助文档。

    基于JAVA的搜索引擎 lucene-2.2.0

    在前面Lucene-2.2.0 源代码阅读学习(1)中,根据Lucene提供的一个Demo,详细分析研究一下索引器org.apache.lucene.index.IndexWriter类,看看它是如果定义的,掌握它建立索引的机制。 通过IndexWriter类的实现源代码...

    最新Lucene教程

    Ø Lucene中的类主要组成如下: 1)org.apache.1ucene.analysis语言分析器,主要用于的切词Analyzer是一个抽象类,管理对文本内容的切分词规则。 2)org.apache.1uceene.document索引存储时的文档结构管理,类似于...

    搜索链接Java网络爬虫(蜘蛛)源码-zhizhu

    利用java.url中的类实现Spider程序与外界通讯,以及处理网页中的URL连接,对蜘蛛程序的核心类(通讯核心、蜘蛛程序工作核心),资源索引的建立与搜索新型了详细的研究。 通过设计分析,完成了自己的蜘蛛爬行程序。...

    Solr培训文档

    搜索引擎的三大核心 中文分词 相关排序 相关排序算法 网络蜘蛛 Lucene简介 Lucene与Solr的关系 Solr的特点与优势 Solr 客户端 Solr 体系结构图 查询HTTP接口参数 分库机制 缓存机制 庖丁解牛分词器

    java中文分词

    java的分词器都是基于 lucene 核心的.该资源里包含所用jar包及一个测试类

    puck-core:开源,跨平台.NET Core CMS。 强大的查询和Lucene集成,可快速,可扩展,代码优先,不干扰和可扩展

    注意:这是mvc 5项目的asp.net核心迁移,您可以在找到。 提交未结转到该新存储库中。帕克CMS 快速,可伸缩,代码优先,功能强大且可扩展,并具有强大的查询功能和Lucene集成功能。为什么要使用冰球没有不必要的抽象...

    微信小程序-小程序商店

    lucene实现的站内搜索。 防xss、csrf攻击。 支持metaWeblog Api(支持windows live writer等离线博客编写) WordPress站点xml文件导入功能(支持文章、附件及标签) 文章标签tag功能、私密文章支持、文章归档 文章...

    Java博客系统Zblog2.zip

    lucene实现的站内搜索。 防xss、csrf攻击。 支持metaWeblog Api(支持windows live writer等离线博客编写) WordPress站点xml文件导入功能(支持文章、附件及标签) 文章标签tag功能、私密文章支持、文章...

    Web下搜索引擎的设计与实现.doc

    本文首先介绍了搜索引擎出现的必要性,以及什么是搜索引擎、搜索引擎的分类、处理流程、核心技术,同时也对如何才能提高搜索引擎的精准度以及关联度进行了更加深入的研究。 关键词: Web搜索

    searchengineer:垂直搜索

    searchengineer 垂直搜索 实现了一个小型完整的搜索引擎系统...最后是搜索引擎的核心全文索引,了解了自定义的数据结构,数据类型,怎样以二进制的形式读写文本,怎样以utf-8编码形式存储文本,快速排序,二分查找的运用。

    网络爬虫调研报告.docx

    而Lucene只是个搜索引擎工具,它提供API接口,通过编写程序对信息进行索引和检索,在其后台需要网络爬虫程序的支持,其目的是通过网络爬虫软件抓取网页,作为提供给Lucene搜索引擎的资源,进行索引和查询。...

    百度云盘 pdf《大数据架构和算法实现之路:电商系统的技术实战》百度云盘-带标签目录

    4.5.1 Lucene 简介……………… 108 4.5.2 Solr 简介 ......………………… 113 4.5.3 Elasticsearch 简介…………… · 120 4.6 案例实践……………… 123 4.6.1 实验环境设置.. ... ....………… 123 4.6.2 基于...

    基于JAVA技术的搜索引擎的研究与实现

    索引和搜索部分借助Lucene全文搜索引擎库中的Java类进行实现。实现搜索引擎的个性化,使搜索引擎具有自我学习的功能,能自动地适应用户的查询需求,并能对用户进行智能分类从而为搜索引擎的个性化提供依据。

    微信公众平台应用开发:方法、技巧与案例.(机械工业.柳峰)

    内容简介 该书系统讲解了微信公众平台应用开发的流程、方法和技巧,并配有若干完整的案例。 全书共11章,逻辑上划分为四个部分: 第一部分(第1~2章)介绍了公众平台的使用、公众账号的认证、编辑模式的使用等...

    SpringBoot开发非常美观的java博客系统(包含后台管理功能).zip

    核心功能 文章/图片/视频发布、喜欢、统计阅读次数。 文章标签tag功能、支持按tag分类 文章支持ueditor/markdown编辑器切换(后台配置) ...lucene实现的站内搜索。 响应式布局 支持用户订阅

    Java EE常用框架.xmind

    Lucene是根据关健字来搜索的文本搜索工具(全文搜索引擎),只能在某个网站内部搜索文本内容,不能跨网站搜索 全文搜索引擎是在硬盘上的搜索,比传统Mysql数据库是要快的 比传统SQL多的功能:查询的结果有...

    JAVA WEB典型模块与项目实战大全

    第9章 搜索引擎(lucene+web spider)  9.1 关于搜索引擎的基本概念  9.2 网络蜘蛛(web spider)  9.3 下载和分析lucene全文搜索组件  9.4 初步使用lucene全文搜索组件  9.5 新闻搜索引擎具体实现  9.6 ...

Global site tag (gtag.js) - Google Analytics