Elasticsearch 동의어 사전 추가(synonym)

By | 2021년 11월 10일
Table of Contents

Elasticsearch 동의어 사전 추가(synonym)

참조

참조

동의어 사전을 추가하여, 검색의 품질을 높일 수 있다.

동의어 사전 위치

동의어 사전은 설정파일에서의 상대경로로 지정된다.

설정파일이 디폴트로 /usr/share/elasticsearch/config 에 위치하므로,
이 경로에서의 상대경로로 설정한다.

동의어 사전 형식

/usr/share/elasticsearch/config/analysis/synonyms.txt

0100,공백
mmmg,밀리미터밀리그람,엠엠엠지

동의어 사전 적용 방식

  • 인덱스 생성/수정시

    동의어 사전에 새로운 단어를 추가해도, 인덱스를 재생성하지 않으면 반영이 않된다.

  • 검색어에 적용

    인덱스 재생성없이 즉시 반영이 된다.

인덱스 생성

curl -XDELETE 'localhost:9200/synonyms_test?pretty'

search_string 필드에 대해, search_analyzer 를 지정해 준다.

curl -XPUT 'localhost:9200/synonyms_test?pretty' -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym_analyzer": {
            "tokenizer": "whitespace",
            "filter": [
              "synonym_filter"
            ]
          }
        },
        "filter": {
          "synonym_filter": {
            "type": "synonym",
            "synonyms_path": "analysis/synonyms.txt",
            "updateable": true
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "search_string": {
        "type": "text",
        "search_analyzer": "synonym_analyzer"
      }
    }
  }
}'

데이타 입력

vi data.json
{ "index":{ "_index" : "synonyms_test", "_type" : "_doc" } }
{ "search_string":"0100"}
{ "index":{ "_index" : "synonyms_test", "_type" : "_doc" } }
{ "search_string":"공백"}
{ "index":{ "_index" : "synonyms_test", "_type" : "_doc" } }
{ "search_string":"mmmg"}
{ "index":{ "_index" : "synonyms_test", "_type" : "_doc" } }
{ "search_string":"밀리미터밀리그람"}
{ "index":{ "_index" : "synonyms_test", "_type" : "_doc" } }
{ "search_string":"엠엠엠지"}
curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' --data-binary @data.json

검색어 analyze

curl -XGET 'localhost:9200/synonyms_test/_analyze?pretty' -H 'Content-Type: application/json' -d'
{
  "analyzer": "synonym_analyzer",
  "text": "엠엠엠지"
}'

데이타 검색

search_string 에 대해, 동의어 검색이 된다.

curl -XGET 'localhost:9200/synonyms_test/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "from": 0,
  "size": 20,
  "sort": {
    "_score": "desc"
  },
  "query": {
    "bool": {
      "must": {
        "match": {
          "search_string": "mmmg"
        }
      }
    }
  }
}'

동어어 사전 Reload

동의어 사전이 변경된 경우 아래 명령으로 Reload 할 수 있다.

curl -XPOST 'localhost:9200/synonyms_test/_reload_search_analyzers?pretty'

동의어사전 관리 자동화

브랜드명, 카테고리명, 상품속성(색상, 사이즈 등)은 쉽게 변경되지 않으므로,
일단위로 자동 업데이트 및 수동추가하면 사전관리가 될 듯 한다.

One thought on “Elasticsearch 동의어 사전 추가(synonym)

답글 남기기