Create Index API
Create Index API는 OpenSearch 클러스터에 새로운 인덱스를 생성하는 API예요. 이 기능은 OpenSearch 1.0에서 처음 소개되었어요.
Create Index API를 사용하면 다음과 같은 인덱스 구성을 지정할 수 있어요:
- 샤드 수, 레플리카 수처럼 인덱스 동작을 제어하는 인덱스 설정
- 인덱스에 저장되는 문서의 각 필드에 대한 데이터 타입과 속성을 정의하는 필드 매핑(field mappings)
- 인덱스를 조회할 때 쓸 수 있는 대체 이름을 제공하는 인덱스 별칭(index aliases)
출처: OpenSearch 문서
본문
엔드포인트
PUT /{index}
인덱스 이름 제한 사항
OpenSearch 인덱스에는 다음과 같은 이름 제한 사항이 있어요:
- 모든 글자는 소문자여야 해요.
- 인덱스 이름은 밑줄(_)이나 하이픈(-)으로 시작할 수 없어요.
- 인덱스 이름에는 공백, 쉼표, 또는 다음 문자를 포함할 수 없어요:
:, ", *, +, /, , |, ?, #, >, or <
경로 매개변수
다음 표는 사용 가능한 경로 매개변수 목록이에요.
| Parameter | Required | Data type | Description | | index | Required | String | The name of the index you wish to create. |
쿼리 매개변수
다음 표는 사용 가능한 쿼리 매개변수 목록이에요. 모든 쿼리 매개변수는 선택 사항이에요.
| Parameter | Data type | Description | | cluster_manager_timeout | String | The amount of time to wait for a connection to the cluster manager node. | | timeout | String | The amount of time to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. | | wait_for_active_shards | Integer or String or NULL or String | The number of shard copies that must be active before proceeding with the operation. Set to all or any positive integer up to the total number of shards in the index (number_of_replicas+1). Valid values are: - all: Wait for all shards to be active. |
요청 본문 필드
새 인덱스를 구성하려면 다음과 같은 요청 본문 필드를 포함할 수 있어요. 모든 요청 본문 필드는 선택 사항이에요.
| Field | Data type | Description | | settings | Object | Index-level settings for the index. For a list of index settings, see Index settings. Optional. | | settings.index.number_of_shards | Integer | The number of primary shards in the index. Default is 1. Optional. | | settings.index.number_of_replicas | Integer | The number of replica shards for each primary shard. Default is 1. Optional. | | settings.number_of_shards | Integer | Simplified syntax for specifying the number of primary shards without the index prefix. Optional. | | settings.number_of_replicas | Integer | Simplified syntax for specifying the number of replica shards without the index prefix. Optional. | | mappings | Object | Field mappings for documents in the index. Defines the data type and properties for each field. For more information, see Mappings. Optional. | | mappings.properties | Object | Defines the fields and their data types in documents. Each key is a field name, and each value is a field definition object. Optional. | | aliases | Object | Index aliases for the index. Each key is an alias name, and each value is an alias definition object. For more information, see Index aliases. Optional. |
참고: settings 섹션 안에 index 섹션을 반드시 명시적으로 적을 필요는 없어요. 간단한 문법(simplified syntax)을 사용할 수 있어요.
예제: 기본 인덱스 생성
보통 비어 있는 인덱스를 명시적으로 만들 필요는 없어요. 존재하지 않는 인덱스에 문서를 색인하면 OpenSearch가 자동으로 인덱스를 생성하기 때문이에요. 하지만 데이터를 넣기 전에 특정 설정, 매핑, 별칭을 구성하고 싶다면 명시적으로 인덱스를 만드는 것이 유용해요.
다음 예제 요청은 설정·매핑·별칭 없이 sample-index라는 인덱스를 생성해요:
PUT /sample-index
{}
response = client.indices.create(
index = "sample-index",
body = {}
)
예제: 설정이 있는 인덱스 생성
다음 예제 요청은 샤드 수와 레플리카 수에 대한 특정 설정을 가진 books라는 인덱스를 생성해요:
PUT /books
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
}
response = client.indices.create(
index = "books",
body = {
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
}
)
예제: 간단한 설정으로 인덱스 생성
다음 예제 요청은 index 접두사 없이 간단한 설정 문법(simplified settings syntax)을 사용해 books-simplified라는 인덱스를 생성해요:
PUT /books-simplified
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
response = client.indices.create(
index = "books-simplified",
body = {
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
)
예제: 매핑이 있는 인덱스 생성
다음 예제 요청은 필드 매핑을 가진 employees라는 인덱스를 생성해요:
PUT /employees
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"department": {
"type": "keyword"
}
}
}
}
response = client.indices.create(
index = "employees",
body = {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"department": {
"type": "keyword"
}
}
}
}
)
예제: 별칭이 있는 인덱스 생성
다음 예제 요청은 두 개의 별칭(하나는 필터가 적용된 별칭)을 가진 orders라는 인덱스를 생성해요:
PUT /orders
{
"aliases": {
"current-orders": {},
"recent-orders": {
"filter": {
"range": {
"timestamp": {
"gte": "now-7d"
}
}
}
}
}
}
response = client.indices.create(
index = "orders",
body = {
"aliases": {
"current-orders": {},
"recent-orders": {
"filter": {
"range": {
"timestamp": {
"gte": "now-7d"
}
}
}
}
}
}
)
예제 응답
인덱스 생성 요청이 성공하면 OpenSearch는 다음과 같은 응답을 반환해요:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "books"
}
응답 본문 필드
다음 표는 모든 응답 본문 필드를 보여줘요.
| Field | Data type | Description | | acknowledged | Boolean | Indicates whether the index was successfully created in the cluster. A value of true means the cluster state was successfully updated with the new index. A value of false means the request timed out before the cluster state was updated, but the index will likely be created. | | shards_acknowledged | Boolean | Indicates whether the number of shard copies specified by the wait_for_active_shards setting became active before the operation timed out. A value of true means the target number of shard copies became active. A value of false means that the operation timed out before the target number of shard copies became active, regardless of whether the cluster state was successfully updated (that is, acknowledged is true). | | index | String | The name of the newly created index. |
활성 샤드 대기(Wait for active shards)
기본적으로 인덱스 생성 작업은 각 샤드의 기본(primary) 복사본이 시작되거나 요청이 타임아웃될 때까지 클라이언트에 응답을 반환하지 않아요. 응답 필드를 사용해 작업 결과를 이해할 수 있어요.
acknowledged 필드는 인덱스가 클러스터 상태에 성공적으로 생성되었는지를 나타내요. shards_acknowledged 필드는 타임아웃 전에 목표한 샤드 복사본 수가 활성화되었는지를 나타내요. 작업이 타임아웃되면 두 필드 모두 false일 수 있지만, 인덱스 생성 자체는 성공할 수도 있어요.
acknowledged가 false라면 클러스터 상태 업데이트가 타임아웃된 것이지만 인덱스는 곧 생성될 가능성이 높아요. shards_acknowledged가 false라면 클러스터 상태가 성공적으로 업데이트되었는지와 관계없이, 타임아웃 전에 목표한 샤드 복사본 수가 활성화되지 않았다는 뜻이에요.
기본적으로 기본(primary) 샤드만 시작되기를 기다리는 동작을 다음 방법 중 하나로 바꿀 수 있어요:
- 인덱스를 생성할 때 index.write.wait_for_active_shards 인덱스 설정을 지정하기. 이 설정은 이후 쓰기 작업의 wait_for_active_shards 동작에도 영향을 줘요.
- 인덱스 생성 요청에서 wait_for_active_shards 쿼리 매개변수 사용하기.
예제: index.write.wait_for_active_shards 설정 사용
다음 예제 요청은 index.write.wait_for_active_shards 설정으로 인덱스를 생성해요. 이 설정은 응답을 반환하기 전에 두 개의 샤드 복사본이 활성화되기를 기다려요. 이 설정은 인덱스에 대한 이후 쓰기 작업에도 영향을 줘요:
PUT /inventory
{
"settings": {
"index.write.wait_for_active_shards": "2",
"number_of_shards": 1,
"number_of_replicas": 1
}
}
response = client.indices.create(
index = "inventory",
body = {
"settings": {
"index.write.wait_for_active_shards": "2",
"number_of_shards": 1,
"number_of_replicas": 1
}
}
)
예제: wait_for_active_shards 쿼리 매개변수 사용
다음 예제 요청은 인덱스를 생성하면서 wait_for_active_shards 쿼리 매개변수로 응답을 반환하기 전에 두 개의 샤드 복사본(기본 1개와 레플리카 1개)이 활성화되기를 기다려요:
PUT /catalog?wait_for_active_shards=2
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
response = client.indices.create(
index = "catalog",
params = { "wait_for_active_shards": "2" },
body = {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
)
필요한 권한
Security 플러그인을 사용한다면 적절한 권한이 있는지 확인해 주세요: indices:admin/create.
더 알아보기 (Learn more)
인덱스 설정과 매핑을 더 깊이 이해하고 싶다면 원문 OpenSearch 문서를 확인해 주세요.