Bool Query (복합 쿼리) 개요
- 여러 개의 검색 조건을 조합하여 복합적인 검색을 수행할 수 있는 강력한 쿼리 방식입니다.
- SQL의 AND, OR, NOT과 유사하지만 완전히 동일하지 않음 → 단항 연산자로 적용됨.
Bool Query 구성 요소
키워드 |
설명 |
must |
쿼리가 **참(True)**인 문서를 검색 (AND 조건) |
must_not |
쿼리가 **거짓(False)**인 문서를 검색 (NOT 조건) |
should |
일치하는 문서의 점수(Score)를 증가 (OR 조건과 유사) |
filter |
쿼리가 **참(True)**인 문서를 검색하되, 점수를 계산하지 않음 (빠름 & 캐싱 가능) |
- SQL과 비교
- must ≈ AND
- must_not ≈ NOT
- should ≈ OR (점수 영향)
- filter ≈ WHERE (점수 없음, 성능 최적화)
Bool Query 기본 문법
GET <인덱스명>/_search
{
"query": {
"bool": {
"must": [
{ <쿼리> }
],
"must_not": [
{ <쿼리> }
],
"should": [
{ <쿼리> }
],
"filter": [
{ <쿼리> }
]
}
}
}
- 각 키워드(must, must_not, should, filter) 안에는 여러 개의 조건을 배열 형식으로 입력 가능.
Bool Query 사용법
✅ 예제 1: "quick" 포함 + "lazy dog" 문장 포함 (AND 조건)
GET my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "message": "quick" } },
{ "match_phrase": { "message": "lazy dog" } }
]
}
}
}
- "quick" 단어가 포함된 문서 + "lazy dog" 문장이 포함된 문서 검색
- 결과: "quick"과 "lazy dog"을 포함하는 문서 검색됨
✅ 예제 2: "quick" 또는 "lazy dog" (OR 조건 유사)
GET my_index/_search
{
"query": {
"bool": {
"should": [
{ "match": { "message": "quick" } },
{ "match_phrase": { "message": "lazy dog" } }
],
"minimum_should_match": 1
}
}
}
- should 사용 → "quick" 또는 "lazy dog" 중 하나라도 포함하면 검색됨
- minimum_should_match: 1 → 최소 하나의 조건을 만족하는 문서 포함
- 결과: "quick" 또는 "lazy dog" 중 하나라도 포함된 문서 검색됨
✅ 예제 3: "quick" & "lazy dog" 제외 (NOT 조건)
GET my_index/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "message": "quick" } },
{ "match_phrase": { "message": "lazy dog" } }
]
}
}
}
- "quick"과 "lazy dog"이 포함되지 않은 문서만 검색
- 결과: "quick"과 "lazy dog"이 없는 문서 반환됨
✅ 예제 4: 특정 단어 포함 (must) + 특정 단어 제외 (must_not)
GET my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "message": "fox" } }
],
"must_not": [
{ "match": { "message": "lazy" } }
]
}
}
}
- "fox" 포함 & "lazy" 제외
- 결과: "fox"가 있는 문서 중 "lazy"가 없는 문서 검색됨
✅ 예제 5: 특정 단어 포함 (filter) - 점수 계산 없음
GET my_index/_search
{
"query": {
"bool": {
"filter": [
{ "match": { "message": "fox" } }
]
}
}
}
- "fox"가 포함된 문서를 검색하지만, 점수(Score)를 계산하지 않음 → 빠름
- must와 비교하면 성능이 더 우수 (검색 결과가 많을수록 차이 커짐)
Bool Query와 SQL 비교
SQL 조건 |
Bool Query 변환 |
A AND B |
must: [A, B] |
A OR B |
should: [A, B], "minimum_should_match": 1 |
NOT A |
must_not: [A] |
(A OR B) AND (NOT C) |
must: [should: [A, B]], must_not: [C] |
✅ 예제 6: SQL 스타일 변환
- SQL 스타일
(A OR B) AND (NOT C)
- Bool Query 변환
GET my_index/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "message": "A" } },
{ "match": { "message": "B" } }
],
"minimum_should_match": 1
}
}
],
"must_not": [
{ "match": { "message": "C" } }
]
}
}
}
Bool Query 정리
키워드 |
설명 |
예제 |
must |
AND 조건 |
"must": [{ "match": { "message": "quick" } }] |
must_not |
NOT 조건 |
"must_not": [{ "match": { "message": "lazy" } }] |
should |
OR 조건 |
"should": [{ "match": { "message": "quick" } }, { "match": { "message": "dog" } }], "minimum_should_match": 1 |
filter |
점수 계산 없이 검색 |
"filter": [{ "match": { "message": "fox" } }] |
🎯 추가 학습 추천
- Boosting (검색 결과 가중치 조절)
- Nested Query (중첩된 JSON 문서 검색)
- Function Score Query (점수 조작)
- Aggregations (데이터 그룹화 및 통계 분석)