Dynamic 매핑 파라미터
Dynamic 매핑 파라미터
dynamic 파라미터는 새로 감지된 필드를 매핑에 동적으로 추가할 수 있는지 여부를 지정해요. 허용되는 값은 true, false, false_allow_templates, strict, strict_allow_templates예요.
출처: 문서
본문
dynamic 파라미터는 새로 감지된 필드를 매핑에 동적으로 추가할 수 있는지 여부를 지정합니다. 다음 표에 나열된 파라미터를 받습니다.
| 파라미터 | 설명 |
|---|---|
true |
새 필드를 매핑에 동적으로 추가할 수 있음을 지정합니다. 기본값은 true입니다. |
false |
새 필드를 매핑에 동적으로 추가할 수 없음을 지정합니다. 새 필드가 감지되면 인덱싱되거나 검색 가능하지 않지만 _source 필드에서 검색할 수 있습니다. |
false_allow_templates |
사전 정의된 다이나믹 템플릿과 일치할 때 새 필드를 매핑에 추가합니다. 템플릿과 일치하지 않는 새 필드는 인덱싱되거나 검색 가능하지 않지만 _source 필드에는 존재합니다. |
strict |
예외를 던집니다. 새 필드가 감지되면 인덱싱 작업이 실패합니다. |
strict_allow_templates |
새 필드가 매핑의 사전 정의된 다이나믹 템플릿과 일치하면 추가합니다. |
예제: dynamic을 true로 설정해 인덱스 생성
다음 요청을 보내 dynamic을 true로 설정한 인덱스를 만듭니다.
PUT testindex1
{
"mappings": {
"dynamic": true
}
}
다음 요청을 보내 두 개의 문자열 필드를 포함하는 객체 필드 patient가 있는 문서를 인덱싱합니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
}
}
다음 요청을 보내 매핑이 예상대로 작동하는지 확인합니다.
GET testindex1/_mapping
객체 필드 patient와 두 하위 필드 name, id가 다음 응답처럼 매핑에 추가됩니다.
{
"testindex1": {
"mappings": {
"dynamic": "true",
"properties": {
"patient": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
예제: dynamic을 false로 설정해 인덱스 생성
다음 요청을 보내 명시적 매핑과 dynamic을 false로 설정한 인덱스를 만듭니다.
PUT testindex1
{
"mappings": {
"dynamic": false,
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
다음 요청을 보내 두 개의 문자열 필드를 포함하는 객체 필드 patient와 추가적인 매핑되지 않은 필드를 포함하는 문서를 인덱싱합니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}
다음 요청을 보내 매핑이 예상대로 작동하는지 확인합니다.
GET testindex1/_mapping
다음 응답은 새 필드 room과 floor가 매핑에 추가되지 않아 매핑이 변경되지 않았음을 보여 줍니다.
{
"testindex1": {
"mappings": {
"dynamic": "false",
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
}
room과 floor 필드를 검색해 보겠습니다.
POST testindex1/_search
{
"query": {
"term": {
"room": "room1"
}
}
}
응답은 결과가 없다고 반환합니다.
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
예제: dynamic을 strict로 설정해 인덱스 생성
다음 요청을 보내 명시적 매핑과 dynamic을 strict로 설정한 인덱스를 만듭니다.
PUT testindex1
{
"mappings": {
"dynamic": strict,
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
다음 요청을 보내 두 개의 문자열 필드를 포함하는 객체 필드 patient와 추가적인 매핑되지 않은 필드를 포함하는 문서를 인덱싱합니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}
다음 응답에서처럼 예외가 발생합니다.
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [room] within [_doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [room] within [_doc] is not allowed"
},
"status": 400
}
예제: dynamic을 strict_allow_templates로 설정해 인덱스 생성
다음 요청을 보내 사전 정의된 다이나믹 템플릿과 dynamic을 strict_allow_templates로 설정한 인덱스를 만듭니다.
PUT testindex1
{
"mappings": {
"dynamic": "strict_allow_templates",
"dynamic_templates": [
{
"strings": {
"match": "room*",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
다음 요청을 보내 두 개의 문자열 필드를 포함하는 객체 필드 patient와 다이나믹 템플릿 중 하나와 일치하는 새 필드 room을 포함하는 문서를 인덱싱합니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1"
}
새 필드 room이 다이나믹 템플릿과 일치하므로 인덱싱이 성공합니다. 그러나 새 필드 floor는 다이나믹 템플릿과 일치하지 않고 명시적으로 매핑되지 않았으므로 인덱싱이 실패합니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}
예제: dynamic을 false_allow_templates로 설정해 인덱스 생성
다음 요청을 보내 사전 정의된 다이나믹 템플릿과 dynamic을 false_allow_templates로 설정한 인덱스를 만듭니다.
PUT testindex1
{
"mappings": {
"dynamic": "false_allow_templates",
"dynamic_templates": [
{
"strings": {
"match": "room*",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"patient": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
}
}
}
}
}
}
다음 요청을 보내 두 개의 문자열 필드를 포함하는 객체 필드 patient와 다이나믹 템플릿 중 하나와 일치하는 새 필드 room을 포함하는 문서를 인덱싱합니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1"
}
새 필드 room이 다이나믹 템플릿과 일치하므로 인덱싱됩니다.
새 필드 floor를 추가하면 매핑 프로퍼티나 다이나믹 템플릿과 일치하지 않더라도 인덱싱됩니다. 그러나 floor 필드에 대한 매핑은 생성되지 않습니다. 따라서 {"match": {"floor": "1"}} 같은 쿼리는 이 문서를 반환하지 않습니다.
PUT testindex1/_doc/1
{
"patient": {
"name" : "John Doe",
"id" : "123456"
},
"room": "room1",
"floor": "1"
}