喬叔的 Elastic Stack 專業教育訓練
  • 喬叔的 Elastic Stack 專業教育訓練
  • 🧑關於喬叔 (Joe Wu)
  • Elastic 課程公開班
    • 🎯Elasticsearch 基礎實務班
      • 💯學員課後回饋
    • 🆕Elasticsearch 進階運維班
      • 💯學員課後回饋
    • Elasticsearch 進階開發班
    • Elastic Stack 基礎實務班
    • Elastic Observability 基礎實務班
    • 📩課程許願池
  • 技術分享
    • 📗喬叔帶你上手 Elastic Stack
      • 前言
      • Elastic Cloud 如何建立 Deployment
        • ES Node 的種類
        • 配置的選擇
      • Index 建立前你該知道的
        • ES Index 如何被建立
        • ES 的超前佈署 - Dynamic Mapping
        • ES 的超前佈署 - Index Template
        • ES Index 的別名 (Alias)
        • 管理你的 Index - Kibana Index
      • 管理 Index 的 Best Practices
        • Shard 的數量與 Rollover & Shrink API
        • 三溫暖架構 - Hot Warm Cold Architecture
        • Index Lifecycle Management (ILM)
        • Rollup
        • Transform
        • Snapshot Lifecycle Management (SLM)
        • 總結
      • Elastic Cloud 比免費版還多的功能
        • Elastic Stack 的方案比較與銷售方式
        • Centralized Beats Management
        • Centralized Pipeline Management
        • Watcher
        • Elasticsearch Token Service
        • Multi-stack monitoring & Automatic stack issue alerts
      • 向 App Search 學習怎麼用 Elasticsearch
        • 揭開 App Search 的面紗
        • Engine 的 Index Settings 篇
        • Engine 的 Mapping 篇
        • Engine 的 Search 基礎剖析篇
        • Engine 的 Search 進階剖析篇
      • Elasticsearch 的優化技巧
        • Indexing 索引效能優化
        • Searching 搜尋效能優化
        • Index 的儲存空間最佳化
        • Shard 的最佳化管理
      • 完賽心得
    • 📘喬叔帶你上手 Elastic Stack - 探索與實踐 Observability 系列
      • 前言 & 淺談 Observability
      • Elastic 的 Observability 解決方案
      • Uptime - 掌握系統的生命徵象
        • 我們要觀測的生命徵象是什麼?
        • 使用 Heartbeat 收集系統生命徵象數據
        • 透過 Kibana 觀看心電圖及設定警報
        • 使用合成監控 (Synthetics Monitor) 從使用者情境驗證服務的運作狀態
      • Metrics - 觀察系統的健康指標
        • Metrics 與 Metricbeat 的基本介紹
        • 使用 Metricbeat 掌握 Elastic Stack 的健康狀態
        • 使用 Metricbeat 掌握 Infrastructure 的健康狀態 Host 篇
        • 使用 Metricbeat 掌握 Infrastructure 的健康狀態 Docker 篇
        • 使用 Metricbeat 掌握 Infrastructure 的健康狀態 Kubernetes 篇
        • 使用 Metricbeat 掌握 Infrastructure 的健康狀態 AWS 篇
      • Logs - 挖掘系統內部發生的狀況
        • Logs 與 Filebeat 的基本介紹
        • 使用 Filebeat 應該要了解的設計細節與原理
        • 透過 Filebeat 收集 Elastic Stack 中各種服務的細節資訊
        • 透過 Filebeat 收集 Infrastructure 中各種服務的細節資訊
      • Traces - 觀察應用程式的效能瓶頸
        • Elastic APM 基本介紹
        • 使用 APM-Integratoin-Testing 建立 Elastic APM 的模擬環境
        • 如何在 Kibana 使用 APM UI
        • 使用 APM Server 來收集 APM 數據
        • 透過 APM Agents 收集並傳送後端服務運作的記錄
        • 透過真實使用者監控 (RUM, Real User Monitoring) 來改善使用者體驗
      • 建立結構化的 Log
        • Elastic Common Schema 結構化 Log 的規範
        • Elasticsearch Ingest Pipeline 資料 Index 前的轉換好幫手
          • 基本介紹
          • 各種常用的 Processor
          • Enrich 資料與例外處理
      • 有效的使用 Observability 的資料
        • 透過 Machine Learning 發現異常的問題
        • 使用 Kibana Alerts 主動通知異常狀況
        • 資料的生命週期管理
        • 使用 Elastic Observability 追縱及觀察問題的心得
      • 完賽心得
    • 😀Elasticsearch 技術分享小品
      • 🤖Elastic 與 AI
        • Elasticsearch Inference API 讓我們直接在 ES 裡運用 OpenAI Completion API
    • 🎥線上分享
      • 喬叔 Elasticsearch Index 管理與效能優化技巧
      • Elastic Certification 認證經驗分享
    • 🛠️workshop
      • 如何在 Elasticsearch 實現敏捷的資料建模與管理 @ DevOpsDays 2023
        • 工作坊實作內容
      • Elastic Observability 實作體驗坊 @ DevOpsDays 2022
        • 行前準備
        • 工作坊實作內容
      • 當 Elasticsearch 搜尋引擎遇上 AI @ HelloWordDevConference 2024
        • 投影片
        • Elasticsearch 環境準備
        • Google Colab 環境準備
        • 工作坊操作說明
        • ElasticSearch Relevance Engine (ESRE)
    • ⬆️Elastic Stack 版本升級記錄
      • 🔍Elasticsearch
  • 其他專業服務
    • 👩‍🎓企業包班 | 企業內訓
    • 👨‍💼顧問服務
    • 🈺專案合作
    • 🧩Elastic 授權代理
  • 相關連結
    • Facebook 粉絲頁
