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

Compound Queries — bool, must vs filter vs should

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

  • Compound Queries — bool, must vs filter vs should concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Elasticsearch query/code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

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

Real-world search တွေမှာ criteria တစ်ခုတည်းနဲ့ ပြီးလေ့မရှိပါ—"redis" ဆိုတဲ့ word ပါပြီး `difficulty: beginner` ဖြစ်ပြီး `publishedAt` က ၂၀၂၆ ခုနှစ်အတွင်းလိုမျိုး criteria များစွာကို ပေါင်းစပ်ရလေ့ရှိပါတယ်—`bool` query ကတော့ ဒီလို compound condition တွေကို clause လေးမျိုး (`must`, `filter`, `should`, `must_not`) ဖြင့် ပေါင်းစပ်ပေးနိုင်ပါတယ်။ `must` clause ထဲက query တွေက document ကို match ဖြစ်ရန်လိုအပ်ပြီး `_score` ကိုပါ contribute လုပ်ပါတယ်—relevance ranking ကို အရေးကြီးတဲ့ full-text search condition တွေကို ဒီနေရာမှာ ထားရပါတယ်။ `filter` clause ထဲက query တွေကလည်း document ကို match ဖြစ်ရန်လိုအပ်ပေမယ့် `_score` ကို လုံးဝ မထည့်ပါ (relevance မတွက်ဘူး—yes/no binary decision ချည်းသာ) ဒါကြောင့် Elasticsearch က filter clause တွေရဲ့ result ကို internally cache လုပ်ထားနိုင်ပြီး request တစ်ခုနောက်တစ်ခု repeat filter (ဥပမာ `status: published`) ကို disk scoring calculation လုံးဝမလိုဘဲ memory ကနေ instant ပြန်ပေးနိုင်ပါတယ်။ `should` clause ကတော့ optional ဖြစ်ပြီး match ဖြစ်ရင် score ကို boost လုပ်ပေးနိုင်ပါတယ် (minimum_should_match မသတ်မှတ်ထားရင်)။ ဒါကို library search station တစ်ခုနဲ့ တွေးလို့ရပါတယ်—librarian က "ဘယ်စာအုပ်တွေက ဒီ topic နဲ့ ပိုနီးစပ်လဲ" ဆိုတဲ့ relevance judgement ကို must clause အတွက်ပဲ လုပ်ပြီး "available in library" ဆိုတဲ့ yes/no fact ကိုတော့ judgement မလိုဘဲ instant checklist အဖြစ် filter clause က ကိုင်တွယ်တာနဲ့တူပါတယ်—checklist က judgement မလိုလို့ ပိုမြန်ပါတယ်။

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

Tutorial Platform ရဲ့ search filter UI မှာ user က keyword "redis" ရိုက်ထည့်ပြီး "Beginner" difficulty checkbox ကိုတွေးလိုက်ရင် `bool` query တစ်ခုတည်းထဲမှာ `must: [{ match: { body: "redis" } }]` နဲ့ `filter: [{ term: { difficulty: "beginner" } }]` ကို ပေါင်းသုံးမယ်—keyword matching ကို relevance-based (`must`) ထားပြီး difficulty selection ကိုတော့ binary criterion (`filter`) အဖြစ် ထားခြင်းဖြင့် irrelevant scoring cost ကို ရှောင်နိုင်ပါတယ်။ `must` clause ထဲမှာပဲ difficulty filter ထည့်ခဲ့ရင်တောင် correctness တူပေမယ့် performance ပိုနှေးနိုင်ပါတယ်—Elasticsearch က difficulty match ကိုပါ relevance score အနေနဲ့ compute ပေးမှာဖြစ်လို့ cache လုပ်ထားလို့ မရနိုင်တော့ပါဘူး။

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

http
GET /tutorials/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "body": "redis" } }
      ],
      "filter": [
        { "term": { "difficulty": "beginner" } },
        { "range": { "publishedAt": { "gte": "2026-01-01" } } }
      ]
    }
  }
}
You should see
"redis" ပါပြီး beginner difficulty၊ 2026 ထုတ်ဝေထားသော tutorials စာရင်း relevance-ranked ရမည်။

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

"caching" keyword ပါပြီး `tags` ထဲ "redis" ပါပြီး `difficulty` က "advanced" မဟုတ်တဲ့ tutorials ရှာသော `bool` query တစ်ခုရေးပါ (`must`, `filter`, `must_not` သုံးမျိုးလုံးသုံးရမည်)။

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

Binary criteria (status, category, exact ID) ကို `filter` မထားဘဲ `must` clause ထဲထည့်ခြင်း—correctness တူပေမယ့် unnecessary relevance scoring ကြောင့် performance ကျဆင်းနိုင်ပါတယ်။

`should` clause ကို `bool` query ထဲထည့်ရင် document အားလုံး match ဖြစ်ရန်မလိုဘူးလို့ မှတ်ထားပြီး `must`/`filter` မပါဘဲ `should` ချည်းတစ်ခုတည်းသုံးမိခြင်း—default behavior အရ `should` clause တစ်ခုတည်းရှိရင် အနည်းဆုံး တစ်ခုတော့ match ရမယ်ဆိုပေမယ့် intention နဲ့ query logic မကိုက်ညီနိုင်ပါ။

Elasticsearch Guide — Bool QueryElastic

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

  • Binary criteria (status, category, exact ID) ကို `filter` မထားဘဲ `must` clause ထဲထည့်ခြင်း—correctness တူပေမယ့် unnecessary relevance scoring ကြောင့် performance ကျဆင်းနိုင်ပါတယ်။
  • `should` clause ကို `bool` query ထဲထည့်ရင် document အားလုံး match ဖြစ်ရန်မလိုဘူးလို့ မှတ်ထားပြီး `must`/`filter` မပါဘဲ `should` ချည်းတစ်ခုတည်းသုံးမိခြင်း—default behavior အရ `should` clause တစ်ခုတည်းရှိရင် အနည်းဆုံး တစ်ခုတော့ match ရမယ်ဆိုပေမယ့် intention နဲ့ query logic မကိုက်ညီနိုင်ပါ။
  • နမူနာ query/mutation ကို production cluster ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နှင့် recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

"caching" keyword ပါပြီး `tags` ထဲ "redis" ပါပြီး `difficulty` က "advanced" မဟုတ်တဲ့ tutorials ရှာသော `bool` query တစ်ခုရေးပါ (`must`, `filter`, `must_not` သုံးမျိုးလုံးသုံးရမည်)။

You'll know it worked when: "redis" ပါပြီး beginner difficulty၊ 2026 ထုတ်ဝေထားသော tutorials စာရင်း relevance-ranked ရမည်။

Compound Queries — bool, must vs filter vs should | Thuta Learning