본문 바로가기
개발/ETC

Elastic Search - 동의어 사전 작업(URI 업데이트)

by 설이주인 2024. 1. 13.

elastic search는 동의어 사전을 개발자가 정의 할 수 있게 해준다. 이는 재기동 없이 명령어로 업데이트가 가능하다.

다만 이는 updateable : true의 상황에서 가능하다... template을 새로 한다는것은 결국 새로운 역인덱싱이다...

 

우선 몇가지를 확인해보자.

1. 현재 읽고 있는 shard의 setting

GET /[shard_name]/_settings

 

//shard_setting
{
  "[shard_name]": {
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "1",
        "provided_name": "[shard_name]",
        "creation_date": "1678339326457",
        "analysis": {
          "filter": {
            "synonym": {
              "type": "synonym",
              "synonyms_path": "analysis/synonym.txt"
            },
            "custom_stoptags": {
              "type": "nori_part_of_speech",
              "stoptags": [
                "E",
                "IC",
                "J",
                "MAG",
                "MAJ",
                "MM",
                "SP",
                "SSC",
                "SSO",
                "SC",
                "SE",
                "XPN",
                "XSA",
                "XSN",
                "XSV",
                "UNA",
                "NA",
                "VSV",
                "VA",
                "VV",
                "VX"
              ]
            }
          },
          "analyzer": {
            "default": {
              "filter": [
                "custom_stoptags",
                "lowercase",
                "synonym"
              ],
              "type": "custom",
              "tokenizer": "custom_tokenizer"
            }
          },
          "tokenizer": {
            "custom_tokenizer": {
              "user_dictionary": "analysis/user_dict.txt",
              "decompound_mode": "mixed",
              "type": "nori_tokenizer",
              "discard_punctuation": "false"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "szE7n7Q1RQ--4wP_wpRB-Q",
        "version": {
          "created": "8030399"
        }
      }
    }
  }
}

 

위의 예시에서 확인 가능하듯이 "synonym" 동의어 부분은  updateable 속성이 누락된 상태이다... 이러면 재기동 없는 업데이트는 불가능하다.

 

하지만 작업은 진행해야하는 법,

 

1. synonym - updateable 속성이 true인 template을 새로 만들자.

PUT _index_template/[template_name]
{
    "index_patterns": [
        "[shard-suffix]_*"
    ],
    "priority": 1,
    "template": {
        "settings": {
            "analysis": {
                "tokenizer": {
                    "custom_tokenizer": {
                        "type": "nori_tokenizer",
                        "decompound_mode": "mixed",
                        "discard_punctuation": "false",
                        "user_dictionary": "analysis/user_dict.txt"
                    }
                },
                "filter": {
                    "custom_stoptags": {
                        "type": "nori_part_of_speech",
                        "stoptags": [
                            "E",
                            "IC",
                            "J",
                            "MAG",
                            "MAJ",
                            "MM",
                            "SP",
                            "SSC",
                            "SSO",
                            "SC",
                            "SE",
                            "XPN",
                            "XSA",
                            "XSN",
                            "XSV",
                            "UNA",
                            "NA",
                            "VSV",
                            "VA",
                            "VV",
                            "VX"
                        ]
                    },
                    "my_synonyms": {
                        "type": "synonym",
                        "synonyms_path": "analysis/synonym.txt",
                        "updateable": true
                    }
                },
                "analyzer": {
                    "my_analyzer": {
                        "type": "custom",
                        "tokenizer": "custom_tokenizer",
                        "filter": [
                            "custom_stoptags",
                            "lowercase"
                        ]
                    },
                    "my_synonyms" : {
                      "type": "custom",
                      "tokenizer": "standard",
                      "filter": [ "my_synonyms" ]
                    }
                }
            }
        },
        "mappings": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "my_analyzer",
                    "search_analyzer": "my_synonyms"
                }
            }
        }
    }
}

 

이후 기존에 적용했었던 logstash를 진행

cd /usr/share/logstash/bin
./logstash -f /etc/logstash/conf.d/[conf_name].conf

 

최종으로 아래의 명령어를 실행하면 된다.

 

//동의어 사전 업데이트
POST /[shard_name]/_reload_search_analyzers

 

 

가끔 동의어 사전의 위치를 찾지 못하는 상황이 많이 발생하는데 필자의 경우에는 설치 경로 (/etc/elasticsearch/[synonyms_path])에서 확인 가능하다.

 

 

'개발 > ETC' 카테고리의 다른 글

CSP - Content Security Policy를 통한 XSS 방어  (0) 2024.02.20
내가 찾아볼려고 찾은 정규식 사이트  (0) 2024.01.28