Elastic 가이드북
  • Elastic 가이드 북
  • 1. 서문
    • 1.1 Elastic Stack 소개
      • 1.1.1 Elasticsearch
      • 1.1.2 Logstash
      • 1.1.3 Kibana
      • 1.1.4 Beats
  • 2. Elasticsearch 시작하기
    • 2.1 데이터 색인
    • 2.2 설치 및 실행
      • 2.2.1 다운로드 설치 및 실행
      • 2.2.2 Unix RPM (yum) 설치 및 실행
      • 2.2.3 윈도우 운영체제에서 MSI 파일로 설치
    • 2.3 elasticsearch 환경 설정
      • 2.3.1 jvm.options
      • 2.3.2 elasticsearch.yml
      • 2.3.3 노드의 역할 : master, data, ingest, ml
      • 2.3.4 커맨드 라인 설정
  • 3. Elasticsearch 시스템 구조
    • 3.1 클러스터 구성
    • 3.2 인덱스와 샤드 - Index & Shards
    • 3.3 마스터 노드와 데이터 노드 - Master & Data Nodes
  • 4. Elasticsearch 데이터 처리
    • 4.1 REST API
    • 4.2 CRUD - 입력, 조회, 수정, 삭제
    • 4.3 벌크 API - _bulk API
    • 4.4 검색 API - _search API
  • 5. 검색과 쿼리 - Query DSL
    • 5.1 풀 텍스트 쿼리 - Full Text Query
    • 5.2 Bool 복합 쿼리 - Bool Query
    • 5.3 정확도 - Relevancy
    • 5.4 Bool : Should
    • 5.5 정확값 쿼리 - Exact Value Query
    • 5.6 범위 쿼리 - Range Query
  • 6. 데이터 색인과 텍스트 분석
    • 6.1 역 인덱스 - Inverted Index
    • 6.2 텍스트 분석 - Text Analysis
    • 6.3 애널라이저 - Analyzer
      • 6.3.1 _analyze API
      • 6.3.2 Term 쿼리
      • 6.3.3 사용자 정의 애널라이저 - Custom Analyzer
      • 6.3.4 텀 벡터 - _termvectors API
    • 6.4 캐릭터 필터 - Character Filter
      • 6.4.1 HTML Strip
      • 6.4.2 Mapping
      • 6.4.3 Pattern Replace
    • 6.5 토크나이저 - Tokenizer
      • 6.5.1 Standard, Letter, Whitespace
      • 6.5.2 UAX URL Email
      • 6.5.3 Pattern
      • 6.5.4 Path Hierarchy
    • 6.6 토큰 필터 - Token Filter
      • 6.6.1 Lowercase, Uppercase
      • 6.6.2 Stop
      • 6.6.3 Synonym
      • 6.6.4 NGram, Edge NGram, Shingle
      • 6.6.5 Unique
    • 6.7 형태소 분석 - Stemming
      • 6.7.1 Snowball
      • 6.7.2 노리 (nori) 한글 형태소 분석기
  • 7. 인덱스 설정과 매핑 - Settings & Mappings
    • 7.1 설정 - Settings
    • 7.2 매핑 - Mappings
      • 7.2.1 문자열 - text, keyword
      • 7.2.2 숫자 - long, double ...
      • 7.2.3 날짜 - date
      • 7.2.4 불리언 - boolean
      • 7.2.5 Object 와 Nested
      • 7.2.6 위치 정보 - Geo
      • 7.2.7 기타 필드 타입 - IP, Range, Binary
    • 7.3 멀티 (다중) 필드 - Multi Field
  • 8. 집계 - Aggregations
    • 8.1 메트릭 - Metrics Aggregations
    • 8.2 버킷 - Bucket Aggregations
    • 8.3 하위 - sub-aggregations
    • 8.4 파이프라인 - Pipeline Aggregations
Powered by GitBook
On this page

Was this helpful?

  1. 6. 데이터 색인과 텍스트 분석
  2. 6.6 토큰 필터 - Token Filter

6.6.3 Synonym

이 문서의 허가되지 않은 무단 복제나 배포 및 출판을 금지합니다. 본 문서의 내용 및 도표 등을 인용하고자 하는 경우 출처를 명시하고 김종민(kimjmin@gmail.com)에게 사용 내용을 알려주시기 바랍니다.

검색 서비스에 따라서 동의어 검색을 제공해야 하는 경우가 있습니다. 예를 들면 클라우드 서비스 관련 정보를 검색하는 시스템에서 "AWS" 라는 단어를 검색했을 때 "Amazon" 또는 한글 "아마존" 도 같이 검색을 하도록 하면 관련된 정보를 더 많이 찾을 수 있을 것입니다. 이 때 Synonym 토큰 필터를 사용하면 텀의 동의어 저장이 가능합니다.

