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

Mapping နှင့် Field Types အနက်ရှုင်း

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

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

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

Elasticsearch field type ရွေးချယ်မှုဟာ ရိုးရှင်းတဲ့ decision လို့ ထင်ရပေမယ့် သင့်ရဲ့ search/aggregation capability ကို အခြေခံကတည်းက ဆုံးဖြတ်ပေးနေပါတယ်။ `text` type field က analyzer ကနေတစ်ဆင့် tokenize ဖြစ်ပြီး full-text search (`match`) အတွက်ဖြစ်ပေမယ့် exact-match filter သို့ aggregation (terms bucket) မှာသုံးလို့မရပါ—value တစ်ခုလုံးကို token အသေးလေးများအဖြစ် ခွဲထားလို့ "original value" ကို ပြန်မရနိုင်တော့ပါဘူး။ `keyword` type ကတော့ analyzer မကုန်ဘဲ value တစ်ခုလုံးကို single token အနေနဲ့ index ထားလို့ exact match, sort, aggregation, filter မှာသင့်တော်ပေမယ့် full-text search မှာတော့ inefficient ဖြစ်ပါတယ် (space တစ်ခုတောင်ကွာရင် match မရတော့ပါ)။ Numeric types (`integer`, `long`, `float`, `double`) ကို ရွေးရာမှာ range/aggregation performance ကို optimize လုပ်ဖို့ range ကို လုံလောက်စွာ ဖုံးလွှမ်းနိုင်သလောက် precision အနည်းဆုံး type ရွေးသင့်ပါတယ်—`long` ကို လိုအပ်တာထက် ပိုသုံးရင် storage/memory waste ဖြစ်ပါတယ်။ `date` type ကို ISO 8601 format standard အနေနဲ့ index ချထားရင် Elasticsearch က automatically date math (range query, date histogram aggregation) ကို support ပေးပါတယ်—string type အဖြစ် index ချထားရင် ဒီ capability အားလုံးပျောက်ပါတယ်။ ဒါကို sharp knife (`keyword`, precise cuts) vs. food processor (`text`, blend everything into searchable chunks) နှစ်ခုနဲ့ တွေးလို့ရပါတယ်—value တစ်ခုတည်းကို field နှစ်ခုမှာ multi-field mapping (`text` + `.keyword` sub-field) ဖြင့် နှစ်မျိုးလုံးရဖို့ ဒီဇိုင်းလုပ်ထားလေ့ရှိပါတယ်။

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

Tutorial Platform ရဲ့ `title` field ကို `text` + `.keyword` sub-field (multi-field mapping) ဖြင့် ဒီဇိုင်းလုပ်မယ်—search box မှာတော့ `title` (text) ကို `match` query သုံးမယ်၊ admin dashboard ရဲ့ "sort by title alphabetically" feature မှာတော့ `title.keyword` ကိုသုံးမယ်—`text` field ပေါ်မှာ sort လုပ်ခဲ့ရင် token order ကြောင့် expected alphabetical order နဲ့ ကွဲသွားမှာဖြစ်လို့ပါ။ `publishedAt` ကို `date` type ရွေးထားလို့ "last 7 days" filter feature က range query တစ်ခုတည်းနဲ့ ရိုးရှင်းစွာ implement လုပ်နိုင်ပါတယ်—`string` type ရွေးခဲ့ရင် date comparison logic ကို application layer မှာ manually implement ရမှာဖြစ်ပါတယ်။ `lessonCount` field ကို `integer` (small range) သုံးမယ်—`long` မလိုအပ်ဘူးလို့ကြိုသိလို့ storage optimize ပါတယ်။

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

json
PUT /tutorials
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "keyword": { "type": "keyword", "ignore_above": 256 }
        }
      },
      "lessonCount":  { "type": "integer" },
      "publishedAt":  { "type": "date", "format": "strict_date_optional_time" },
      "price":        { "type": "float" }
    }
  }
}
You should see
`title` field ကို `match` (full-text) နှင့် `title.keyword` (exact sort) နှစ်မျိုးလုံး သုံးနိုင်သော mapping ရမည်။

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

`products` index အတွက် `sku` (exact match လိုသော code), `description` (full-text search လိုသော), `stockCount` (small integer), `lastRestockedAt` (date) field လေးခုအတွက် mapping ရေးပါ။

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

Category/tag/status field (dropdown value တွေဖြစ်လို့ exact match/filter/aggregation ချည်းသာလိုအပ်) ကို `text` type လို့ mapping လုပ်ခြင်း—terms aggregation run လို့မရတော့ပြီး filter result မမှန်နိုင်ပါ။

`date` field ကို ISO format မဟုတ်ဘဲ locale-specific string (ဥပမာ "08/29/2026") အဖြစ် index ချခြင်း—date range query/date histogram aggregation အလုပ်မလုပ်တော့ပါ။

Elasticsearch Guide — Field Data TypesElastic

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

  • Category/tag/status field (dropdown value တွေဖြစ်လို့ exact match/filter/aggregation ချည်းသာလိုအပ်) ကို `text` type လို့ mapping လုပ်ခြင်း—terms aggregation run လို့မရတော့ပြီး filter result မမှန်နိုင်ပါ။
  • `date` field ကို ISO format မဟုတ်ဘဲ locale-specific string (ဥပမာ "08/29/2026") အဖြစ် index ချခြင်း—date range query/date histogram aggregation အလုပ်မလုပ်တော့ပါ။
  • နမူနာ query/mutation ကို production cluster ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

`products` index အတွက် `sku` (exact match လိုသော code), `description` (full-text search လိုသော), `stockCount` (small integer), `lastRestockedAt` (date) field လေးခုအတွက် mapping ရေးပါ။

You'll know it worked when: `title` field ကို `match` (full-text) နှင့် `title.keyword` (exact sort) နှစ်မျိုးလုံး သုံးနိုင်သော mapping ရမည်။

Mapping နှင့် Field Types အနက်ရှုင်း | Thuta Learning