Create Or Update Index Template API
Create or Update Index Template API를 사용하면 미리 정의된 매핑과 설정으로 인덱스를 만들고, 기존 인덱스 템플릿을 업데이트할 수 있어요.
출처: OpenSearch 문서
본문
엔드포인트 (Endpoints)
PUT _index_template/{template-name}
POST _index_template/{template-name}
경로 매개변수 (Path parameters)
| Parameter | Data type | Description | | template-name | String | The name of the index template. |
쿼리 매개변수 (Query parameters)
다음 선택적 쿼리 매개변수를 지원해요.
| Parameter | Data type | Description | | create | Boolean | When true, the API cannot replace or update any existing index templates. Default is false. | | cluster_manager_timeout | Time | The amount of time to wait for a connection to the cluster manager node. Default is 30s. |
요청 본문 필드 (Request body fields)
요청 본문에서 다음 옵션을 사용해 인덱스 템플릿을 커스터마이즈할 수 있어요.
| Parameter | Type | Description | | index_patterns | String array | An array of wildcard expressions that match the names of data streams and indexes created during template creation. Required. | | composed_of | String array | An ordered list of component template names. These templates are merged using the specified order. For more information, see Using multiple component templates. Optional. | | data_stream | Object | When used, the request creates data streams and any backing indexes based on the template. This setting requires a matching index template. It can also be used with the hidden setting, which, when set to true, hides the data stream backing indexes. Optional. | | _meta | Object | Optional metadata that provides details about the index template. Optional. | | priority | Integer | A number that determines which index templates take precedence during the creation of a new index or data stream. OpenSearch chooses the template with the highest priority. When no priority is given, the template is assigned a 0, signifying the lowest priority. Optional. | | template | Object | The template that includes the aliases, mappings, or settings for the index. For more information, see [#template]. Optional. | | version | Integer | The version number used to manage index templates. Version numbers are not automatically set by OpenSearch. Optional. | | context | Object | (Experimental) The context parameter provides use-case-specific predefined templates that can be applied to an index. Among all settings and mappings declared for a template, context templates hold the highest priority. For more information, see index-context. |
템플릿 (Template)
요청 본문의 template 옵션과 함께 다음 객체들을 사용할 수 있어요.
alias
템플릿과 연결할 별칭의 이름을 키로 지정해요. 요청 본문에 template 옵션이 있다면 필수예요. 이 옵션은 여러 별칭을 지원해요.
객체 본문에는 다음 선택적 별칭 매개변수가 포함돼요.
| Parameter | Data type | Description | | filter | Query DSL object | The query that limits the number of documents that the alias can access. | | index_routing | String | The value that routes indexing operations to a specific shard. When specified, overwrites the routing value for indexing operations. | | is_hidden | Boolean | When true, the alias is hidden. Default is false. All alias indexes must have matching values for this setting. | | is_write_index | Boolean | When true, the index is the write index for the alias. Default is false. | | routing | String | The value used to route index and search operations to a specific shard. | | search_routing | String | The value used to write specific search operations to a specific shard. When specified, this option overwrites the routing value for search operations. |
mappings
인덱스에 존재하는 필드 매핑이에요. 자세한 내용은 Mappings and field types를 참고하세요. 선택 사항이에요.
settings
인덱스에 대한 구성 옵션이에요. 자세한 내용은 Index settings를 참고하세요.
예제 요청 (Example requests)
다음 예제들은 Create or Update Index Template API 사용법을 보여줘요.
인덱스 별칭이 있는 인덱스 템플릿 (Index template with index aliases)
다음 예제 요청은 템플릿에 인덱스 별칭을 포함해요.
PUT /_index_template/alias-template
{
"index_patterns": [
"sh*"
],
"template": {
"settings": {
"number_of_shards": 1
},
"aliases": {
"alias1": {},
"alias2": {
"filter": {
"term": {
"user.id": "hamlet"
}
},
"routing": "shard-1"
},
"{index}-alias": {}
}
}
}
response = client.indices.put_index_template(
name = "alias-template",
body = {
"index_patterns": [
"sh*"
],
"template": {
"settings": {
"number_of_shards": 1
},
"aliases": {
"alias1": {},
"alias2": {
"filter": {
"term": {
"user.id": "hamlet"
}
},
"routing": "shard-1"
},
"{index}-alias": {}
}
}
}
)
여러 매칭 템플릿 사용 (Using multiple matching templates)
여러 인덱스 템플릿이 새 인덱스나 데이터 스트림의 이름과 일치하면, 우선순위가 가장 높은 템플릿이 사용돼요. 예를 들어 다음 두 요청은 서로 다른 우선순위의 인덱스 템플릿을 만들어요.
첫 번째 요청은 우선순위 0의 템플릿을 만들며, h로 시작하는 인덱스 이름과 일치해요.
PUT /_index_template/template_one
{
"index_patterns" : ["h*"],
"priority" : 0,
"template": {
"settings" : {
"number_of_shards" : 1,
"number_of_replicas": 0
},
"mappings" : {
"_source" : { "enabled" : false }
}
}
}
두 번째 요청은 우선순위 1의 템플릿을 만들며, ha로 시작하는 더 좁은 인덱스 이름 집합과 일치해요.
PUT /_index_template/template_two
{
"index_patterns" : ["ha*"],
"priority" : 1,
"template": {
"settings" : {
"number_of_shards" : 2
},
"mappings" : {
"_source" : { "enabled" : true }
}
}
}
ha로 시작하는 인덱스의 경우 _source가 활성화돼요. template_two만 적용되므로 인덱스는 2개의 기본 샤드와 1개의 복제본을 갖게 돼요.
동일한 우선순위로 겹치는 인덱스 패턴은 허용되지 않아요. 기존 인덱스 템플릿과 동일한 우선순위로 일치하는 템플릿을 만들려고 하면 오류가 발생해요.
템플릿 버전 관리 추가 (Adding template versioning)
다음 예제 요청은 인덱스 템플릿에 버전 번호를 추가해요. 이는 외부 시스템의 템플릿 관리를 단순화해요.
PUT /_index_template/versioned-template
{
"index_patterns": [
"mac",
"cheese"
],
"priority": 0,
"template": {
"settings": {
"number_of_shards": 1
}
},
"version": 1
}
response = client.indices.put_index_template(
name = "versioned-template",
body = {
"index_patterns": [
"mac",
"cheese"
],
"priority": 0,
"template": {
"settings": {
"number_of_shards": 1
}
},
"version": 1
}
)
템플릿 메타데이터 추가 (Adding template metadata)
다음 예제 요청은 meta 매개변수를 사용해 인덱스 템플릿에 메타데이터를 추가해요. 모든 메타데이터는 클러스터 상태에 저장돼요.
PUT /_index_template/metadata-template
{
"index_patterns": [
"rom",
"juliet"
],
"template": {
"settings": {
"number_of_shards": 2
}
},
"_meta": {
"description": "Where art thou",
"serialization": {
"class": "MyIndexTemplate",
"id": 12
}
}
}
response = client.indices.put_index_template(
name = "metadata-template",
body = {
"index_patterns": [
"rom",
"juliet"
],
"template": {
"settings": {
"number_of_shards": 2
}
},
"_meta": {
"description": "Where art thou",
"serialization": {
"class": "MyIndexTemplate",
"id": 12
}
}
}
)
데이터 스트림 정의 (Data stream definition)
다음 예제 요청처럼 data_stream 객체를 포함하면 인덱스 템플릿을 데이터 스트림에 사용할 수 있어요.
PUT /_index_template/logs-template
{
"index_patterns": [
"logs-*"
],
"data_stream": {}
}
response = client.indices.put_index_template(
name = "logs-template",
body = {
"index_patterns": [
"logs-*"
],
"data_stream": {}
}
)
여러 컴포넌트 템플릿 사용 (Using multiple component templates)
composed_of 필드와 함께 여러 컴포넌트 템플릿을 사용하면 컴포넌트 템플릿이 지정된 순서대로 병합돼요. 그다음 컴포넌트의 부모 인덱스 템플릿의 모든 매핑, 설정, 별칭이 병합돼요. 마지막으로 인덱스 요청에 추가된 구성 옵션들이 병합돼요.
다음 예제에서 my-index-*와 일치하는 인덱스는 병합된 2개의 기본 샤드를 가져요. composed_of 배열의 순서가 뒤집히면 인덱스는 1개의 기본 샤드를 갖게 돼요.
먼저 1개의 기본 샤드를 설정하는 컴포넌트 템플릿을 만들어요.
PUT /_component_template/template_with_1_shard
{
"template": {
"settings": {
"index.number_of_shards": 1
}
}
}
다음으로 2개의 기본 샤드를 설정하는 컴포넌트 템플릿을 만들어요.
PUT /_component_template/template_with_2_shards
{
"template": {
"settings": {
"index.number_of_shards": 2
}
}
}
마지막으로 두 컴포넌트 템플릿을 순서대로 조합하는 인덱스 템플릿을 만들어요.
PUT /_index_template/composed-template
{
"index_patterns": ["my-index-*"],
"composed_of": ["template_with_1_shard", "template_with_2_shards"]
}
매핑 정의와 dynamic_templates·meta 같은 루트 옵션에는 재귀 병합이 사용돼요. 즉 이전 컴포넌트에 meta 블록이 있으면 새 meta 항목이 인덱스의 메타데이터 끝에 추가돼요. 기존 키를 포함하는 항목은 덮어써져요.
필요한 권한 (Required permissions)
Security 플러그인을 사용한다면 적절한 권한을 확인하세요: indices:admin/index_template/put