工作坊實作內容

DevOpsDays Taipei 2023 工作坊 - 如何在 Elasticsearch 實現敏捷的資料建模與管理

事先定義好的 Data Model

明確的欄位定義

Create Index or Update Mapping

PUT my-index
{
  "mappings": {
    "properties": { 
      "name":     { "type": "text"    },
      "age":      { "type": "integer" },
      "birthday": { "type": "date"    },
      "email":    { "type": "keyword" },
      "salary":   { "type": "double"  }
    }
  }
}
PUT my-index/_mapping
{
  "properties": { 
    "name":     { "type": "text"    },
    "age":      { "type": "integer" },
    "birthday": { "type": "date"    },
    "email":    { "type": "keyword" },
    "salary":   { "type": "double"  }
  }
}

Sprint 1: 收集電商訂單 Logs

配合的後端工程師告訴你,這個 Sprint 會完成一個訂單系統,右方是訂單的資料結構,每筆訂單完成都會產生一筆 Log 記錄到 Elasticsearch,未來 Data Team 會要用這些訂單資料做數據分析。

溝通好會將訂單 Logs 資料寫入到 devopsdays-taipei-2023-ec-order 的 index 之中。

Testing Data for Sprint 1

Sprint 2: Index Mapping 的版本选代

動態新增欄位 Dynamic Mapping

Dynamic Template 範例

  • 將 String 型態的欄位,定義成 keyword,而不是預設的 text + keyword

  • 將 String 型態的欄位,且欄位名字是 long_ 開頭,同時不是 _text 結尾,就定義成 long 型態。

  • name 物件裡除了 middle 之外的所有欄位,都 copy 到 full_name 的欄位,並指定為 text 型態。

  • 先針對字串欄位給予定義,再來針對所有非字串的欄位,關閉 doc_values

Sprint 3: 建立適用的 Dynamic Template

無法事先定義好 Data Model

Runtime Field 的使用方式

在 Searching 時指定 Runtime Field

  • Searching 時指定 runtime field

在 Mapping 中定義 Runtime Field

Sprint 4: 事先所定義的欄位不足

Sprint 5: 當我們想將 Runtime Field 正式定義

Data Model 的事後修改

Update by Query

Sprint 6: 將舊資料也轉成 Schema on Write

Last updated