reindex 와 alias

개발새발·2023년 4월 8일
0

elasticsearch

목록 보기
48/54

회사에서 기존에 많은 데이터가 담긴 인덱스의 스탑태그를 변경해줘야할 작업이 생겼다. 변경하기 위해서는 리인덱싱이 필요했는데, 이전에 리인덱싱을 해본적이 있지만 이번에는 서비스 운영을 하는 도중에 작업을 해야해서 좀 큰 작업이 되었다. 아직 실제로 돌려보기 전이고, 회사에 다른 분의 의견에 따라 alias와 reindex 를 사용하여 최대한 다운타임없이 진행을 하기로 했다. 진행하기 위해 찾아본 부분들에 대해 미리 정리를 해봤다.

과정

  1. 기존 인덱스를 alias화 한다.

    PUT /_aliases
    {
      "actions": [
        {
          "add": {
            "index": "index_one", 
            "alias": "index_one_alias"
          }
        }
      ]
    }
  1. 코드에서 사용하던 기존 인덱스명을 alias명으로 변경해준다.

  2. 매핑이 같은 인덱스 여러개를 리인덱싱해야하기 때문에 매핑을 템플릿화시켰다.

    PUT _index_template/{new_template_name}
    {
      "index_patterns": [
        "new_index_*" 
      ],
      "priority": 1,
      "template": {
        "mappings": {
          "dynamic": "false",
          "properties": {
            "a_property": {
              "type": "text"
            },
            "b_property": {
              "type": "date",
              "null_value": ""
            },
            "c_property": {
              "type": "keyword"
            },
            "d_property": {
              "type": "object"
            }
        },
        "settings": {
          "index": {
            "routing": {
              "allocation": {
                "include": {
                  "_tier_preference": "data_content"
                }
              }
            },
            "number_of_shards": "1",
            "number_of_replicas": "2",
            "analysis": {
              "filter": {
                "porter2": {
                  "name": "porter2",
                  "type": "stemmer"
                },
                "english_stop": {
                  "type": "stop",
                  "stopwords": "_english_"
                },
                "nori_stop": {
                  "type": "nori_part_of_speech",
                  "stoptags": [
                    "E",
                    "IC",
                    "J",
                    "MAG",
                    "MM",
                    "NA",
                    "NR",
                    "SC"
                  ]
                }
              },
              "analyzer": {
                "K_analyzer": {
                  "filter": [
                    "nori_stop",
                    "porter2",
                    "english_stop"
                  ],
                  "type": "custom",
                  "tokenizer": "nori_mixed"
                }
              },
              "tokenizer": {
                "nori_mixed": {
                  "type": "nori_tokenizer",
                  "decompound_mode": "mixed"
                }
              }
            }
          }
        }
      }
    }
  1. 새로운 인덱스를 생성한다.
    *위 템플릿 API의 index_patterns에 의하여 위 템플릿의 매핑되어 인덱스가 생성된다.
    PUT new_index_one
  2. 리인덱싱을 진행한다.
    *wait_for_completion=false : document양이 10만 이상이 넘어가게 되면 작업이 오래걸리기때문에 kibana에서 504 gateway timeout이 발생하고 작업이 중단된다. 그래서 해당 작업을 비동기로 실행시키는 옵션인 wait_for_completion=false를 함께 설정해주고 진행해야한다.
    POST _reindex?wait_for_completion=false
    {
      "source": {
        "index": "index_one"
      },
      "dest": {
        "index": "new_index_one"
      }
    }
  1. 기존 인덱스 alias 제거후 새로운 인덱스 alias 세팅
    *tip : is_write_index 는 같은 alias를 가진 인덱스가 있을 때, 한 인덱스에서만 사용이 가능하다. (예를 들어, alias로 index_one 에 new_index_one, index_one 인덱스 두개만 걸려있다면 둘 중에 하나에만 is_write_index 세팅이 가능하다.)
    PUT /_aliases
    {
      "actions": [
        {
          "add": {
            "index": "index_one",
            "alias": "index_one_alias",
            "is_write_index": true
          }
        },
        {
          "remove": {
            "index": "new_index_one",
            "alias": "index_one_alias"
          }
        }
      ]
    }
  2. 기존 인덱스를 삭제한다.
    DELETE /index_one

참고: https://wedul.site/611?category=680504

profile
발새발개

0개의 댓글