参考:

http://www.slideshare.net/lucenerevolution/what-is-inaluceneagrandfinal

http://www.slideshare.net/jpountz/how-does-lucene-store-your-data

http://www.infoq.com/cn/articles/database-timestamp-02?utm_source=infoq&utm_medium=related_content_link&utm_campaign=relatedContent_articles_clk

摘录一些重要的:

看一下Lucene的倒排索引是怎么构成的。

我们来看一个实际的例子,假设有如下的数据:

docid

年龄

性别

1

18

2

20

3

18

这里每一行是一个document。每个document都有一个docid。那么给这些document建立的倒排索引就是:

年龄

18

[1,3]

20

[2]

性别

[1,2]

[3]

可以看到,倒排索引是per field的,一个字段有一个自己的倒排索引。18,20这些叫做 term,而[1,3]就是posting list。Posting list就是一个int的数组,存储了所有符合某个term的文档id。那么什么是term dictionary 和 term index?

那么什么是term dictionary 和 term index?

假设我们有很多个term,比如:

Carla,Sara,Elin,Ada,Patty,Kate,Selena

如果按照这样的顺序排列,找出某个特定的term一定很慢,因为term没有排序,需要全部过滤一遍才能找出特定的term。排序之后就变成了:

Ada,Carla,Elin,Kate,Patty,Sara,Selena

这样我们可以用二分查找的方式,比全遍历更快地找出目标的term。这个就是 term dictionary。有了term dictionary之后,可以用 logN 次磁盘查找得到目标。但是磁盘的随机读操作仍然是非常昂贵的(一次random access大概需要10ms的时间)。所以尽量少的读磁盘,有必要把一些数据缓存到内存里。但是整个term dictionary本身又太大了,无法完整地放到内存里。于是就有了term index。term index有点像一本字典的大的章节表。比如:

A开头的term ……………. Xxx页

C开头的term ……………. Xxx页

E开头的term ……………. Xxx页

如果所有的term都是英文字符的话,可能这个term index就真的是26个英文字符表构成的了。但是实际的情况是,term未必都是英文字符,term可以是任意的byte数组。而且26个英文字符也未必是每一个字符都有均等的term,比如x字符开头的term可能一个都没有,而s开头的term又特别多。实际的term index是一棵trie 树:

例子是一个包含 "A", "to", "tea", "ted", "ten", "i", "in", 和 "inn" 的 trie 树。这棵树不会包含所有的term,它包含的是term的一些前缀。通过term index可以快速地定位到term dictionary的某个offset,然后从这个位置再往后顺序查找。再加上一些压缩技术(搜索 Lucene Finite State Transducers) term index 的尺寸可以只有所有term的尺寸的几十分之一,使得用内存缓存整个term index变成可能。整体上来说就是这样的效果。

现在我们可以回答“为什么Elasticsearch/Lucene检索可以比mysql快了。Mysql只有term dictionary这一层,是以b-tree排序的方式存储在磁盘上的。检索一个term需要若干次的random access的磁盘操作。而Lucene在term dictionary的基础上添加了term index来加速检索,term index以树的形式缓存在内存中。从term index查到对应的term dictionary的block位置之后,再去磁盘上找term,大大减少了磁盘的random access次数。

额外值得一提的两点是:term index在内存中是以FST(finite state transducers)的形式保存的,其特点是非常节省内存。Term dictionary在磁盘上是以分block的方式保存的,一个block内部利用公共前缀压缩,比如都是Ab开头的单词就可以把Ab省去。这样term dictionary可以比b-tree更节约磁盘空间。

--------------------------------------------------------

lucene并非使用Tree structure
– sorted for range queries
– O(log(n)) search

