Menggabungkan Kueri dengan Bool
Jelajahi kueri `bool` untuk menggabungkan beberapa kueri menggunakan klausa `must`, `should`, `must_not`, dan `filter` demi logika yang kompleks.
Menggabungkan Kueri dengan Bool adalah pelajaran Elasticsearch & Full Text Search Systems gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Elasticsearch & Full Text Search Systems, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Elasticsearch & Full Text Search Systems mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Combine Search Logic
Welcome to combining queries! Up until now, we've looked at single search conditions. But real-world searches often need multiple criteria.
Elasticsearch's bool query is your go-to for building complex search logic. It lets you combine several queries using AND, OR, and NOT conditions.
Anatomy of a Bool Query
A bool query acts like a container for other queries. It has four main clauses:
must: All queries inside must match (AND).should: At least one query inside should match (OR).must_not: Queries inside must NOT match (NOT).filter: Queries inside must match, but don't affect relevancy score.
We'll explore each one!
`must`: All Conditions Must Match
The must clause is like an "AND" operator. All queries listed within must must be true for a document to be considered a match.
Documents that satisfy all must clauses contribute positively to the relevancy score.
Example: `must` Clause
This query finds products that are both "laptop" AND "gaming".
{
"query": {
"bool": {
"must": [
{ "match": { "category": "laptop" } },
{ "match": { "tags": "gaming" } }
]
}
}
}`should`: At Least One Condition
The should clause is like an "OR" operator. If a document matches any of the queries listed in should, it's considered a potential match.
The more should queries a document matches, the higher its relevancy score usually is.
By default, if there are no must clauses, at least one should clause must match for a document to be returned.
Example: `should` Clause
This query finds products that are either "laptop" OR "tablet".
{
"query": {
"bool": {
"should": [
{ "match": { "category": "laptop" } },
{ "match": { "category": "tablet" } }
]
}
}
}`must_not`: Exclude Documents
The must_not clause is like a "NOT" operator. Documents matching any query inside must_not will be excluded from the results.
Queries in must_not do not contribute to the relevancy score.
Example: `must_not` Clause
This query finds all products EXCEPT those in the "refurbished" category.
{
"query": {
"bool": {
"must_not": [
{ "match": { "category": "refurbished" } }
]
}
}
}`filter`: Fast, Non-Scoring AND
The filter clause is similar to must in that all queries within it must match. However, there's a key difference:
- No Scoring: Filter queries don't affect the relevancy score of documents.
- Caching: Filter queries are often cached, making them very fast for frequently used criteria.
Use filter for "yes/no" conditions where you just want to include or exclude documents without influencing their rank.
Advanced: Multiple Clauses
You can combine all these clauses within a single bool query to build powerful, precise searches. This example finds:
- Products with "laptop" in the name (
must) - THAT are either "gaming" OR "ultrabook" (
should) - BUT are NOT "refurbished" (
must_not) - AND have a
statusof "available" (filter)
{
"query": {
"bool": {
"must": { "match": { "name": "laptop" } },
"should": [
{ "match": { "tags": "gaming" } },
{ "match": { "tags": "ultrabook" } }
],
"must_not": { "term": { "condition": "refurbished" } },
"filter": { "term": { "status.keyword": "available" } }
}
}
}Complex Query Logic
Consider a bool query designed to find articles:
- About "Elasticsearch" (
must) - Published in "2023" OR "2024" (
should) - But NOT written by "John Doe" (
must_not)
Which of the following statements is TRUE regarding this query's behavior?
Recap: Bool Query Power
Great job! You've learned how to combine simple queries into powerful, complex search logic using Elasticsearch's bool query.
must: All conditions must be met (AND).should: At least one condition should be met (OR).must_not: Documents must NOT match these conditions (NOT).filter: Conditions must be met, but don't affect relevancy (fast & cached).
Understanding bool queries is crucial for building precise and effective search applications!
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Menggabungkan Kueri dengan Bool” gratis?
Ya — teks lengkap “Menggabungkan Kueri dengan Bool” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Elasticsearch & Full Text Search Systems, upgrade ke CoddyKit PRO. Kursus Elasticsearch & Full Text Search Systems mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Menggabungkan Kueri dengan Bool”?
Jelajahi kueri `bool` untuk menggabungkan beberapa kueri menggunakan klausa `must`, `should`, `must_not`, dan `filter` demi logika yang kompleks. Kamu berlatih Elasticsearch & Full Text Search Systems dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Elasticsearch & Full Text Search Systems?
Tidak diperlukan pengalaman sebelumnya. Elasticsearch & Full Text Search Systems di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “Menggabungkan Kueri dengan Bool” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Elasticsearch & Full Text Search Systems ini?
Ya. Setiap pelajaran Elasticsearch & Full Text Search Systems menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengantar Query DSL
- Kueri Term dan Match
- Menggabungkan Kueri dengan Bool
- Penyaringan, Rentang, dan Konteks Kueri