본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

인덱스 매핑 생성·갱신 API (Create or Update Index Mappings API)

원문 보기 위키 갱신

이 API를 사용하면 기존 인덱스에 새 필드를 도입하거나 기존 필드의 검색 설정을 수정할 수 있어요. 인덱스를 처음부터 다시 생성하지 않고도 스키마를 발전시킬 수 있어요.

출처: OpenSearch 문서

본문

Introduced 1.0

이 API를 사용하면 기존 인덱스에 새 필드를 도입하거나 기존 필드의 검색 설정을 수정할 수 있어요. 이 연산을 통해 인덱스를 처음부터 다시 만들지 않고도 인덱스 스키마를 발전시킬 수 있어요.

이미 인덱싱된 데이터가 있는 필드의 매핑이나 필드 타입을 이 연산으로 변경할 수는 없어요. 기존 필드의 타입을 수정하면 이전에 인덱싱된 데이터가 새 매핑과 호환되지 않을 위험이 있어요. 기존 필드의 타입을 변경해야 한다면 원하는 매핑으로 새 인덱스를 만든 다음 Reindex 연산으로 원본 인덱스에서 문서를 복사하세요. 리인덱싱 중 다운타임을 피하려면 aliases를 사용할 수 있어요. 자세한 내용은 Changing the type of an existing field를 참고하세요.

엔드포인트 (Endpoints)

POST /{index}/_mapping
PUT  /{index}/_mapping

경로 매개변수 (Path parameters)

아래 표는 사용 가능한 경로 매개변수를 정리한 거예요.

| Parameter | Required | Data type | Description | | index | Required | String | 갱신할 인덱스의 이름이에요. 단일 인덱스 이름, 쉼표로 구분된 인덱스 이름 목록, 또는 와일드카드 표현식을 지정할 수 있어요. 모든 인덱스의 매핑을 갱신하려면 _all 또는 *을 사용해요. |

쿼리 매개변수 (Query parameters)

아래 표는 사용 가능한 쿼리 매개변수를 정리한 거예요. 모든 쿼리 매개변수는 선택 사항이에요.

| Parameter | Data type | Description | Default | | allow_no_indices | Boolean | 어떤 인덱스와도 일치하지 않는 와일드카드를 무시할지 지정해요. false면 와일드카드가 어떤 인덱스와도 일치하지 않을 때 오류를 반환해요. | true | | cluster_manager_timeout | String | 클러스터 매니저 노드에 연결하기 위해 기다리는 시간이에요. | 30s | | expand_wildcards | String | 와일드카드 표현식이 확장될 수 있는 인덱스 유형을 지정해요. 쉼표로 구분된 값을 지원해요. 유효한 값은: - all: 숨은 인덱스를 포함한 모든 인덱스 매칭 - open: 열린 인덱스 매칭 - closed: 닫힌 인덱스 매칭 - hidden: 숨은 인덱스 매칭. open, closed 또는 둘 다와 결합해야 해요. - none: 와일드카드 표현식을 허용하지 않아요. | open | | ignore_unavailable | Boolean | 누락되거나 닫힌 인덱스를 무시할지 지정해요. true면 누락되거나 닫힌 인덱스가 응답에 포함되지 않아요. | false | | timeout | String | 응답을 기다리는 시간이에요. 타임아웃이 만료되기 전에 응답을 받지 못하면 요청이 실패하고 오류를 반환해요. | 30s | | write_index_only | Boolean | true면 매핑이 대상의 현재 쓰기 인덱스에만 적용돼요. | false |

요청 본문 필드 (Request body fields)

아래 표는 사용 가능한 요청 본문 필드를 정리한 거예요.

| Field | Data type | Description | | properties | Object | 필수예요. 인덱스 매핑의 필드와 해당 타입을 정의해요. 각 필드는 이름, 필드 데이터 타입, 매핑 매개변수를 포함할 수 있어요. | | dynamic | String | 새 필드를 동적으로 추가할지 제어해요. 유효한 값은 true(새 필드 자동 추가), false(새 필드 무시), strict(매핑되지 않은 필드가 포함된 요청 거부)예요. 기본값은 true예요. |

