분석기 테스트

원본 보기

분석기 테스트

analyze API는 분석기가 생성한 텀(term)을 확인할 수 있는 매우 유용한 도구입니다. 내장 분석기는 요청 안에 인라인으로 지정할 수 있습니다.

				POST _analyze
					{
  "analyzer": "whitespace",
  "text":     "The quick brown fox."
}
		

API는 다음과 같은 응답을 반환합니다.

{
  "tokens": [
    {
      "token": "The",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 0
    },
    {
      "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": 20,
      "type": "word",
      "position": 3
    }
  ]
}
		

다음 요소들의 조합을 테스트할 수도 있습니다.

  • 토크나이저 1개
  • 토큰 필터 0개 이상
  • 문자 필터 0개 이상
				POST _analyze
					{
  "tokenizer": "standard",
  "filter":  [ "lowercase", "asciifolding" ],
  "text":      "Is this déja vu?"
}
		

API는 다음과 같은 응답을 반환합니다.

{
  "tokens": [
    {
      "token": "is",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "this",
      "start_offset": 3,
      "end_offset": 7,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "deja",
      "start_offset": 8,
      "end_offset": 12,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "vu",
      "start_offset": 13,
      "end_offset": 15,
      "type": "<ALPHANUM>",
      "position": 3
    }
  ]
}
		
위치와 문자 오프셋

analyze API의 출력에서 볼 수 있듯이, 분석기는 단어를 텀으로 변환할 뿐만 아니라 각 텀의 순서 또는 상대적인 위치(구문 쿼리나 단어 근접도 쿼리에 사용됨)와 원본 텍스트에서 각 텀의 시작 및 끝 문자 오프셋(검색 결과 하이라이팅에 사용됨)도 기록합니다.

또는 특정 인덱스에서 analyze API를 실행할 때 custom 분석기를 참조할 수도 있습니다.

				PUT my-index-000001
					{
  "settings": {
    "analysis": {
      "analyzer": {
        "std_folded": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "my_text": {
        "type": "text",
        "analyzer": "std_folded"
      }
    }
  }
}
				GET my-index-000001/_analyze
					{
  "analyzer": "std_folded",
  "text":     "Is this déjà vu?"
}
				GET my-index-000001/_analyze
					{
  "field": "my_text",
  "text":  "Is this déjà vu?"
}
		
  1. std_folded라는 이름의 custom 분석기를 정의합니다.
  2. my_text 필드는 std_folded 분석기를 사용합니다.
  3. 이 분석기를 참조하려면 analyze API에 인덱스 이름을 지정해야 합니다.
  4. 이름으로 분석기를 참조합니다.
  5. my_text 필드가 사용하는 분석기를 참조합니다.

API는 다음과 같은 응답을 반환합니다.

{
  "tokens": [
    {
      "token": "is",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "this",
      "start_offset": 3,
      "end_offset": 7,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "deja",
      "start_offset": 8,
      "end_offset": 12,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "vu",
      "start_offset": 13,
      "end_offset": 15,
      "type": "<ALPHANUM>",
      "position": 3
    }
  ]
}