Powered by GitBook
On this page
  • 進入此章節的先備知識
  • 此章節的重點學習
  • Index Alias 的使用方式
  • 使用 _cat API,列出所有的 Aliases
  • Add Alias alias API
  • Update index alias API
  • Best Practices
  • 參考資料
  1. 技術分享
  2. 喬叔帶你上手 Elastic Stack
  3. Index 建立前你該知道的

ES Index 的別名 (Alias)

PreviousES 的超前佈署 - Index TemplateNext管理你的 Index - Kibana Index

Last updated 2 years ago

進入此章節的先備知識

  • 知道什麼是 Index。

  • 知道如何使用 Search 以及 Filter。

此章節的重點學習

  • Index Alias 的基本使用方式。

  • 學會 Index Alias 一些最佳實踐的做法。


Index Alias 的使用方式

使用 _cat API,列出所有的 Aliases

GET _cat/aliases?v

使用到的參數說明:

  • v: verbose,在回傳的結果顯示標題。

  • alias:依 alias name 進行篩選。

_cat list aliases

Add Alias alias API

建立與更新 index alias,這系列 API 都是針對某個 Index 來操作。

PUT /<index>/_alias/<alias>
POST /<index>/_alias/<alias>
PUT /<index>/_aliases/<alias>
POST /<index>/_aliases/<alias>

直接以官方的例子來看:

  1. 建立一個 2030 的 alias,並直接指向 logs_20302801 這個 index。

PUT /logs_20302801/_alias/2030
  1. 建立時,也可以指定 routing 的值,或是 filter 的條件,來讓這個 alias 有限定的用途。

PUT /users/_alias/user_12
{
  "routing" : "12",
  "filter" : {
    "term" : {
      "user_id" : 12
    }
  }
}

使用 alias 搭配 filter 的用法,可以想像是一些 Relational Database 有提供的 View 這樣的功能,透過指定的條件,讓這個 alias 是具有某些限定的存取行為、或是具有明確使用意義的。例如 Movies 的資料有一個 Index,但可以建立 Top Movies 或是 Action Movies 這樣的 alias ,配合特定的 filter 條件,但是都指向 Movies 的 Index。

