ELASTICSEARCH

[엘라스틱서치] 5. 데이터 색인, 사용자 정의 토큰 필터

sejin2 2024. 3. 13. 10:40

데이터 색인 : 역인덱스 ( 단어 → 매핑문서 )
                        → tokenizer filter

GET _analyze
{
  "text":"Today is a day where I feel lucky and happy",
  "tokenizer":"whitespace", 
  "filter":[
    "lowercase",  // 소문자로 변환
    "stop",       // 불용어 제외
    "snowball"    // 단어의 원형
  ]
}

"snowball" // 단어의 원형 → 을 넣으면, y는 i 로 바뀌어서 저장됨
빼면 lucky, happy 그대로 나옴
"stop", // 불용어 제외 → is, a, and 이런거 빠짐, 빼고 넣으면 다 나옴

결과값

{
  "tokens": [
    {
      "token": "today",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 0
    },
    {
      "token": "day",
      "start_offset": 11,
      "end_offset": 14,
      "type": "word",
      "position": 3
    },
    {
      "token": "where",
      "start_offset": 15,
      "end_offset": 20,
      "type": "word",
      "position": 4
    },
    {
      "token": "i",
      "start_offset": 21,
      "end_offset": 22,
      "type": "word",
      "position": 5
    },
    {
      "token": "feel",
      "start_offset": 23,
      "end_offset": 27,
      "type": "word",
      "position": 6
    },
    {
      "token": "lucki",
      "start_offset": 28,
      "end_offset": 33,
      "type": "word",
      "position": 7
    },
    {
      "token": "happi",
      "start_offset": 38,
      "end_offset": 43,
      "type": "word",
      "position": 9
    }
  ]
}

 

사용자 정의 토큰 필터 만들기

PUT t_index2
{
  "settings":{
    "index":{
      "analysis":{
        "analyzer":{
          "my_custom_analyzer":{
            "type":"custom",
            "tokenizer":"whitespace",
            "filter":[
              "lowercase",
              "my_stop_filter",
              "snowball"
            ]
          }
        },
        "filter":{
          "my_stop_filter":{
            "type":"stop",
            "stopwords":[
              "feel"
            ]
          }
        }
      }
    }
  }
}

“feel” 하나만 불용어로 설정

*불용어 : 뭔가 막고자하는 키워드가 있을 때 사용
 불용어(Stop words)는 일반적으로 검색 엔진에서 무시되는 단어로, 특정 언어에 대해 일반적으로 사용되지만 검색 쿼리에
 포함되면 검색 결과의 유효성을 낮출 수 있는 단어이다.
 예를 들어, "the", "and", "but"과 같은 매우 일반적인 단어들은 불용어로 간주되며 검색 시 무시될 수 있다.

GET t_index2/_analyze
{
  "analyzer":"my_custom_analyzer",
  "text":["Today Is A day where I feel lucky and happy"]
}

조회해보면, 불용어로 설정한 feel이 조회되지 않는 것을 확인할 수 있다.

{
  "tokens": [
    {
      "token": "today",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 0
    },
    {
      "token": "is",
      "start_offset": 6,
      "end_offset": 8,
      "type": "word",
      "position": 1
    },
    {
      "token": "a",
      "start_offset": 9,
      "end_offset": 10,
      "type": "word",
      "position": 2
    },
    {
      "token": "day",
      "start_offset": 11,
      "end_offset": 14,
      "type": "word",
      "position": 3
    },
    {
      "token": "where",
      "start_offset": 15,
      "end_offset": 20,
      "type": "word",
      "position": 4
    },
    {
      "token": "i",
      "start_offset": 21,
      "end_offset": 22,
      "type": "word",
      "position": 5
    },
    {
      "token": "lucki",
      "start_offset": 28,
      "end_offset": 33,
      "type": "word",
      "position": 7
    },
    {
      "token": "and",
      "start_offset": 34,
      "end_offset": 37,
      "type": "word",
      "position": 8
    },
    {
      "token": "happi",
      "start_offset": 38,
      "end_offset": 43,
      "type": "word",
      "position": 9
    }
  ]
}

 

_termvectors : term의 분석에 대한 자세한 정보 보기

GET t_index3/_termvectors/1?fields=msg

{
  "_index": "t_index3",
  "_id": "1",
  "_version": 1,
  "found": true,
  "took": 65,
  "term_vectors": {
    "msg": {
      "field_statistics": {
        "sum_doc_freq": 10,
        "doc_count": 1,
        "sum_ttf": 10
      },
      "terms": {
        "a": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 2,
              "start_offset": 9,
              "end_offset": 10
            }
          ]
        },
        "and": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 8,
              "start_offset": 34,
              "end_offset": 37
            }
          ]
        },
        "day": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 3,
              "start_offset": 11,
              "end_offset": 14
            }
          ]
        },
        "feel": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 6,
              "start_offset": 23,
              "end_offset": 27
            }
          ]
        },
        "happy": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 9,
              "start_offset": 38,
              "end_offset": 43
            }
          ]
        },
        "i": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 5,
              "start_offset": 21,
              "end_offset": 22
            }
          ]
        },
        "is": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 1,
              "start_offset": 6,
              "end_offset": 8
            }
          ]
        },
        "lucky": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 7,
              "start_offset": 28,
              "end_offset": 33
            }
          ]
        },
        "today": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 0,
              "start_offset": 0,
              "end_offset": 5
            }
          ]
        },
        "where": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 4,
              "start_offset": 15,
              "end_offset": 20
            }
          ]
        }
      }
    }
  }
}