ElasticSearch7.3學習(二十五)—-Doc value、query phase、fetch phase解析

1、Doc value

搜索的時候,要依靠倒排索引;

排序的時候,需要依靠正排索引,看到每個document的每個field,然後進行排序。

所謂的正排索引,其實就是doc values

在建立索引的時候,一方面會建立倒排索引,以供搜索用;一方面會建立正排索引,也就是doc values,以供排序,聚合,過濾等操作使用,doc values是被保存在磁碟上的,此時如果記憶體足夠,os會自動將其快取在記憶體中,性能還是會很高;如果記憶體不足夠,os會將其寫入磁碟上

1.1 倒排索引

doc1: hello world you and me

doc2: hi, world, how are you

term doc1 doc2
hello *  
world * *
you * *
and *  
me *  
hi   *
how   *
are   *

搜索時:

hello you –> hello, you

hello –> doc1

you –> doc1,doc2

sort by 出現問題,如果需要自定義排序(按照某些欄位排序)那麼就會出現問題,因為倒排索引已經被分詞了。,此時就需要使用正排索引來進行分詞

1.2 正排索引

doc1: { “name”: “jack”, “age”: 27 }

doc2: { “name”: “tom”, “age”: 30 }

document name age
doc1 jack 27
doc2 tom 30

2、文檔查詢

關於文檔的查詢過程,前面部落格已經解析過了:ElasticSearch7.3學習(六)—-文檔(document)內部機制詳解

這裡再簡單的回顧一下。分為兩個步驟,第一query,第二fetch

2.1 query 

2.1.1 query phase

(1)搜索請求發送到某一個coordinate node,構構建一個priority queue,長度以paging操作from和size為準,默認為10

(2)coordinate node將請求轉發到所有shard,每個shard本地搜索,並構建一個本地的priority queue

(3)各個shard將自己的priority queue返回給coordinate node,並構建一個全局的priority queue

2.1.2 replica shard提升搜索吞吐量

一次請求要打到所有shard的一個replica/primary上去,如果每個shard都有多個replica,那麼同時並發過來的搜索請求可以同時打到其他的replica上去

2.2 fetch 

2.2.1 fetch phase

(1)coordinate node構建完priority queue之後,就發送mget請求去所有shard上獲取對應的document

(2)各個shard將document返回給coordinate node

(3)coordinate node將合併後的document結果返回給client客戶端