Thuta Learning
ရှာဖွေရန်
Elasticsearch
IntermediateData & Databasesbeginner

Aggregations — Metric နှင့် Bucket

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Aggregations — Metric နှင့် Bucket concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Elasticsearch query/code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Aggregations ဆိုတာ Elasticsearch ရဲ့ analytics framework ဖြစ်ပြီး "documents ဘယ်တွေက match ဖြစ်လဲ" ဆိုတဲ့ search question ကနေ "data ရဲ့ pattern/summary ဘာလဲ" ဆိုတဲ့ analytics question ဆီ ပြောင်းလဲပေးပါတယ်—SQL ရဲ့ `GROUP BY` + `AVG()`/`COUNT()` combo နဲ့ ရည်ရွယ်ချက်တူပါတယ်။ Metric aggregation (`avg`, `sum`, `min`, `max`, `stats`) ကတော့ numeric field တစ်ခုကို input အဖြစ်ယူပြီး single computed value တစ်ခု (ဥပမာ tutorial အားလုံးရဲ့ average lesson count) ကို ပြန်ပေးပါတယ်။ Bucket aggregation (`terms`, `date_histogram`, `range`) ကတော့ document တွေကို criteria အလိုက် group (bucket) များအဖြစ် ခွဲထားပေးပါတယ်—`terms` aggregation ဆိုရင် `keyword` field တစ်ခုရဲ့ unique value တစ်ခုစီအတွက် bucket တစ်ခုစီဆောက်ပြီး document count ကို ပြန်ပေးလေ့ရှိပါတယ်—ဒါကြောင့် `terms` aggregation ကို `keyword`/numeric field ပေါ်မှာသာ run သင့်ပါတယ် (`text` field ပေါ်က run ရင် token တစ်ခုချင်းစီ (individual word) အတွက် bucket ဖြစ်သွားလို့ ကိုယ်ရည်ရွယ်တဲ့ result မဟုတ်တော့ပါ)။ Aggregation တွေကို search query ဘေးနားမှာ တွဲထည့်နိုင်ပြီး—`query` ကနေ document set ကို ဦးစွာ narrow လုပ်ပြီးမှ ဒီ narrow ဖြစ်သွားတဲ့ document set အပေါ်မှာသာ aggregation ကို run ပေးတာဖြစ်လို့ "filtered analytics" ကို request တစ်ခုတည်းနဲ့ ရနိုင်ပါတယ်။ ဒါကို survey data တစ်ခုကနေ pie chart (bucket = category count) နှင့် average score (metric) နှစ်ခုလုံးကို တစ်ပြိုင်နက် ထုတ်ယူသလိုမျိုး တွေးလို့ရပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Tutorial Platform ရဲ့ admin analytics dashboard မှာ "topic တစ်ခုစီအောက်မှာ tutorial ဘယ်နှစ်ခု ရှိလဲ" ဆိုတဲ့ chart ကို `tags` field ပေါ်မှာ `terms` bucket aggregation တစ်ခုနဲ့ တစ်ခါတည်း ရနိုင်ပါတယ်—PostgreSQL ဆိုရင် `GROUP BY tag` query တစ်ခု ဆက်ရေးရမှာဖြစ်ပေမယ့် Elasticsearch မှာတော့ search query ဘေးမှာ `aggs` object ကို တွဲထည့်လိုက်ရုံပါ။ "published in the last 30 days" query တစ်ခု filter ထားပြီးမှ ဒီ narrowed set ပေါ်မှာ `avg` aggregation ဖြင့် "recent tutorials ရဲ့ average lesson count" ကိုပါ တစ်ပြိုင်နက် ထုတ်ယူနိုင်ပါတယ်—search + analytics ကို request တစ်ခုတည်းနဲ့ ရလိုက်တာဖြစ်ပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

http
GET /tutorials/_search
{
  "size": 0,
  "query": {
    "range": { "publishedAt": { "gte": "now-30d" } }
  },
  "aggs": {
    "tutorials_per_tag": {
      "terms": { "field": "tags" }
    },
    "avg_lesson_count": {
      "avg": { "field": "lessonCount" }
    }
  }
}
You should see
Last 30 days ထုတ်ဝေထားသော tutorials အတွက် tag တစ်ခုစီရဲ့ document count buckets နှင့် average lesson count ရမည်။

၅ မိနစ် စမ်းကြည့်

`difficulty` field ပေါ်မှာ `terms` bucket aggregation နှင့် `lessonCount` field ပေါ်မှာ `max` metric aggregation ကို query တစ်ခုတည်းထဲ တွဲထည့်ရေးပါ (`size: 0` သုံးရမည်)။

သတိလေးတစ်ချက်

`terms` bucket aggregation ကို `text` type field (analyzer ကနေ tokenize လုပ်ထားသော field) ပေါ်မှာ run ခြင်း—word တစ်ခုချင်းစီအတွက် bucket ဖြစ်သွားပြီး intended category count နဲ့ လုံးဝကွဲသွားနိုင်ပါတယ်။

Aggregation result ချည်းသာလိုချင်ပြီး `size` parameter ကို default (10) အတိုင်း ချန်ထားခြင်း—document 10 ခုကို hits array ထဲ ထပ်ပြန်ပါလာပြီး response size/bandwidth ကို မလိုအပ်ဘဲ တိုးစေနိုင်ပါတယ်—`size: 0` ထည့်သင့်ပါတယ်။

Elasticsearch Guide — AggregationsElastic

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `terms` bucket aggregation ကို `text` type field (analyzer ကနေ tokenize လုပ်ထားသော field) ပေါ်မှာ run ခြင်း—word တစ်ခုချင်းစီအတွက် bucket ဖြစ်သွားပြီး intended category count နဲ့ လုံးဝကွဲသွားနိုင်ပါတယ်။
  • Aggregation result ချည်းသာလိုချင်ပြီး `size` parameter ကို default (10) အတိုင်း ချန်ထားခြင်း—document 10 ခုကို hits array ထဲ ထပ်ပြန်ပါလာပြီး response size/bandwidth ကို မလိုအပ်ဘဲ တိုးစေနိုင်ပါတယ်—`size: 0` ထည့်သင့်ပါတယ်။
  • နမူနာ query/mutation ကို production cluster ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

`difficulty` field ပေါ်မှာ `terms` bucket aggregation နှင့် `lessonCount` field ပေါ်မှာ `max` metric aggregation ကို query တစ်ခုတည်းထဲ တွဲထည့်ရေးပါ (`size: 0` သုံးရမည်)။

You'll know it worked when: Last 30 days ထုတ်ဝေထားသော tutorials အတွက် tag တစ်ခုစီရဲ့ document count buckets နှင့် average lesson count ရမည်။

Aggregations — Metric နှင့် Bucket | Thuta Learning