ElasticSearch 6.x 한글 형태소분석기(seunjeon) 설치

By | 2020년 6월 19일
Table of Contents

ElasticSearch 6.x 한글 형태소분석기(seunjeon) 설치

ES 최신버전은 설치가 안되고 ES 6.1.4 로 설치해야 합니다.

아래 명령으로 6.4.3까지는 작동하는 것을 확인했습니다.

sudo apt install elasticsearch=6.4.3
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/javacafe-project/elastic-book-etc/raw/master/plugin/elasticsearch-analysis-seunjeon-6.4.3.zip

왜 seunjeon 인가

처음에 ES 공식지원 형태소분석기인 Nori 를 사용할까 했습니다.

그런데, 실제 서비스로 운영하기 위해서는 AWS Elasticsearch Service 를 이용해야 할건데, AWS ES 에서는 seunjeon 을 지원하고 있습니다.

그래서 seunjeon 으로 변경하게 되었습니다.

JDK 설치

JAVA 8 이상의 버전을 설치합니다. 설치되어 있지 않으면 여기 를 참조해서 설치합니다.

gpg key 추가

# gpg key 추가
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

# ElasticSearch 6.x 추가
echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list

# ElasticSearch 7.x 추가
# echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

ElasticSearch 설치

sudo apt update
sudo apt install elasticsearch=6.1.4
sudo systemctl start elasticsearch

아래 명령으로 정상작동을 확인할 수 있습니다.

curl -XGET 'http://localhost:9200'
{
  "name" : "R4UHYUL",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "56hmE2BkRnuG-4h401Zjow",
  "version" : {
    "number" : "6.1.4",
    "build_hash" : "d838f2d",
    "build_date" : "2018-03-14T08:28:22.470Z",
    "build_snapshot" : false,
    "lucene_version" : "7.1.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

설정파일과 로그파일의 위치는 아래와 같습니다.

sudo ls -al /etc/elasticsearch/
sudo ls -al /var/log/elasticsearch/

seunjeon 플러그인 설치

seunjeon 플러그인을 설치합니다.

sudo apt install zip unzip
bash <(curl -s https://bitbucket.org/eunjeon/seunjeon/raw/master/elasticsearch/scripts/downloader.sh) -e 6.1.4 -p 6.1.1.1
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install file://`pwd`/elasticsearch-analysis-seunjeon-6.1.1.1.zip
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list

# 재시작을 해야 인식합니다.
sudo systemctl restart elasticsearch
curl -XGET 'http://localhost:9200'

인덱스를 생성하고, 형태소분석기로 seunjeon 을 지정합니다.

curl -XPUT http://localhost:9200/korean_analyzer?pretty -H 'Content-Type: application/json' -d '{
  "settings" : {
    "index":{
      "analysis":{
        "analyzer":{
          "korean":{
            "type":"custom",
            "tokenizer":"seunjeon_default_tokenizer"
          }
        },
          "tokenizer": {
            "seunjeon_default_tokenizer": {
              "index_eojeol": "true",
              "index_poses": [
                "UNK", "EP", "I", "J", "M",
                "N", "SL", "SH", "SN", "VCP",
                "XP", "XS", "XR"
              ],
              "decompound": "true",
              "type": "seunjeon_tokenizer"
            }
          }
      }
    }
  },
  "mappings": {
    "_doc" : {
      "properties" : {
        "text" : {
          "type" : "text",
          "analyzer": "korean"
        }
      }
    }
  }
}'

정상적으로 작동하는 것을 확인할 수 있습니다.

curl -XPOST http://localhost:9200/korean_analyzer/_analyze?pretty -H 'Content-Type: application/json' -d '{
    "analyzer":"korean",
    "text":"아버지가방에들어가신다."
}'

사용자 사전 추가

사용자 사전을 추가합니다.

"user_dict_path": "user_dict.csv" 테스트 필요!!

curl -XDELETE http://localhost:9200/korean_analyzer?pretty

curl -XPUT http://localhost:9200/korean_analyzer?pretty -H 'Content-Type: application/json' -d '{
  "settings" : {
    "index":{
      "analysis":{
        "analyzer":{
          "korean":{
            "type":"custom",
            "tokenizer":"seunjeon_default_tokenizer"
          }
        },
          "tokenizer": {
            "seunjeon_default_tokenizer": {
              "index_eojeol": "true",
              "user_words": [
                "텐바이텐", "엠디"
              ],
              "index_poses": [
                "UNK", "EP", "I", "J", "M",
                "N", "SL", "SH", "SN", "VCP",
                "XP", "XS", "XR"
              ],
              "decompound": "true",
              "type": "seunjeon_tokenizer"
            }
          }
      }
    }
  },
  "mappings": {
    "_doc" : {
      "properties" : {
        "text" : {
          "type" : "text",
          "analyzer": "korean"
        }
      }
    }
  }
}'

curl -XPOST http://localhost:9200/korean_analyzer/_analyze?pretty -H 'Content-Type: application/json' -d '{
    "analyzer":"korean",
    "text":"텐바이텐 엠디가 추천합니다."
}'

동의어 사전 추가

sudo vi /etc/elasticsearch/synonyms.txt
노레바,noreva,노래바
airpods,에어팟,airpod,airpot
2080,이공팔공
curl -XDELETE http://localhost:9200/korean_analyzer?pretty

curl -XPUT http://localhost:9200/korean_analyzer?pretty -H 'Content-Type: application/json' -d '{
  "settings" : {
    "index":{
      "analysis":{
        "analyzer":{
          "korean":{
            "type":"custom",
            "tokenizer":"seunjeon_default_tokenizer",
            "filter" : ["synonym"]
          }
        },
        "filter" : {
          "synonym" : {
            "type" : "synonym",
            "synonyms_path" : "synonyms.txt"
          }
        },
        "tokenizer": {
          "seunjeon_default_tokenizer": {
            "index_eojeol": "true",
            "user_words": [
              "텐바이텐", "엠디", "에어팟", "러브플라보", "씰스티커",
              "1+1"
            ],
            "index_poses": [
                "UNK", "EP", "I", "J", "M",
                "N", "SL", "SH", "SN", "VCP",
                "XP", "XS", "XR"
            ],
            "decompound": "true",
            "type": "seunjeon_tokenizer"
          }
        }
      }
    }
  },
  "mappings": {
    "_doc" : {
      "properties" : {
        "text" : {
          "type" : "text",
          "analyzer": "korean"
        }
      }
    }
  }
}'

curl -XPOST http://localhost:9200/korean_analyzer/_analyze?pretty -H 'Content-Type: application/json' -d '{
    "analyzer":"korean",
    "text":"airpod과 에어팟은 동의어일까?"
}'

답글 남기기