Exists 쿼리
Exists 쿼리
특정 필드를 포함한 문서를 검색할 때 exists 쿼리를 사용해요. 이 글에서는 필드가 언제 인덱싱된 값을 가지는지/가지지 않는지와 사용 예제를 살펴봐요.
출처: 문서
본문
특정 필드를 포함한 문서를 검색하려면 exists 쿼리를 사용해요. 다음의 경우에는 문서 필드에 인덱싱된 값이 존재하지 않아요:
- 매핑에 "index": false로 지정된 필드인 경우
- source JSON의 필드가 null이거나 []인 경우
- 필드 값의 길이가 매핑의 ignore_above 설정을 초과하는 경우
- 필드 값이 잘못된 형태(또는 형식)이고 매핑에 ignore_malformed가 정의된 경우 다음의 경우에는 문서 필드에 인덱싱된 값이 존재해요:
- 값이 null 요소와 non-null 요소를 하나 이상씩 포함하는 배열인 경우(예: ["one", null])
- 값이 빈 문자열("" 또는 "-")인 경우
- 값이 필드 매핑에 정의된 사용자 지정 null_value인 경우
예제 (Example)
예를 들어 다음 두 문서를 포함하는 인덱스를 살펴봐요:
PUT testindex/_doc/1
{
"title": "The wind rises"
}
PUT testindex/_doc/2
{
"title": "Gone with the wind",
"description": "A 1939 American epic historical film"
}
다음 쿼리는 description 필드를 포함한 문서를 검색해요:
GET testindex/_search
{
"query": {
"exists": {
"field": "description"
}
}
}
응답에는 일치하는 문서가 포함돼요:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "testindex",
"_id": "2",
"_score": 1,
"_source": {
"title": "Gone with the wind",
"description": "A 1939 American epic historical film"
}
}
]
}
}
인덱싱된 값이 없는 문서 찾기
인덱싱된 값이 없는 문서를 찾으려면 내부 exists 쿼리와 함께 must_not 불리언 쿼리를 사용할 수 있어요. 예를 들어 다음 요청은 description 필드가 없는 문서를 검색해요:
GET testindex/_search
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "description"
}
}
}
}
}
응답에는 일치하는 문서가 포함돼요:
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 0,
"_source": {
"title": "The wind rises"
}
}
]
}
}
파라미터 (Parameters)
쿼리는 필드 이름(boost | 부동 소수점(Floating-point) | 이 필드가 관련성 점수에 기여하는 가중치를 지정하는 부동 소수점 값이에요. 1.0보다 큰 값은 필드의 관련성을 높이고, 0.0과 1.0 사이의 값은 관련성을 낮춰요. 기본값은 1.0이에요.