예시: 인덱스에 필드 추가 (Example: Adding fields to an index)

Create or Update Mappings API는 기존 인덱스가 필요해요. 다음 예시는 products 인덱스에 description과 price 필드를 추가해요.

REST Python

PUT /products/_mapping
{
"properties": {
"description": {
"type": "text"
},
"price": {
"type": "float"
}
}
}

Copy Copy as cURL

response = client.indices.put_mapping(
index = "products",
body =   {
"properties": {
"description": {
"type": "text"
},
"price": {
"type": "float"
}
}
}
)

Copy

Get Mappings API로 매핑이 적용되었는지 확인할 수 있어요.

GET /products/_mapping

예시: 여러 인덱스 갱신 (Example: Updating multiple indexes)

인덱스 이름을 쉼표로 구분해 지정하면 단일 요청으로 여러 인덱스에 매핑 갱신을 적용할 수 있어요. 다음 예시는 US 및 EU 지역 카탈로그에 currency와 tax_rate 필드를 추가해요.

REST Python

PUT /products-us,products-eu/_mapping
{
"properties": {
"currency": {
"type": "keyword"
},
"tax_rate": {
"type": "float"
}
}
}

Copy Copy as cURL

response = client.indices.put_mapping(
index = "products-us,products-eu",
body =   {
"properties": {
"currency": {
"type": "keyword"
},
"tax_rate": {
"type": "float"
}
}
}
)

Copy

예시: 기존 객체 필드에 속성 추가 (Example: Adding properties to an existing object field)

기존 object 필드에 새 내부 필드를 추가할 수 있어요. products 인덱스에 name 필드가 있는 manufacturer 객체가 이미 있다고 가정해 봐요. 다음 예시는 manufacturer 객체에 country 키워드 필드를 추가해요.

REST Python

PUT /products/_mapping
{
"properties": {
"manufacturer": {
"properties": {
"country": {
"type": "keyword"
}
}
}
}

Copy Copy as cURL

response = client.indices.put_mapping(
index = "products",
body =   {
"properties": {
"manufacturer": {
"properties": {
"country": {
"type": "keyword"
}
}
}
}
)

Copy

매핑을 조회해 중첩 구조를 확인할 수 있어요.

GET /products/_mapping

Response

{
"products" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text"
},
"manufacturer" : {
"properties" : {
"country" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
}
}
},
"price" : {
"type" : "float"
},
"product_name" : {
"type" : "text"
}
}
}
}
}

예시: 기존 필드에 멀티 필드 추가 (Example: Adding multi-fields to an existing field)

Multi-fields를 사용하면 같은 필드를 여러 방식으로 인덱싱할 수 있어요. 예를 들어 전체 텍스트 검색에 사용되는 text 필드에 정렬이나 집계용 keyword 하위 필드를 추가할 수 있어요. 다음 예시는 ignore_above를 256으로 설정한 product_name.keyword 하위 필드를 추가하여 제품 이름에 대한 정확히 일치 필터링과 정렬을 가능하게 해요.

REST Python

PUT /products/_mapping
{
"properties": {
"product_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}

Copy Copy as cURL

response = client.indices.put_mapping(
index = "products",
body =   {
"properties": {
"product_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
)

Copy

멀티 필드 구성을 확인할 수 있어요.

GET /products/_mapping

Response

{
"products" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text"
},
"manufacturer" : {
"properties" : {
"country" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
}
}
},
"price" : {
"type" : "float"
},
"product_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}

예시: 지원되는 매핑 매개변수 변경 (Example: Changing supported mapping parameters)

일부 매핑 매개변수는 Create or Update Mappings API로 기존 필드에 대해 갱신할 수 있어요. 예를 들어 keyword 필드의 ignore_above 값을 변경할 수 있어요. 다음 예시는 sku 필드의 ignore_above를 20에서 50으로 늘려 더 긴 제품 코드도 인덱싱될 수 있게 해요.

REST Python

PUT /products/_mapping
{
"properties": {
"sku": {
"type": "keyword",
"ignore_above": 50
}
}
}

Copy Copy as cURL

response = client.indices.put_mapping(
index = "products",
body =   {
"properties": {
"sku": {
"type": "keyword",
"ignore_above": 50
}
}
}
)

Copy

갱신된 매개변수 값을 확인할 수 있어요.

GET /products/_mapping

Response

{
"products" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text"
},
"manufacturer" : {
"properties" : {
"country" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
}
}
},
"price" : {
"type" : "float"
},
"product_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"sku" : {
"type" : "keyword",
"ignore_above" : 50
}
}
}
}

