본문 바로가기

Elasticsearch

[Elasticsearch] index alias란? alias 설정, 추가, 수정, 삭제하기

반응형

Alias란 ? What is Alias in Elasticsearch ? 

Alias란 data stream 또는 indices에 대한 두번째 이름(별칭)이다. 대부분의 ES APIs는 data stream 또는 index 이름 대신에 alias를 받는다. Alias를 사용하는 주된 목적은 어플리케이션 코드에 정지시간(downtime) 또는 어떤 변화없이 데이터를 reindex (copy) 할 수 있다는 것이다. 

 

Elasticsearch 의 Index mapping, setting 등의 메타데이터를 수정하기가 쉽지 않아서 새로운 인덱스를 생성한 후 reindex 해주는 방식을 사용할 수 있다. 그런데 reindex가 진행되는 시간이 길어지는 경우 원래의 index를 사용할 수 없다.

-> 이는 실제 운영 중인 서비스라면 상당한 문제가 될 수도 있다. 

=> 여기에서 나온 것이 Elasticsearch의 Alias 이다. 

alias의 영어 뜻은 "가명"이다. 어떤 인덱스의 별칭(가명)을 정하고 ES에 요청이 올 때 인덱스 명이 별칭인 요청이 들어올 경우 별칭에 해당하는 인덱스로 보내주는 것이다. 

서비스 중단없이 인덱스 설정을 바꿀 때 활용한다. 

 

An alias is a secondary name for a group of data streams or indices. It is like nickname. Most Elasticsearch APIs accept an alias in place of a data stream or index name. in short, aliases are like soft links or shortcuts to actual indices. 

 

The reason why we use aliases is to be able to have an alias pointing to the origin index while building or re-indexing on other (new) index. And the moment of swapping them is atomic thanks to the alias, to which all code should point, no need to woory about a short period of time where the alias does not point to an index. 

 

 

 

 

Alias 종류, Alias Types 

1. A data stream alias 

하나 또는 그 이상의 Data streams를 가리키는 데이터 스트림 alias 

A data stream alias points to one or more data streams.

 

2. An index alias 

하나 또는 그 이상의 인덱스를 가리키는 인덱스 alias 

An index alias points to one or more indices. 

 

 

 

 

Alias 설정하기,  Add an alias

현재 존재하는 data stream 또는 Index에 alias를 추가하는 방법은 aliase API의 "add" 가 있다. 

To add an existing data stream or index to an alias, use the aliases API's add action. 

 

Kibana

POST /_aliases?pretty
{
  "actions": [
    {
      "add": {
        "index": "index_1",
        "alias": "alias_1"
      }
    }
  ]
}

 

curl

curl -X POST "https://localhost:9243/_aliases?pretty" -H "Authorization:ApiKey OOACMSKJEYOURAPIKEY" -H "Content-Type: application/json" -d'
{
  "actions": [
    {
      "add": {
        "index": "my_index",
        "alias": "my_alias_index"
      }
    }
  ]
}
'

 

 

Alias 동시에 여러개 추가하기 

index_1, index_2 두 개의 인덱스를 바라보는 alias_1이라는 alias 추가하기

POST /_aliases?pretty
{
  "actions": [
    {
      "add": {
        "index": "index_1",
        "alias": "alias_1"
      }
    },
    {
      "add": {
        "index": "index_2",
        "alias": "alias_1"
      }
    }
  ]
}

 

 

 

Alias 삭제하기 

index_2를 바라보는 alias_1 이라는 alias를 remove 삭제한다.

POST /_aliases?pretty
{
  "actions": [
    {
      "remove": {
        "index": "index_2",
        "alias": "alias_1"
      }
    }
  ]
}

 

 

 

 

 

Alias 삭제 및 추가 동시에 하기 

기존에 Index_original이라는 인덱스를 바라보던 alias라는 이름의 alias를 remove 없애고 새로운 Index_new를 바라보게 만듬 

POST /_aliases?pretty
{
  "actions": [
    {
      "remove": {
        "index": "index_original",
        "alias": "alias"
      }
    },
    {
      "add": {
        "index": "index_new",
        "alias": "alias"
      }
    }
  ]
}

 

 

 

alias 기능 중 위와 같이 생성, 추가, 삭제를 제외하고 더 자세한 (filter 사용 등) 기능은 아래 사이트에 잘 나와있닷.

 

 

 

 

Reference :https://opster.com/guides/elasticsearch/glossary/elasticsearch-alias/

https://soo-sin.tistory.com/21 https://www.elastic.co/guide/en/elasticsearch/reference/master/aliases.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-alias.html

반응형