Update index alias API

這個 API 不會綁定從某個 Index 出發,而且是可以同時進行多項 alias 相關的操作,整個請求所包含的動作會被封裝是一個 atomic 的操作,也就是成功就會全部都成功,其中有一個失敗就會全部都失敗,不會只有某部份成功而其他部份發生失敗。

POST /_aliases
  1. 新增一個 alias alias2 並指向 index my-index-000001 並且套用特定 filter 的規則:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "my-index-000001",
        "alias": "alias2",
        "filter": { "term": { "user.id": "kimchy" } }
      }
    }
  ]
}
  1. 透過 atomic 的特性,幫 alias 改名字,不會在改名的過程中,造成服務中斷。

POST /_aliases
{
  "actions" : [
    { "remove" : { "index" : "test1", "alias" : "alias1" } },
    { "add" : { "index" : "test1", "alias" : "alias2" } }
  ]
}
  1. 建立 alias 並搭配 filter 與 routing 的設定:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test",
        "alias": "alias2",
        "search_routing": "1,2",
        "index_routing": "2"
      }
    }
  ]
}

指定 routing 可以減少讓 request 跑到其他 shard 運作的時間,能直接強制導到某些 shard 身上。

  1. 使用 alias 時,若會需要將資料透過 alias 來寫入,必預要明確的標示哪個 index 是 is_write_index ,這部份的設置在建立 alias 與 index 的關係時,可以一併加上宣告:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test",
        "alias": "alias1",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "test2",
        "alias": "alias1"
      }
    }
  ]
}

可透過 atomic 的特性,來把 write_index 的能力,從某個 index 移到另個 index 身上:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test",
        "alias": "alias1",
        "is_write_index": false
      }
    }, {
      "add": {
        "index": "test2",
        "alias": "alias1",
        "is_write_index": true
      }
    }
  ]
}

Best Practices

  • 盡量全面使用 Index Aliases 來存取 index:因為要修改 mappings 中已存在的設定時,會需要重建 index、並重新 re-index 資料,因此使用 alias 的話,可以先建立新的 index (新的名字可再額外加上版號,例如 v2 ),資料從 v1 搬到 v2 後,再把 alias 直向新的 v2 index,驗證完成後再刪掉舊的 v1 index,index 的使用者們因為指向 alias ,也就不用全面更改成新的 index 名字了。

  • 善用 Index Aliases 搭配 Filter:

    1. 減少使用端的查詢複雜化,先將適度的 filter 封裝在 alias 中,明確的定義這個 alias 提供的內容,讓使用端不用處理這部份的邏輯。

    2. 權限的管理,限定使用者只能存取特定的子集合的資料、或是限定時間內的資料,而配合 security 的權限處理,可避免使用者存取到不應取得的資料,或是一口氣查詢太多舊資料導致效能的影響。

  • 配合 routing 來指定資料寫入到特定的 shard:資料在 Indexing 進入 Elasticsearch 時,會依照 routing value 來進行 hash 的運算 (預設是使用 _id 當 routing value,並使用 murmur3 的 hash 演算法。),並依照計算出來的值與 primary shard 的數量來進行 mod 運算,以決定資料要寫入到哪個 shard 身上,但如果有指定的 routing value,就可以決定同樣 routing value 的資料會被計算放到同樣的 shard 身上,這樣對於 performance 的優化或是資料的管理上都可以有許多應用的方式,而 alias 就能配合指定 routing 的值來達到這類型的運用。

  • 配合 index Lifecycle Management (ILM):隨著時間增長的資料,使用 ILM 來管理這些資料時,其中就是搭配 Index Alias 來切換寫入時要指定到的實體 Index 在哪邊。

參考資料

API 的部份細節直接參考 ,這邊只快速列出幾個例子:

📗
官方文件 - Update index alias API
官方文件 - cat aliases API
官方文件 - Add index alias
官方文件 - Update index alias API
Index Alias - Elasticsearch Best Practice