而是如下核心的数据结构,FST,delta encode压缩数组,列存储,LZ4压缩算法:
●Terms index: map a term prefix to a block in the dict ○ FST: automaton with weighted arcs, compact thanks to shared prefixes/suffixes 核心数据结构,本质是前后缀共享的状态机,类似trie来搜索用户输入的某个单词是否能搜到,搜到的话就跳转到Terms dictionary里去,搜到的结果是单词在terms dict里的offset(本质是数组的偏移量)
Lookup the term in the terms index
– In-memory FST storing terms prefixes
– Gives the offset to look at in the terms dictionary
– Can fast-fail if no terms have this prefix
●Terms dictionary: statistics + pointer in postings lists, Store terms and documents in arrays – binary search
• Jump to the given offset in the terms dictionary
– compressed based on shared prefixes, similarly to a burst trie
– called the “BlockTree terms dict”
• read sequentially until the term is found
●Postings lists: encodes matching docs in sorted order ○ + positions + offsets 倒排的文档ID都在此
• Jump to the given offset in the postings lists
• Encoded using modified FOR (Frame of Reference) delta
– 1. delta-encode
– 2. split into block of N=128 values
– 3. bit packing per block
– 4. if remaining docs, encode with vInt
●Stored fields
• In-memory index for a subset of the doc ids
– memory-efficient thanks to monotonic compression
– searched using binary search
• Stored fields
– stored sequentially
– compressed (LZ4) in 16+KB blocks

Query execution:
• 2 disk seeks per field for search
• 1 disk seek per doc for stored fields
• It is common that the terms dict / postings lists fits into the file-system cache
• “Pulse” optimization
– For unique terms (freq=1), postings are inlined in the terms dict
– Only 1 disk seek
– Will always be used for your primary keys

插入新数据:
Insertion = write a new segment 一直写信segment可以防止使用锁
• Merge segments when there are too many of them
– concatenate docs, merge terms dicts and postings lists (merge sort!)
删除:
Deletion = turn a bit off
• Ignore deleted documents when searching and merging (reclaims space)
• Merge policies favor segments with many deletions

优缺点:
Updates require writing a new segment
– single-doc updates are costly, bulk updates preferred
– writes are sequential
• Segments are never modified in place
– filesystem-cache-friendly
– lock-free!
• Terms are deduplicated
– saves space for high-freq terms
• Docs are uniquely identified by an ord
– useful for cross-API communication
– Lucene can use several indexes in a single query
• Terms are uniquely identified by an ord
– important for sorting: compare longs, not strings
– important for faceting (more on this later)

针对field使用列存储:
Per doc and per field single numeric values, stored in a column-stride fashion
• Useful for sorting and custom scoring
• Norms are numeric doc values

一些设计原则:
• Save file handles
– don’t use one file per field or per doc
• Avoid disk seeks whenever possible
– disk seek on spinning disk is ~10 ms
• BUT don’t ignore the filesystem cache
– random access in small files is fine
• Light compression helps
– less I/O
– smaller indexes
– filesystem-cache-friendly

针对Compression techniques的数据结构:FSTs LZ4

转载于:https://www.cnblogs.com/bonelee/p/6226386.html

lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法...相关推荐

  1. Lucene核心数据结构——FST存词典,跳表存倒排或者roarning bitmap 见另外一个文章...

    Lucene实现倒排表没有使用bitmap,为了效率,lucene使用了一些策略,具体如下: 1. 使用FST保存词典,FST可以实现快速的Seek,这种结构在当查询可以表达成自动机时(PrefixQ ...

  2. lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为一行...

    如何联合索引查询? 所以给定查询过滤条件 age=18 的过程就是先从term index找到18在term dictionary的大概位置,然后再从term dictionary里精确地找到18这个 ...

  3. Redis底层数据结构详解(一)

    Redis底层数据结构 一.简单动态字符串SDS 1. SDS 2. 为什么Redis没用C语言原生字符串? 2.1 C语言中的字符串 2.2 使用SDS的好处 二.链表linkedlist 三.压缩 ...

  4. SQL Server 2014聚集列存储索引

    转发请注明引用和原文博客(http://www.cnblogs.com/wenBlog) 简介 之前已经写过两篇介绍列存储索引的文章,但是只有非聚集列存储索引,今天再来简单介绍一下聚集的列存储索引,也 ...

  5. 行存储和列存储小介绍

    .定义 1.1定义 Sybase在2004年左右就推出了列存储的Sybase IQ数据库系统,主要用于在线分析.数据挖掘等查询密集型应用.列存储,缩写为DSM,相对于NSM(N-ary storage ...

  6. 列存储、行存储之间的关系和比较

    目录(?)[-] 一定义 定义 优点 场合 发展史 二sysbase iq 列存储介绍 列存储 数据压缩 索引 Low Fast 索引 Bit-Wise索引 High Group索引 Word 索引 ...

  7. predicate 列存储索引扫描_ColumnStore index (列存储索引)解析

    简介 首先介紹列存储的概念: 传统的数据库存储是行存储.对于SQL Server来说,每个page是8K:往page里面塞数据,假设该表每条数据长度是500字节,那么这个page 先塞第一条数据,然后 ...

  8. mysql按照列构建索引_列存储索引增强功能–在线和离线(重新)构建

    mysql按照列构建索引 In this article, we will explore ' Clustered columnstore online index build and rebuild ...

  9. 行存储索引改换成列存储索引_列存储索引增强功能–数据压缩,估计和节省

    行存储索引改换成列存储索引 Data compression is required to reduce database storage size as well as improving perf ...

最新文章

  1. python的image读取的图片是什么类型的-opencv python 读取图像/显示图像/保存图像...
  2. 程序员过关斩将--要想获取我的用户信息,就得按照规矩来
  3. 智慧物业小程序_刷脸支付+电商小程序+智慧酒店营销方案
  4. mq补偿机制java代码_RocketMQ源码分析之消息消费机制-消费端消息负载均衡机制与重新分布 - Java 技术驿站-Java 技术驿站...
  5. (28)VHDL实现数码管直译
  6. Linux配置手册(五)Linux 配置域名服务器(DNS)
  7. mongodb adminmongo 使用过程中的一些小问题记录
  8. php验证码图像程序,完美的php生成验证码程序代码-图像处理-Php教程
  9. 华为模拟器ensp与真实机器连接使用的配置内容二
  10. 《推荐系统实践》算法纯享(附代码链接)(三)—— 冷启动篇
  11. linux 服务器长ping 加时间戳
  12. python编程从入门到实践
  13. 《模拟电子技术》-童诗白
  14. 21天学通c语言中用的编译器,21天学通C语言
  15. 怎么SSH远程连接云服务器
  16. 关查找我的iphone时显示服务器连接超时,iPhone 屏幕镜像无法关闭,一直显示“正在查找 Apple TV”怎么办?...
  17. DataGridView显示时间格式
  18. FreeSwitch连接语音网关配置及使用场景
  19. VSCode JavaScript 代码格式化 美化 Beautify
  20. Excel数据分类汇总与数据透视表

热门文章

  1. Selenium3自动化测试——5. 鼠标悬停操作
  2. php xml转化为html,php将xml文件转换为html Web程序 - 贪吃蛇学院-专业IT技术平台
  3. 华为stfal00是什么手机_opporeno5pro和华为p40pro有什么区别 哪款手机更好
  4. mvc4.0 html.actionlink comfired,未触发MVC 5远程验证
  5. java 同步块 抛出异常_不把 wait 放在同步块中,为啥这种情况不会抛出 IllegalMonitorStateException?...
  6. 通过IP获取地址,限制某些地址访问(GeoIp)
  7. 【Linux入门到精通系列讲解】VFS分区动态绑定和CentOS7分区类型
  8. Oracle-PL/SQL语法基础
  9. python【数据结构与算法】棋盘覆盖问题
  10. 全卷积神经网络( FCN ):语义分割深度模型先驱