동의어를 설정하는 옵션은 synonyms 항목에서 직접 동의어 목록을 입력하는 방법과 동의어 사전 파일을 만들어 synonyms_path 로 지정하는 방법이 있습니다. 동의어 사전 명시 규칙에는 다음의 것들이 있습니다.

  • "A, B => C" : 왼쪽의 A, B 대신 오른쪽의 C 텀을 저장합니다. A, B 로는 C 의 검색이 가능하지만 C 로는 A, B 가 검색되지 않습니다.

  • "A, B" : A, B 각 텀이 A 와 B 두개의 텀을 모두 저장합니다. A 와 B 모두 서로의 검색어로 검색이 됩니다.

다음은 my_synonym 인덱스에 "amazon => aws" 으로 동의어를 지정하는 예제입니다.

"amazon => aws" 동의어를 지정하는 my_synonym 인덱스 생성
PUT my_synonym
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_syn": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "syn_aws"
          ]
        }
      },
      "filter": {
        "syn_aws": {
          "type": "synonym",
          "synonyms": [
            "amazon => aws"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "my_syn"
      }
    }
  }
}

이제 여기에 "Amazon Web Service", "AWS" 값을 가진 도큐먼트 두 개를 저장하고 각 도큐먼트의 _termvectors 를 확인 해 보겠습니다.

AWS, Amazon Web Service 도큐먼트 저장
PUT my_synonym/_doc/1
{ "message" : "Amazon Web Service" }
PUT my_synonym/_doc/2
{ "message" : "AWS" }
1 도큐먼트 message 필드의 termvectors 확인
GET my_synonym/_termvectors/1?fields=message
1 도큐먼트 message 필드의 termvectors 확인 결과
{
  "_index" : "my_synonym",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "took" : 9,
  "term_vectors" : {
    "message" : {
      "field_statistics" : {
        "sum_doc_freq" : 4,
        "doc_count" : 2,
        "sum_ttf" : 4
      },
      "terms" : {
        "aws" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 6
            }
          ]
        },
        "service" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 2,
              "start_offset" : 11,
              "end_offset" : 18
            }
          ]
        },
        "web" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 1,
              "start_offset" : 7,
              "end_offset" : 10
            }
          ]
        }
      }
    }
  }
}
2 도큐먼트 message 필드의 termvectors 확인
GET my_synonym/_termvectors/2?fields=message
2 도큐먼트 message 필드의 termvectors 확인 결과
{
  "_index" : "my_synonym",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "found" : true,
  "took" : 0,
  "term_vectors" : {
    "message" : {
      "field_statistics" : {
        "sum_doc_freq" : 4,
        "doc_count" : 2,
        "sum_ttf" : 4
      },
      "terms" : {
        "aws" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 3
            }
          ]
        }
      }
    }
  }
}

1 도큐먼트의 "Amazon Web Service"가 amazon 대신 "aws", "web", "service" 로 저장된 것을 확인할 수 있습니다. 다음은 각각 term 쿼리로 aws, amazon 을 검색한 결과와 match 쿼리로 amazon 을 검색한 결과입니다.

term 쿼리로 aws 검색
GET my_synonym/_search
{
  "query": {
    "term": {
      "message": "aws"
    }
  }
}
term 쿼리로 aws 검색 결과
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.22920427,
    "hits" : [
      {
        "_index" : "my_synonym",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.22920427,
        "_source" : {
          "message" : "AWS"
        }
      },
      {
        "_index" : "my_synonym",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.1513613,
        "_source" : {
          "message" : "Amazon Web Service"
        }
      }
    ]
  }
}
term 쿼리로 amazon 검색
GET my_synonym/_search
{
  "query": {
    "term": {
      "message": "amazon"
    }
  }
}
term 쿼리로 amazon 검색 결과
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
match 쿼리로 amazon 검색
GET GET my_synonym/_search
{
  "query": {
    "match": {
      "message": "amazon"
    }
  }
}
match 쿼리로 amazon 검색 결과
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.22920427,
    "hits" : [
      {
        "_index" : "my_synonym",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.22920427,
        "_source" : {
          "message" : "AWS"
        }
      },
      {
        "_index" : "my_synonym",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.1513613,
        "_source" : {
          "message" : "Amazon Web Service"
        }
      }
    ]
  }
}

첫 번째 쿼리와 마지막 세 번째 쿼리의 결과가 동일합니다.

term 쿼리는 검색어에 애널라이저를 적용하지 않고 그대로 검색하기 때문에 term 쿼리로 aws 를 검색하면 두개의 도큐먼트가 모두 검색되고 amazon을 검색 하면 검색이 되지 않습니다. match 쿼리는 검색어 amazon도 my_syn 애널라이저가 적용이 되어 aws 로 변환하여 검색을 하기 때문에 aws로 검색을 한 것과 같은 결과가 나타납니다.

이번에는 my_synonym 인덱스에 "amazon, aws" 로 동의어를 지정하는 예제입니다. 기존의 my_synonym 인덱스를 먼저 삭제하고 입력합니다.

"amazon, aws" 동의어를 지정하는 my_synonym 인덱스 생성
PUT my_synonym
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_syn": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "syn_aws"
          ]
        }
      },
      "filter": {
        "syn_aws": {
          "type": "synonym",
          "synonyms": [
            "amazon, aws"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "my_syn"
      }
    }
  }
}

