Database/Elasticsearch

# Elasticsearch(docker) + Analysis Plugin(Nori) 한글 형태소 적용

skysoo1111 2019. 10. 24. 14:10

예전에는 elasticsearch에서 한글 형태소 분석을 하려면 은전한닢을 사용했었는데, 이제는 elasticsearch 6.4에서 plug-in 으로 지원을 하기 시작했다. 바로 analysis-nori 이다.

 

1998년에 한국 정부가 시작한 21세기 세종 프로젝트를 통해 대규모 한국어 말뭉치가 만들어졌다. mecab-ko-dic도 그 중 하나로, 한국어 형태론의 확률적 모델을 학습시킬 때, 유명한 오픈 소스 형태소 분석 엔진인 MeCab(“메카부”)를 사용한다.

 

analysis-nori도 원래의 사전(mecab-ko-dic)을 이용하는 것이고 차이점은 같은 데이터를 담고 있지만, 보다 작고, 검색에 최적화된 모듈을 제공하는 것이다.

 

그럼 이제부터 elasticsearch에 한글 형태소 분석기를 적용해 보겠다.

 

필자는 docker에 elasticsearch 컨테이너를 띄워서 사용하고 있으며, 기본적인 elasticsearch의 경로는 같을 것이다.

 

Step 1. elasticsearch 컨테이너 접속

# es container 확인
$ docker ps 

# es컨테이너 접속
$ docker exec -it <container_id> /bin/bash

 

Step 2. analysis-nori plug-in 설치

$ cd /usr/share/elasticsearch/bin/
$ ./elasticsearch-plugin install analysis-nori

 

Step 3. user_dictionary 파일 생성

$ cd /usr/share/elasticsearch/config
$ mkdir userdict_ko.txt

 

Step 4. Index analyzer 생성

PUT book
{
  "settings": {
    "index": {
      "number_of_shards": "3",
      "number_of_replicas": "0",
      "analysis": {
        "tokenizer": {
          "nori_user_dict": {
            "type": "nori_tokenizer",
            "decompound_mode": "mixed",
            "user_dictionary": "userdict_ko.txt"
          }
        },
        "analyzer": {
          "my_analyzer": {
            "type": "custom",
            "tokenizer": "nori_user_dict"
          }
        }
      }
    }
  }
}

 

Step 5. Index analyzer 적용 확인

Step 6. 한글 형태 분석소 적용 확인

{
  "tokens": [
    {
      "token": "뿌리",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "가",
      "start_offset": 2,
      "end_offset": 3,
      "type": "word",
      "position": 1
    },
    {
      "token": "깊",
      "start_offset": 4,
      "end_offset": 5,
      "type": "word",
      "position": 2
    },
    {
      "token": "은",
      "start_offset": 5,
      "end_offset": 6,
      "type": "word",
      "position": 3
    },
    {
      "token": "나무",
      "start_offset": 7,
      "end_offset": 9,
      "type": "word",
      "position": 4
    },
    {
      "token": "는",
      "start_offset": 9,
      "end_offset": 10,
      "type": "word",
      "position": 5
    }
  ]
}

=> 가 / 은 / 는 등의 조사까지 분리해서 저장하는 것을 알 수 있다.

 

 

 

 

<참조 사이트>

https://www.elastic.co/guide/en/elasticsearch/plugins/current/analysis-nori-tokenizer.html

https://www.elastic.co/kr/blog/nori-the-official-elasticsearch-plugin-for-korean-language-analysis