본문 바로가기
WIKI 기술 지식 베이스

Condition token filter

원문 보기 위키 갱신

출처: 문서

본문

Condition 토큰 필터

condition 토큰 필터는 특정 기준에 따라 다른 토큰 필터를 조건부로 적용할 수 있게 해 주는 특수한 유형의 필터예요. 이를 통해 텍스트 분석 과정에서 특정 토큰 필터를 언제 적용할지 더 세밀하게 제어할 수 있어요. 여러 필터를 설정해 두고, 정의한 조건을 충족할 때만 적용되도록 만들 수도 있어요. 이 토큰 필터는 언어별 처리나 특수 문자를 다룰 때 특히 유용하게 쓸 수 있어요.

파라미터

condition 토큰 필터를 사용하려면 두 개의 파라미터를 반드시 설정해야 해요.

파라미터 필수/선택 데이터 타입 설명
filter 필수 Array 지정한 조건(script 파라미터로 정의)이 충족됐을 때 토큰에 어떤 토큰 필터를 적용할지 지정해요.
script 필수 Object filter 파라미터에서 지정한 필터가 적용되기 위해 충족해야 하는 조건을 정의하는 인라인 스크립트를 설정해요(인라인 스크립트만 허용돼요).

예제

다음 예제 요청은 my_conditional_index라는 새 인덱스를 생성하고 condition 필터가 설정된 애널라이저를 구성해요. 이 필터는 문자 시퀀스 "um"을 포함하는 모든 토큰에 lowercase 필터를 적용해요:

PUT /my_conditional_index
{
  "settings": {
    "analysis": {
      "filter": {
        "my_conditional_filter": {
          "type": "condition",
          "filter": ["lowercase"],
          "script": {
            "source": "token.getTerm().toString().contains('um')"
          }
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "my_conditional_filter"
          ]
        }
      }
    }
  }
}

{% include copy-curl.html %}

생성된 토큰

다음 요청을 사용해 애널라이저가 생성한 토큰을 확인할 수 있어요:

GET /my_conditional_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "THE BLACK CAT JUMPS OVER A LAZY DOG"
}

{% include copy-curl.html %}

응답에는 생성된 토큰이 담겨 있어요:

{
  "tokens": [
    {
      "token": "THE",
      "start_offset": 0,
      "end_offset": 3,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "BLACK",
      "start_offset": 4,
      "end_offset": 9,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "CAT",
      "start_offset": 10,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "jumps",
      "start_offset": 14,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "OVER",
      "start_offset": 20,
      "end_offset": 24,
      "type": "<ALPHANUM>",
      "position": 4
    },
    {
      "token": "A",
      "start_offset": 25,
      "end_offset": 26,
      "type": "<ALPHANUM>",
      "position": 5
    },
    {
      "token": "LAZY",
      "start_offset": 27,
      "end_offset": 31,
      "type": "<ALPHANUM>",
      "position": 6
    },
    {
      "token": "DOG",
      "start_offset": 32,
      "end_offset": 35,
      "type": "<ALPHANUM>",
      "position": 7
    }
  ]
}

더 알아보기 (Learn more)