Bool을 활용한 쿼리 결합
`must`, `should`, `must_not`, `filter` 절을 사용하여 여러 쿼리를 복잡한 논리로 결합하는 `bool` 쿼리를 살펴봅니다.
Bool을 활용한 쿼리 결합은(는) CoddyKit의 무료 Elasticsearch & Full Text Search Systems 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Elasticsearch & Full Text Search Systems 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Elasticsearch & Full Text Search Systems 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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!
자주 묻는 질문
“Bool을 활용한 쿼리 결합” 강의는 무료인가요?
네 — “Bool을 활용한 쿼리 결합” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Elasticsearch & Full Text Search Systems 강의 전체를 잠금 해제할 수 있습니다. Elasticsearch & Full Text Search Systems 강의에는 총 4개의 강의가 포함되어 있습니다.
“Bool을 활용한 쿼리 결합”에서 뭘 배우나요?
`must`, `should`, `must_not`, `filter` 절을 사용하여 여러 쿼리를 복잡한 논리로 결합하는 `bool` 쿼리를 살펴봅니다. 브라우저에서 직접 실행하는 실습 코드로 Elasticsearch & Full Text Search Systems을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Elasticsearch & Full Text Search Systems을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Elasticsearch & Full Text Search Systems은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“Bool을 활용한 쿼리 결합” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Elasticsearch & Full Text Search Systems 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Elasticsearch & Full Text Search Systems 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Query DSL 소개
- Term 및 Match 쿼리
- Bool을 활용한 쿼리 결합
- 필터링, 범위 및 쿼리 컨텍스트