예시: 별칭으로 필드 이름 변경 (Example: Renaming a field using an alias)

필드 이름을 바꾸면 이전에 저장된 데이터가 새 이름으로는 접근할 수 없게 되므로, alias 필드 타입을 사용해 필드를 참조하는 대체 방법을 제공해요. 다음 예시는 기존 product_id 필드를 가리키는 item_id 별칭을 만들어 두 이름 모두로 쿼리할 수 있게 해요.

REST Python

PUT /products/_mapping
{
"properties": {
"item_id": {
"type": "alias",
"path": "product_id"
}
}
}

Copy Copy as cURL

response = client.indices.put_mapping(
index = "products",
body =   {
"properties": {
"item_id": {
"type": "alias",
"path": "product_id"
}
}
}
)

Copy

별칭이 생성되었는지 확인할 수 있어요.

GET /products/_mapping

Response

{
"products" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text"
},
"item_id" : {
"type" : "alias",
"path" : "product_id"
},
"manufacturer" : {
"properties" : {
"country" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
}
}
},
"price" : {
"type" : "float"
},
"product_id" : {
"type" : "keyword"
},
"product_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"sku" : {
"type" : "keyword",
"ignore_above" : 50
}
}
}
}

예시: 기존 필드의 타입 변경 (Example: Changing the type of an existing field)

이미 인덱싱된 데이터가 있는 필드의 타입을 직접 변경할 수는 없어요. 대신 올바른 매핑으로 새 인덱스를 만든 다음 Reindex API로 원본 인덱스에서 문서를 복사해야 해요.

다음 예시는 소수점 값(예: 0.75 kg)을 정확히 저장할 수 있도록 weight 필드를 integer에서 float로 변경해요.

먼저 갱신된 필드 타입으로 새 인덱스를 생성해요.

REST Python

PUT /products-v2
{
"mappings": {
"properties": {
"product_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"price": {
"type": "float"
},
"weight": {
"type": "float"
}
}
}
}

Copy Copy as cURL

response = client.indices.create(
index = "products-v2",
body =   {
"mappings": {
"properties": {
"product_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"price": {
"type": "float"
},
"weight": {
"type": "float"
}
}
}
)

Copy

그런 다음 원본 인덱스의 데이터를 새 인덱스로 리인덱싱해요.

REST Python

POST /_reindex
{
"source": {
"index": "products"
},
"dest": {
"index": "products-v2"
}
}

Copy Copy as cURL

response = client.reindex(
body =   {
"source": {
"index": "products"
},
"dest": {
"index": "products-v2"
}
}
)

Copy Response

{
"took" : 8,
"timed_out" : false,
"total" : 2,
"updated" : 0,
"created" : 2,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}

예시 응답 (Example response)

성공적인 매핑 갱신은 다음 응답을 반환해요.

{
"acknowledged": true
}

응답 본문 필드 (Response body fields)

아래 표는 모든 응답 본문 필드를 정리한 거예요.

| Field | Data type | Description | | acknowledged | Boolean | 클러스터의 모든 관련 노드가 요청을 승인했는지 여부를 나타내요. |

필요한 권한 (Required permissions)

Security 플러그인을 사용한다면 적절한 권한이 있는지 확인해 주세요: indices:admin/mapping/put.

더 알아보기 (Learn more)