[ELASTICSEARCH] index api

2025. 8. 19. 12:05Big Data

특정 index의 replica 개수 변경하기

 

$> curl -X PUT "localhost:9200/test/_settings?pretty" -H 'Content-Type: application/json' -d'
{
    "index.number_of_replicas": 3
}
'

 

url의 첫 번째 path에 index 명(여기서는 test)을 넣어 준다. 만약 모든 index를 대상으로 설정하고 싶은 경우에는 _all 지시자를 넣어주자.

 

$> curl -X PUT "localhost:9200/_all/_settings?pretty" -H 'Content-Type: application/json' -d'
{
    "index.number_of_replicas": 3
}
'

 

index 색인 갱신 주기 설정

 

$> curl -X PUT "localhost:9200/test/_settings?pretty" -H 'Content-Type: application/json' -d'
{
    "index.refresh_interval": "30s"
}
'

 

색인 갱신 주기를 너무 빠르게 잡을 경우엔 검색 성능 상의 이슈가 발생할 수 있다. 적절한 값을 찾도록 하자.

 

index 열기/닫기

 

index open을 하게 되면 해당 index의 사용이 가능한 반면, close를 하면 해당 index는 접근이 불가능한 상태가 된다.

$> curl -X POST "localhost:9200/test/_close?pretty" -H 'Content-Type: application/json'
$> curl -X POST "localhost:9200/test/_open?pretty" -H 'Content-Type: application/json'

 

index 별칭 설정하기

 

index에 별칭을 설정함으로써 별칭으로 index에 접근할 수 있게 된다.

 

$> curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
{
    "actions": [
        { "add" : { "index" : "test", "alias" : "test_alias" } }
    ]
}
'

 

rollover api

 

로그를 쌓는 index를 만들었다고 가정해 보자. 해당 로그가 너무 많이 쌓여서 index를 분리하고자 할 때 rollover api를 사용한다.

log-0001이라는 index가 있으며 해당 index 데이터가 너무 커졌을 때, rollover api 를 통해 log-0002라는 index를 새로 생성하고 새롭게 생성된 log-0002 index에 log-0001에서 사용되던 alias를 log-0002로 변경하는 작업을 진행한다.

이렇게 rollover를 하는 경우 alias 설정이 반드시 필요하다. rollover 시점에 변경된 index로 alias가 설정되어야 하기 때문이다.

 

$> curl -X PUT "localhost:9200/log-0001?pretty" -H 'Content-Type: application/json' -d'
{
    "aliases" : { "test_alias": {} }  
}
'
$> curl -X POST "localhost:9200/test_alias/_rollover/log-0002?pretty" -H 'Content-Type: application/json' -d'
{
    "conditions" : {
        "max_age": "7d",
        "max_docs": 2,
        "max_size": "5gb"
    }  
}
'

 

위의 명령어를 보면 rollover 조건이 index 생성이 7일이 지났거나 문서 수가 2개 이상이거나 index size가 5gb 이상일 경우 rollover를 실행하도록 명시되어져 있다.

 

refresh api

 

refresh api는 메모리 버퍼에 있는 문서들을 segment file로 저장해 주는 api 이다. 해당 api를 통해 문서가 검색 가능한 상태가 되도록 할 수 있다.

 

$> curl -X POST "localhost:9200/test/_refresh?pretty" -H 'Content-Type: application/json'

 

forcemerge api

 

segment의 경우 수정이나 삭제 작업은 없고 새롭게 생성 되는 작업만 존재한다. 예를 들어 어떤 문서에 대해 업데이트를 진행하게 되면 기존에 저장이 되어 있던 segment의 문서를 불용으로 마킹하고 업데이트된 문서는 새롭게 생성한다. 이렇게 진행이 될 경우 segment 내에는 사용하지 않는 문서의 개수가 많아지게 되면 이는 성능 이슈가 발생할 우려가 있다. 이를 방지하기 위해 es에서는 주기적으로 마킹된 문서를 제거하고 마킹 없는 문서들을 다시 병합하는 segment merge 작업을 실행한다.

 

forcemerge api는 강제로 위의 작업을 실행하는 api이다. 

 

$> curl -X POST "localhost:9200/test/_forcemerge?max_num_segments=10?pretty" -H 'Content-Type: application/json'

 

위의 명령에서는 기존에 있던 segment를 10개로 병합하도록 호출하고 있다.

 

reindex api

 

index migration 등이 필요한 경우에 사용이 된다. reindex api를 통해 기존 index에서 migration 된 새로운 index를 생성할 수 있다.

 

$> curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
    "source" : {
        "index": "test"
    },
    "dest": {
        "index": "new_test"
    }
}
'

 

reindex는 서로 다른 클러스터 간에도 가능하다. 서로 다른 클러스터 사이의 reindex를 진행하기 위해서는 우선 복사하고자 하는 클러스터 쪽 elasticsearch.yml 에 아래와 같이 추가한다.

 

...
reindex.remote.whitelist: ":test1.com:9200"
...

 

설정이 끝난 이후에는 아래의 명령어를 통해 test1.com에 있는 index를 reindexing 할 수 있다.

 

$> curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
    "source" : {
        "remote" : {
            "host": "http://test1.com:9200"
        },
        "index": "test"
    },
    "dest": {
        "index": "new_test"
    }
}
'

 

template api

 

template을 통해 index 설정을 적용할 수 있다. 우선 아래와 같이 template를 생성하자.

 

$> curl -X PUT "localhost:9200/_template/testtemplate?pretty" -H 'Content-Type: application/json' -d'
{
    "index_patterns" : ["test*"],
    "order": 1,
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 1
    },
    "mappings": {
        "_doc": {
            "properties": {
                "name": {
                    "type": "text"
                }
            }
        }
    },
    "aliases" : {
        "test_alias" : {}
    }
}
'

 

index_patterns : index명이 test로 시작하는 index에 대해 해당 template이 적용되도록 설정이 되어 있다. 

order : 동일한 index가 적용된 template일 경우 order 값에 따라 우선 순위가 결정된다. 값이 클수록 우선 순위가 높다.

settings : primary shard 3개, replica shard 1개로 설정되어 있다.

mappings : 문서의 필드 정보가 매핑되어 있다.

aliases : test_alias라고 별칭이 설정되었다.

 

이 후 test라는 이름의 index를 생성한 후 확인해보면 위의 template이 적용 된 것을 확인할 수가 있다.

 

$> curl -X PUT "localhost:9200/test?pretty" -H 'Content-Type: application/json'
$> curl -X GET "localhost:9200/test?pretty"

 

template의 설정 정보는 아래의 명령을 통해 확인할 수 있다.

 

$> curl -X GET "localhost:9200/_template/testtemplate?pretty"

 

또한 아래의 명령을 통해 모든 template의 목록을 확인할 수 있다.

 

$> curl -X GET "localhost:9200/_cat/templates?v"

 

출처 : https://search.daum.net/search?w=bookpage&bookId=5554436&tab=introduction&DA=LB2&q=%EA%B8%B0%EC%B4%88%EB%B6%80%ED%84%B0%20%EB%8B%A4%EC%A7%80%EB%8A%94%20elasticsearch%20%EC%9A%B4%EC%98%81%20%EB%85%B8%ED%95%98%EC%9A%B0