16. Index 建立前你應該要知道 資料該怎麼被處理
● Text 欄位 與 Analyzer 的運作
○ 全文檢索機制運作的背後,文字是如何透過 Lucene 處理的。
→ 這題內容太多,不在今天的範圍。
● Dynamic Mapping
○ 當一份被 indexing 進入 Elasticsearch 的文件,若是有一個欄位沒有在 mapping 中被定義
,Elasticsearch 會自動的判斷他的資料型態,並且依照預設或指定的規則,來 產生這個新欄位
的 mapping 設定。
● Index Template
○ 當新的 Index 要被建立時,若符合設定好的 Index Template 中的 index_patterns,就會依照
Template 裡的設定來建立 Index。
● Index Alias
○ 透過 別名 的方式來存取一個或多個 Index。
17. Index Template 使用的建議
● 可以針對 Index Template 的 index_pattern 及 priority 建立結構化的管理方式,
例如 logs 與 logs-xxxxx-* 這樣的子、從關係。(會有繼承的效果)
● version 一定要給,早期沒有 _meta 時可以考慮以日期來當版本號,不過有
_meta 後,會建議將此 index template 的基本描述及最後更新時間記錄在
_meta 中,以便於維護及管理。
● 可共用的設置抽出成 Component Template,可以當作某種角色的定義,讓管理
更容易。
● 在設計好之後,請多使用 Simulate API 來驗證這個 Index Template 的產生結果
及影響範圍。
● Elastic 官方還有其他的 Index Template 及 Component Template,在使用前可
以多參考官方或其他人的 Template 設計方式。
18. Index 建立前你應該要知道 資料該怎麼被處理
● Text 欄位 與 Analyzer 的運作
○ 全文檢索機制運作的背後,文字是如何透過 Lucene 處理的。
→ 這題內容太多,不在今天的範圍。
● Dynamic Mapping
○ 當一份被 indexing 進入 Elasticsearch 的文件,若是有一個欄位沒有在 mapping 中被定義
,Elasticsearch 會自動的判斷他的資料型態,並且依照預設或指定的規則,來 產生這個新欄位
的 mapping 設定。
● Index Template
○ 當新的 Index 要被建立時,若符合設定好的 Index Template 中的 index_patterns,就會依照
Template 裡的設定來建立 Index。
● Index Alias
○ 透過 別名 的方式來存取一個或多個 Index。
19. Index Alias 的 Best Practices (1/2)
● 盡量全面使用 Index Aliases 來存取 index
○ 若非 time series index,原 Index 可配合版本管理的小技巧,加上 _v1 之類的版本編號,而
Alias 則可用原名,例如:song alias 指向 song_v1 的 index。
=> 日後 Index 要修改、甚至要重建並 reindex 資料時,就不容易影響到使用端了。
● 善用 Index Aliases 搭配 Filter
○ 適度的 將 filter 封裝在 alias 中,減少使用端的查詢複雜化。
○ 資料存取範圍的權限管理 :限定使用者只能存取特定的子集合的資料、或是 限定時間內的資料,
而配合 security 的權限處理,可避免使用者存取到不應取得的資料,或是一口氣 查詢太多舊資
料導致效能的影響。
20. Index Alias 的 Best Practices (2/2)
● 配合 routing 來指定資料寫入到特定的 shard
○ 資料在 Indexing 進入 Elasticsearch 時,會依照 routing value 決定資料要寫入到哪個 shard 身
上,但如果有指定的 routing value,就可以決定同樣 routing value 的資料會被計算放到同樣的
shard 身上,這樣對於 performance 的優化或是資料的管理上都可以有許多應用的方式,而
alias 就能配合指定 routing 的值來達到這類型的運用。
=> 相同使用者或相同地區的資料寫到相同的 shard 來優化查詢效能。
● 配合 Index Lifecycle Management (ILM)
○ 隨著時間增長的資料,使用 ILM 來管理這些資料時,其中就是搭配 Index Alias 來切換寫入時要
指定到的實體 Index 在哪邊。
30. Terminology
Cluster: A group of nodes that work together to operate Elasticsearch.
一群會互相合作、分工處理任務、互相備援的 ES nodes 們。
Node: A Java process that runs the Elasticsearch software.
Index: A group of shards that form a logical data store.
一個資料庫,並且擁有一或多個分片( shards ),資料被分配存放在這些分片之中。
Shard: A Lucene index that stores and processes a portion of an Elasticsearch index.
一個 Lucene 索引的儲存單位,裡面存有多個的Segments,同時也是 Cluster 資料搬
移時的最小單位。
Segment: A Lucene segment that immutably stores a portion of a Lucene index.
實際寫在 Disk 的 Lucene 索引檔案,是唯讀的。
Document: A record that is submitted to and retrieved from an Elasticsearch index.
31. How the indexing request works?
Shard 數量決定 Indexing
處理的分散能力
圖片來源:https://www.elastic.co/pdf/elasticsearch-sizing-and-capacity-planning.pdf
32. How the searching request works?
從許多 Segment Files
中進行查詢
查詢的方式決定記憶體用多少 圖片來源:https://www.elastic.co/pdf/elasticsearch-sizing-and-capacity-planning.pdf
44. Off Heap
● 7.3 released & 7.7 enhanced.
● 將本來存在 Heap 中的 Term Index (tip),寫到 off heap,透過 MMAP 在需要時
載入到 OS filesystem 記憶體中處理。
● 與 7.6 相比,7.7 在 Geonames 的 benchmark 資料集跑 benchmark,記憶體使
用減少 3 ~ 7倍,NYC taxis 與 HTTP logs 減少近100倍。
● 可增加 OS filesystem memory 的使用分配,減少 JVM heap,在存放 log 與
metric 的巨量資料 Cluster 中,常常 JVM heap size 使用率 > OS filesystem
cache,這部份的調整是很好的優化。(32G 的 JVM Compressed OOPS 限制)
45. ● Elasticsearch 7.0 released
● Introduced Lucene 8.0 with Block-max WAND 演算法
● 效能優化
○ term queries between 3x and 7x faster
○ conjunctions between 3% and 7x faster
○ disjunctions between -8% (slightly slower) and 15x faster
○ Other queries such as phrases and constant-scoring queries like prefix queries got speedups
too
● 透過 track_total_hits 參數調整。
Search Result Hits.Total 不再預設回傳所有比對結果的筆數