본문 바로가기

Elasticsearch (ELK)/Elasticsearch

7-2. 데이터 색인과 텍스트 분석(실전) - 애널라이저 & 토큰필터

1. 애널라이저 - Analyzer

Elasticsearch 에는 애널라이저를 조합하고 그 동작을 자세히 확인할 수 있는 API 들이 있다.

 

_analyze 텍스트 분석

// Request
GET _analyze
{
  "text": "The quick brown fox jumps over the lazy dog",
  "tokenizer": "whitespace",
  "filter": [
    "lowercase",
    "stop",
    "snowball"
  ]
}

// Response
{
  "tokens" : [
    {
      "token" : "quick",
      "start_offset" : 4,
      "end_offset" : 9,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "brown",
      "start_offset" : 10,
      "end_offset" : 15,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "fox",
      "start_offset" : 16,
      "end_offset" : 19,
      "type" : "word",
      "position" : 3
    },
    {
      "token" : "jump",
      "start_offset" : 20,
      "end_offset" : 25,
      "type" : "word",
      "position" : 4
    },
    {
      "token" : "over",
      "start_offset" : 26,
      "end_offset" : 30,
      "type" : "word",
      "position" : 5
    },
    {
      "token" : "lazi",
      "start_offset" : 35,
      "end_offset" : 39,
      "type" : "word",
      "position" : 7
    },
    {
      "token" : "dog",
      "start_offset" : 40,
      "end_offset" : 43,
      "type" : "word",
      "position" : 8
    }
  ]
}

Tokenizer : 1개만 적용가능

Filter : 여러개 적용가능, 순서에따라 결과가 달라지니 주의하자

 

더 정확한 예들은 아래 링크에서 확인하자 :

https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html

 

Create a custom analyzer | Elasticsearch Reference [7.7] | Elastic

Create a custom analyzeredit When the built-in analyzers do not fulfill your needs, you can create a custom analyzer which uses the appropriate combination of: Configurationedit The custom analyzer accepts the following parameters: tokenizer A built-in or

www.elastic.co

 

_analyze 적용

//my_index2 인덱스의 message 필드에 snowball 애널라이저 적용

PUT my_index2
{
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "snowball"
      }
    }
  }
}

 

2. Term 쿼리 (Match vs Term)

Term 쿼리는 Match와 비슷하지만 Analyzer를 적용하지 않고 입력된 검색어 그대로 일치하는 텀을 찾습니다.

따라서 jumps, jumping 으로 검색하면 결과가 나타나지 않고 jump로 검색해야 결과가 나타납니다.

(결국 고유명사 등에 많이 사용될듯).

GET my_index2/_search
{
  "query": {
    "term": {
      "message": "jumps"
    }
  }
}

위와같은 원문대로 검색하는 특징을 Full Text Search 또는 전문 검색 이라고 불리운다.

 

3. 사용자 정의 애널라이저 - Custom Analyzer

Custom Analyzer 생성

my_index3 인덱스의 settings 안에 my_custom_analyzer 생성
PUT my_index3
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_custom_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "stop",
              "snowball"
            ]
          }
        }
      }
    }
  }
}

Custom Analyzer 사용

_analyzer API 로 my_index2 에서 my_custom_analyzer 사용
GET my_index3/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": [
    "The quick brown fox jumps over the lazy dog"
  ]
}

 

Custom Token Filter 생성 & 적용

my_stop_filter 를 생성 후 my_custom_analyzer 에서 사용
PUT my_index3
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_custom_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "my_stop_filter",
              "snowball"
            ]
          }
        },
        "filter": {
          "my_stop_filter": {
            "type": "stop",
            "stopwords": [
              "brown"
            ]
          }
        }
      }
    }
  }
}

 

Mapping에 Custom Analyzer 적용

// message 필드에 my_custom_analyzer 애널라이저 적용

PUT my_index3
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_custom_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "my_stop_filter",
              "snowball"
            ]
          }
        },
        "filter": {
          "my_stop_filter": {
            "type": "stop",
            "stopwords": [
              "brown"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "my_custom_analyzer"
      }
    }
  }
}

 

4. 텀 벡터 - _termvectors

역 인덱스의 자세한 내용을 확인하고자할때 _termvectors를 사용한다.

// my_index3/_doc/1 도큐먼트의 message 필드의 termvectors 확인
// 여러개 필드를 확인하고자 할때는 ?fields=field1,field2 와 같이 쉼표로 나열할 수 있다

GET my_index3/_termvectors/1?fields=message