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

Core Data Model — Indices, Documents, Mapping

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

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

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

Elasticsearch မှာ index ဆိုတာ PostgreSQL ရဲ့ table နဲ့ ခြားနားပေမယ့် ဆင်တူတဲ့ concept တစ်ခုဖြစ်ပြီး similar structure ရှိတဲ့ JSON document များစုစည်းထားတဲ့ container တစ်ခုပါ—ဥပမာ `tutorials` index က tutorial document များအားလုံးကို သိမ်းထားနိုင်ပါတယ်။ Document တစ်ခုစီက unique `_id` ရှိတဲ့ JSON object ဖြစ်ပြီး PostgreSQL row နဲ့ ခြားနားချက်ကတော့ document က nested object၊ array of objects စတာတွေကို native အနေနဲ့ ထားနိုင်တာပါ—join table မလိုအပ်ပါဘူး။ Mapping ကတော့ index တစ်ခုအောက်က field တစ်ခုစီရဲ့ data type (text, keyword, integer, date, စသည်) ကို ကြေညာထားတဲ့ schema-like definition ဖြစ်ပြီး PostgreSQL ရဲ့ `CREATE TABLE` column definitions နဲ့ ရည်ရွယ်ချက်တူပါတယ်။ ဒါပေမယ့် PostgreSQL နဲ့ တကယ်ကွာတဲ့ အချက်တစ်ခုက mapping ကို explicit မထားရင် Elasticsearch က dynamic mapping ကနေ field type ကို auto-detect လုပ်ပေးနိုင်တာပါ—ဒါက quick prototyping အတွက်အဆင်ပြေပေမယ့် production မှာတော့ auto-detected type က ကိုယ်လိုချင်တဲ့ type နဲ့ မတူနိုင်လို့ risk ရှိပါတယ် (ဥပမာ `price` field ကို string လို့ auto-detect ဖြစ်သွားနိုင်ခြင်း)။ Mapping ကို ဇာတ်ကားတစ်ခုရဲ့ script format လိုတွေးလို့ရပါတယ်—script format (schema) က ဇာတ်ကားထဲမှာ ဘယ် element (dialogue, action, scene heading) ကို ဘယ်လို parse ဖတ်ရမလဲ ကြိုသတ်မှတ်ပေးသလိုပဲ mapping ကလည်း Elasticsearch ဘယ် field ကို ဘယ်လို index/search ဖတ်ရမလဲ ကြိုသတ်မှတ်ပေးပါတယ်။

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

Tutorial Platform search index ကို `tutorials` ဆိုပြီး design မယ်—document တစ်ခုစီမှာ `title`, `body`, `tags` (array), `authorName`, `publishedAt`, `difficulty` fields ပါမယ်။ Mapping ကို explicit ဖြင့် `title` ကို full-text search ရဖို့ `text` type, `tags` ကို exact filter/facet အတွက် `keyword` type, `publishedAt` ကို `date` type ဟုသတ်မှတ်ထားမယ်—dynamic mapping ကို trust မလုပ်ဘဲ ကိုယ့်ဘာသာ decide ချမယ်။ PostgreSQL ရဲ့ `tutorials` table row တစ်ခုက Elasticsearch document တစ်ခုအဖြစ် transform ဖြစ်တဲ့အခါ `lessons` table က related rows တွေကို document ထဲမှာ nested array တစ်ခုတည်းအဖြစ် flatten လုပ်ပေးမယ်—join query ကို search-time မှာ လုံးဝ လိုအပ်တော့မှာ မဟုတ်ပါ။

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

json
PUT /tutorials
{
  "mappings": {
    "properties": {
      "title":       { "type": "text" },
      "body":        { "type": "text" },
      "tags":        { "type": "keyword" },
      "authorName":  { "type": "keyword" },
      "publishedAt": { "type": "date" },
      "difficulty":  { "type": "keyword" }
    }
  }
}
You should see
Explicit mapping ပါသော `tutorials` index တစ်ခု ဖန်တီးနိုင်မည်။

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

`comments` index တစ်ခုအတွက် mapping ရေးပါ—`body` (text), `authorId` (keyword), `createdAt` (date), `upvotes` (integer) fields ပါဝင်ရမည်။

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

Mapping ကို explicit မထားဘဲ index ကို straight away data ထည့်ပြီး dynamic mapping ကို trust လုပ်ခြင်း—`tags` field ကို `text` အဖြစ် auto-detect ဖြစ်သွားပြီး exact-match filtering မရနိုင်တော့ခြင်းလို ပြဿနာဖြစ်နိုင်ပါတယ်။

Document structure ကို PostgreSQL row structure တစ်ခုတည်းအတိုင်း တိုက်ရိုက် copy လုပ်ပြီး related table data (ဥပမာ lessons) ကို nested array အဖြစ် denormalize မလုပ်ခြင်း—search query တိုင်းမှာ join-like logic ပြန်လိုအပ်လာပါတယ်။

Elasticsearch Guide — Documents and IndicesElastic

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

  • Mapping ကို explicit မထားဘဲ index ကို straight away data ထည့်ပြီး dynamic mapping ကို trust လုပ်ခြင်း—`tags` field ကို `text` အဖြစ် auto-detect ဖြစ်သွားပြီး exact-match filtering မရနိုင်တော့ခြင်းလို ပြဿနာဖြစ်နိုင်ပါတယ်။
  • Document structure ကို PostgreSQL row structure တစ်ခုတည်းအတိုင်း တိုက်ရိုက် copy လုပ်ပြီး related table data (ဥပမာ lessons) ကို nested array အဖြစ် denormalize မလုပ်ခြင်း—search query တိုင်းမှာ join-like logic ပြန်လိုအပ်လာပါတယ်။
  • နမူနာ query/mutation ကို production cluster ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

`comments` index တစ်ခုအတွက် mapping ရေးပါ—`body` (text), `authorId` (keyword), `createdAt` (date), `upvotes` (integer) fields ပါဝင်ရမည်။

You'll know it worked when: Explicit mapping ပါသော `tutorials` index တစ်ခု ဖန်တီးနိုင်မည်။

Core Data Model — Indices, Documents, Mapping | Thuta Learning