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

Documents Indexing — Single ဖြင့်၊ Bulk API ဖြင့်

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

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

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

Document တစ်ခုကို index ချချင်ရင် `PUT /index/_doc/id` (ID ကိုယ့်ဘာသာသတ်မှတ်) သို့မဟုတ် `POST /index/_doc` (Elasticsearch က auto-generate ID ပေး) သုံးနိုင်ပါတယ်—ID ကို ကိုယ့်ဘာသာသတ်မှတ်ထားရင် same ID ပြန်ပို့ရင် document ကို update (replace) လုပ်တာဖြစ်ပြီး auto-generate ID ဆိုရင်တော့ request တစ်ခုစီက document အသစ်တစ်ခု ဖန်တီးပါတယ်။ Document တစ်ခုချင်း request တစ်ခါက network round trip တစ်ခါ ဆိုပြီး document ရာနဲ့ချီ index ချမယ်ဆိုရင် round trip overhead ကြောင့် လွန်စွာနှေးနိုင်ပါတယ်—ဒါကို ဖြေရှင်းဖို့ `_bulk` API က multiple index/update/delete operations တွေကို single HTTP request တစ်ခုထဲ NDJSON (newline-delimited JSON) format နဲ့ပို့ပေးလို့ round trip တစ်ခါတည်းနဲ့ document ထောင်ချီကို process လုပ်နိုင်ပါတယ်။ Bulk request ရဲ့ format ကတော့ line pairs ဖြစ်ပြီး—action/metadata line (ဘယ် operation, ဘယ် index, ဘယ် ID) တစ်ကြောင်းနောက် document data line တစ်ကြောင်း ဆက်တိုက်ပါလာရပါတယ်—format မှားရင် whole request fail ဖြစ်နိုင်ပါတယ်။ Bulk request တစ်ခုလုံး HTTP status 200 ပြန်ရင်တောင် individual item တစ်ခုချင်းစီမှာ error ဖြစ်နိုင်လို့ response ထဲက `errors` field နဲ့ item တစ်ခုချင်းစီရဲ့ status ကို အမြဲစစ်ရပါတယ်—ဒါက post office ကနေ package အများကြီး တစ်ခါတည်း post လိုက်တာနဲ့တူပါတယ်—overall shipment succeeded လို့ confirmation ရပေမယ့် package တစ်ခုနှစ်ခုက address မှားပြီး ပြန်လာနိုင်ပါသေးတယ်။

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

Tutorial Platform ရဲ့ PostgreSQL `tutorials` table ကို ပထမဆုံးအကြိမ် Elasticsearch ဆီ sync ဆွဲတဲ့အခါ tutorial 5,000 ခုကို document တစ်ခုချင်း `POST` ဖြင့် ပို့မယ်ဆိုရင် round trip 5,000 ကြောင့် ရာနှင့်ချီသော စက္ကန့်ကြာနိုင်ပါတယ်—ဒါ့အစား `_bulk` API ကို chunks (ဥပမာ 500 documents/request) ဖြင့် သုံးမယ်။ Bulk response ထဲက `errors: true` ရှိနေရင် ဘယ် item တွေ fail ဖြစ်ခဲ့လဲ log ထုတ်ပြီး retry ကို failed items ချည်းသာလုပ်မယ်—success ဖြစ်ခဲ့တဲ့ items တွေကို ပြန်မ process ဖို့ပါ။ Single document update (ဥပမာ author က tutorial တစ်ခု edit) ကိုတော့ `PUT /tutorials/_doc/<postgresql-id>` ဖြင့် ID တူအောင် သုံးမယ်—PostgreSQL row ID ကို Elasticsearch document ID အဖြစ် တစ်ထပ်တည်းသုံးထားလို့ sync logic ရိုးရှင်းသွားပါတယ်။

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

http
PUT /tutorials/_doc/42
{
  "title": "Redis Basics",
  "tags": ["redis", "cache"],
  "publishedAt": "2026-08-01"
}

POST /_bulk
{ "index": { "_index": "tutorials", "_id": "43" } }
{ "title": "GraphQL Basics", "tags": ["graphql", "api"], "publishedAt": "2026-08-15" }
{ "index": { "_index": "tutorials", "_id": "44" } }
{ "title": "Elasticsearch Basics", "tags": ["elasticsearch", "search"], "publishedAt": "2026-08-29" }
You should see
Single document တစ်ခု index ချနိုင်ပြီး `_bulk` request ဖြင့် document နှစ်ခုကို တစ်ခါတည်း index ချနိုင်မည်။

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

Tutorial 3 ခု (id 100, 101, 102) ကို `_bulk` API တစ်ခုတည်းနဲ့ index ချသော request ရေးပါ—id 101 ကို intentionally malformed document line (missing closing brace) ဖြစ်အောင်လုပ်ပြီး response ထဲ ဘယ်လိုပြသမလဲ ခန့်မှန်းရေးပါ။

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

Document ပေါင်းများစွာကို single-document `PUT`/`POST` request loop ထဲ တစ်ခုချင်းပို့ခြင်း—round trip overhead ကြောင့် performance ဆိုးရွားစွာ ကျဆင်းနိုင်ပါတယ်။

Bulk request response HTTP status 200 ရလို့ document အားလုံး success ဖြစ်တယ်လို့ ယူဆပြီး response body ထဲက `errors`/item-level status ကို စစ်မကြည့်ခြင်း။

Elasticsearch Guide — Bulk APIElastic

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

  • Document ပေါင်းများစွာကို single-document `PUT`/`POST` request loop ထဲ တစ်ခုချင်းပို့ခြင်း—round trip overhead ကြောင့် performance ဆိုးရွားစွာ ကျဆင်းနိုင်ပါတယ်။
  • Bulk request response HTTP status 200 ရလို့ document အားလုံး success ဖြစ်တယ်လို့ ယူဆပြီး response body ထဲက `errors`/item-level status ကို စစ်မကြည့်ခြင်း။
  • နမူနာ query/mutation ကို production cluster ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

Tutorial 3 ခု (id 100, 101, 102) ကို `_bulk` API တစ်ခုတည်းနဲ့ index ချသော request ရေးပါ—id 101 ကို intentionally malformed document line (missing closing brace) ဖြစ်အောင်လုပ်ပြီး response ထဲ ဘယ်လိုပြသမလဲ ခန့်မှန်းရေးပါ။

You'll know it worked when: Single document တစ်ခု index ချနိုင်ပြီး `_bulk` request ဖြင့် document နှစ်ခုကို တစ်ခါတည်း index ချနိုင်မည်။

Documents Indexing — Single ဖြင့်၊ Bulk API ဖြင့် | Thuta Learning