앞의 예제와 동일하게 "Amazon Web Service", "AWS" 값을 가진 도큐먼트 두 개를 저장하고 각 도큐먼트의 _termvectors 를 확인 해 보겠습니다.

AWS, Amazon Web Service 도큐먼트 저장
PUT my_synonym/_doc/1
{ "message" : "Amazon Web Service" }
PUT my_synonym/_doc/2
{ "message" : "AWS" }
1 도큐먼트 message 필드의 termvectors 확인
GET my_synonym/_termvectors/1?fields=message
1 도큐먼트 message 필드의 termvectors 확인 결과
{
  "_index" : "my_synonym",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "took" : 0,
  "term_vectors" : {
    "message" : {
      "field_statistics" : {
        "sum_doc_freq" : 6,
        "doc_count" : 2,
        "sum_ttf" : 6
      },
      "terms" : {
        "amazon" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 6
            }
          ]
        },
        "aws" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 6
            }
          ]
        },
        "service" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 2,
              "start_offset" : 11,
              "end_offset" : 18
            }
          ]
        },
        "web" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 1,
              "start_offset" : 7,
              "end_offset" : 10
            }
          ]
        }
      }
    }
  }
}
2 도큐먼트 message 필드의 termvectors 확인
GET my_synonym/_termvectors/2?fields=message
2 도큐먼트 message 필드의 termvectors 확인 결과
{
  "_index" : "my_synonym",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "found" : true,
  "took" : 0,
  "term_vectors" : {
    "message" : {
      "field_statistics" : {
        "sum_doc_freq" : 6,
        "doc_count" : 2,
        "sum_ttf" : 6
      },
      "terms" : {
        "amazon" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 3
            }
          ]
        },
        "aws" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 3
            }
          ]
        }
      }
    }
  }
}

두 도큐먼트 모두 "position" : 0 위치에 "aws", "amazon" 두개의 텀들이 모두 저장된 것을 확인할 수 있습니다. 이제 term 쿼리로 amazon 을 검색해도 두개 도큐먼트가 모두 검색이 됩니다. 이것은 한번 직접 실행 해 보시기 바랍니다.

동의어 여러 개를 입력 할 때는 "synonyms": [ ... ] 항목 안에 배열로 넣어도 되지만, 그 보다는 파일을 따로 만들어 관리하는 것이 편합니다. stop 토큰 필터와 마찬가지로 synonyms_path 항목에 config 디렉토리 기준의 상대 경로에 파일을 저장하고 경로명을 입력하면 됩니다. 동의어는 하나의 규칙당 한 줄씩 입력해야 하며 파일은 UTF-8로 인코딩 되어야 합니다.

다음은 "hop, jump", "quick, fast" 를 user_dic/my_syn_dic.txt 에 저장해서 동의어 사전으로 사용하는 예제입니다.

config/user_dic 디렉토리 아래에 my_syn_dic.txt 파일 생성
$ echo 'quick, fast
hop, jump' > config/user_dic/my_syn_dic.txt
synonyms_path 설정을 가진 my_synonym 인덱스 생성
PUT my_synonym
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_syn": {
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "syn_aws"
          ]
        }
      },
      "filter": {
        "syn_aws": {
          "type": "synonym",
          "synonyms_path": "user_dic/my_syn_dic.txt"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "my_syn"
      }
    }
  }
}

이제 term 쿼리로 quick 과 jump를 검색하면 hop, fast 도 검색이 됩니다.

quick, jump, hop, fast 를 포함하는 도큐먼트 저장
PUT my_synonym/_doc/1
{ "message": "Quick brown fox jump" }
PUT my_synonym/_doc/2
{ "message": "hop rabbit is fast" }
term 쿼리로 quick, jump 검색
GET GET my_synonym/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "message": "quick"
          }
        },
        {
          "term": {
            "message": "jump"
          }
        }
      ]
    }
  }
}
term 쿼리로 quick, jump 검색 결과
{
  "took" : 370,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.42221838,
    "hits" : [
      {
        "_index" : "my_synonym",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.42221838,
        "_source" : {
          "message" : "Quick brown fox jump"
        }
      },
      {
        "_index" : "my_synonym",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.42221838,
        "_source" : {
          "message" : "hop rabbit is fast"
        }
      }
    ]
  }
}

마지막으로 synonym 토큰 필터에는 추가적으로 다음과 같은 옵션들이 있습니다.

  • expand (true / false. 디폴트는 true)

    "expand": false 로 설정하게 되면 "synonyms": "aws, amazon" 같은 설정에 토큰들을 모두 저장하지 않고 맨 처음에 명시된 토큰 하나만 저장합니다. 앞의 설정은 "synonyms": "aws, amazon => aws" 로 설정한 것과 동일하게 동작합니다.

  • lenient (true / false. 디폴트는 false)

    "lenient": true 로 설정하면 synonym 설정에 오류가 있는 경우 오류가 있는 부분을 무시하고 실행합니다.

Previous6.6.2 StopNext6.6.4 NGram, Edge NGram, Shingle

Last updated 5 years ago

Was this helpful?