ElasticSearch(二十一)正排和倒排索引

1.區別緩存

搜索的時候,要依靠倒排索引;排序的時候,須要依靠正排索引,看到每一個document的每一個field,而後進行排序,所謂的正排索引,其實就是doc values併發

在創建索引的時候,一方面會創建倒排索引,以供搜索用;一方面會創建正排索引,也就是doc values,以供排序,聚合,過濾等操做使用性能

doc values是被保存在磁盤上的,此時若是內存足夠,os會自動將其緩存在內存中,性能仍是會很高;若是內存不足夠,os會將其寫入磁盤上spa

 

下面兩條documentcode

doc1: hello world you and me
doc2: hi, world, how are youorm

2.倒排索引,用來進行搜索blog

word        doc1        doc2

hello        *
world        *        *
you          *        *
and          *
me           *
hi                    *
how                   *
are                   *

3.正排索引,用來進行排序排序

document    name        age

doc1        jack        27
doc2        tom        30    

sort by age,取出age進行排序索引

4.倒排索引的結構 內存

(1)包含這個關鍵詞的document list
(2)包含這個關鍵詞的全部document的數量:IDF(inverse document frequency)
(3)這個關鍵詞在每一個document中出現的次數:TF(term frequency)
(4)這個關鍵詞在這個document中的次序
(5)每一個document的長度:length norm
(6)包含這個關鍵詞的全部document的平均長度

5.倒排索引不可變的好處和壞處

(1)不須要鎖,提高併發能力,避免鎖的問題
(2)數據不變,一直保存在os cache中,只要cache內存足夠
(3)filter cache一直駐留在內存,由於數據不變
(4)能夠壓縮,節省cpu和io開銷

倒排索引不可變的壞處:每次都要從新構建整個索引

相關文章
相關標籤/搜索