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

Analyze API

원문 보기 위키 갱신

Analyze API를 사용하면 텍스트 분석을 수행할 수 있어요. 텍스트 분석은 비정형 텍스트를 검색에 최적화된 개별 토큰(보통 단어)으로 변환하는 과정이에요. 캐릭터 필터, 토크나이저, 토큰 필터, 노멀라이저 같은 일반적인 분석 구성 요소에 대한 자세한 내용은 Analyzers 문서를 참고하세요.

Analyze API는 텍스트 문자열을 분석해서 그 결과로 나온 토큰을 반환해요.

Security 플러그인을 사용한다면 manage index 권한이 필요해요. 단순히 텍스트만 분석하려면 manage cluster 권한이 필요해요.

출처: OpenSearch 문서

본문

엔드포인트 (Endpoints)

GET /_analyze
GET /{index}/_analyze
POST /_analyze
POST /{index}/_analyze

analyze 요청은 GET과 POST 둘 다로 보낼 수 있지만, 둘 사이에는 중요한 차이가 있어요. GET 요청은 데이터가 인덱스에 캐시되어 다음에 같은 데이터를 요청할 때 더 빨리 가져올 수 있어요. POST 요청은 인덱스에 아직 존재하지 않는 문자열을 분석기에 보내서 이미 인덱스에 있는 데이터와 비교해요. POST 요청은 캐시되지 않아요.

경로 매개변수 (Path parameter)

요청에 다음과 같은 선택적 경로 매개변수를 포함할 수 있어요.

| Parameter | Data type | Description | | index | String | Index that is used to derive the analyzer. |

요청 본문 필드 (Request body fields)

다음 표는 사용할 수 있는 요청 본문 필드를 나열한 것이에요.

| Field | Data type | Description | | text | String or Array of Strings | The text to analyze. If you provide an array of strings, the text is analyzed as a multi-value field. Required. | | analyzer | String | The name of the analyzer to apply to the text field. The analyzer can be built or configured in the index.If analyzer is not specified, the Analyze API uses the analyzer defined in the mapping of the field field.If the field field is not specified, the Analyze API uses the default analyzer for the index. If no index is specified or the index does not have a default analyzer, the Analyze API uses the standard analyzer. Optional. See Analyzers. | | attributes | Array of Strings | An array of token attributes for filtering the output of the explain field. | | char_filter | Array of Strings | An array of character filters for preprocessing characters before the tokenizer field. Optional. See Character filters. | | explain | Boolean | If true, causes the response to include token attributes and additional details. Optional. Default is false. | | field | String | The field for deriving the analyzer. If you specify field, you must also specify the index path parameter. If you specify the analyzer field, it overrides the value of field. If you do not specify field, the Analyze API uses the default analyzer for the index. If you do not specify the index field or the index does not have a default analyzer, the Analyze API uses the standard analyzer. Optional. | | filter | Array of Strings | Array of token filters to apply after the tokenizer field. Optional. See Token filters. | | normalizer | String | A normalizer for converting text into a single token. Optional. See Normalizers. | | tokenizer | String | A tokenizer for converting the text field into tokens. Optional. See Tokenizers. |

예제 요청 (Example requests)

Analyze array of text strings

Apply a built-in analyzer

Apply a custom analyzer

Apply a custom transient analyzer

Specify an index

Derive the analyzer from an index field

Specify a normalizer

Get token details

Set a token limit

문자열 배열 분석 (Analyze array of text strings)

text 필드에 문자열 배열을 전달하면 다중 값(multi-value) 필드로 분석돼요.

GET /_analyze
{
"analyzer": "standard",
"text": [
"first array element",
"second array element"
]
}
response = client.indices.analyze(
body =   {
"analyzer": "standard",
"text": [
"first array element",
"second array element"
]
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "first",
"start_offset" : 0,
"end_offset" : 5,
"type" : "",
"position" : 0
},
{
"token" : "array",
"start_offset" : 6,
"end_offset" : 11,
"type" : "",
"position" : 1
},
{
"token" : "element",
"start_offset" : 12,
"end_offset" : 19,
"type" : "",
"position" : 2
},
{
"token" : "second",
"start_offset" : 20,
"end_offset" : 26,
"type" : "",
"position" : 3
},
{
"token" : "array",
"start_offset" : 27,
"end_offset" : 32,
"type" : "",
"position" : 4
},
{
"token" : "element",
"start_offset" : 33,
"end_offset" : 40,
"type" : "",
"position" : 5
}
]
}

내장 분석기 적용 (Apply a built-in analyzer)

index 경로 매개변수를 생략하면 내장 분석기 중 아무거나 텍스트 문자열에 적용할 수 있어요.

다음 요청은 standard 내장 분석기를 사용해 텍스트를 분석해요.

GET /_analyze
{
"analyzer": "standard",
"text": "OpenSearch text analysis"
}
response = client.indices.analyze(
body =   {
"analyzer": "standard",
"text": "OpenSearch text analysis"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "opensearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "",
"position" : 0
},
{
"token" : "text",
"start_offset" : 11,
"end_offset" : 15,
"type" : "",
"position" : 1
},
{
"token" : "analysis",
"start_offset" : 16,
"end_offset" : 24,
"type" : "",
"position" : 2
}
]
}

커스텀 분석기 적용 (Apply a custom analyzer)

자체 분석기를 만들어 analyze 요청에서 지정할 수 있어요.

이 시나리오에서는 lowercase_ascii_folding 커스텀 분석기가 만들어져 books2 인덱스에 연결돼 있어요. 이 분석기는 텍스트를 소문자로 변환하고 비-ASCII 문자를 ASCII로 변환해요.

다음 요청은 주어진 텍스트에 커스텀 분석기를 적용해요.

GET /books2/_analyze
{
"analyzer": "lowercase_ascii_folding",
"text": "Le garçon m'a SUIVI."
}
response = client.indices.analyze(
index = "books2",
body =   {
"analyzer": "lowercase_ascii_folding",
"text": "Le garçon m'a SUIVI."
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "le",
"start_offset" : 0,
"end_offset" : 2,
"type" : "",
"position" : 0
},
{
"token" : "garcon",
"start_offset" : 3,
"end_offset" : 9,
"type" : "",
"position" : 1
},
{
"token" : "m'a",
"start_offset" : 10,
"end_offset" : 13,
"type" : "",
"position" : 2
},
{
"token" : "suivi",
"start_offset" : 14,
"end_offset" : 19,
"type" : "",
"position" : 3
}
]
}

커스텀 임시 분석기 적용 (Apply a custom transient analyzer)

토크나이저, 토큰 필터, 캐릭터 필터로 커스텀 임시 분석기를 만들 수 있어요. 토큰 필터를 지정하려면 filter 매개변수를 사용하세요.

다음 요청은 uppercase 캐릭터 필터를 사용해 텍스트를 대문자로 변환해요.

GET /_analyze
{
"tokenizer": "keyword",
"filter": [
"uppercase"
],
"text": "OpenSearch filter"
}
response = client.indices.analyze(
body =   {
"tokenizer": "keyword",
"filter": [
"uppercase"
],
"text": "OpenSearch filter"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "OPENSEARCH FILTER",
"start_offset" : 0,
"end_offset" : 17,
"type" : "word",
"position" : 0
}
]
}

다음 요청은 html_strip 필터를 사용해 텍스트에서 HTML 문자를 제거해요.

GET /_analyze
{
"tokenizer": "keyword",
"filter": [
"lowercase"
],
"char_filter": [
"html_strip"
],
"text": "Leave right now!"
}
response = client.indices.analyze(
body =   {
"tokenizer": "keyword",
"filter": [
"lowercase"
],
"char_filter": [
"html_strip"
],
"text": "Leave right now!"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "leave right now!",
"start_offset" : 3,
"end_offset" : 23,
"type" : "word",
"position" : 0
}
]
}

배열을 사용해 필터들을 조합할 수도 있어요.

다음 요청은 lowercase 변환을 stopwords 배열의 단어를 제거하는 stop 필터와 조합해요.

GET /_analyze
{
"tokenizer": "whitespace",
"filter": [
"lowercase",
{
"type": "stop",
"stopwords": [
"to",
"in"
]
}
],
"text": "how to train your dog in five steps"
}
response = client.indices.analyze(
body =   {
"tokenizer": "whitespace",
"filter": [
"lowercase",
{
"type": "stop",
"stopwords": [
"to",
"in"
]
}
],
"text": "how to train your dog in five steps"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "how",
"start_offset" : 0,
"end_offset" : 3,
"type" : "word",
"position" : 0
},
{
"token" : "train",
"start_offset" : 7,
"end_offset" : 12,
"type" : "word",
"position" : 2
},
{
"token" : "your",
"start_offset" : 13,
"end_offset" : 17,
"type" : "word",
"position" : 3
},
{
"token" : "dog",
"start_offset" : 18,
"end_offset" : 21,
"type" : "word",
"position" : 4
},
{
"token" : "five",
"start_offset" : 25,
"end_offset" : 29,
"type" : "word",
"position" : 6
},
{
"token" : "steps",
"start_offset" : 30,
"end_offset" : 35,
"type" : "word",
"position" : 7
}
]
}

인덱스 지정 (Specify an index)

인덱스의 기본 분석기로 텍스트를 분석하거나, 다른 분석기를 지정할 수도 있어요.

다음 요청은 books 인덱스에 연결된 기본 분석기를 사용해 주어진 텍스트를 분석해요.

GET /books/_analyze
{
"text": "OpenSearch analyze test"
}
response = client.indices.analyze(
index = "books",
body =   {
"text": "OpenSearch analyze test"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

"tokens" : [
{
"token" : "opensearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "",
"position" : 0
},
{
"token" : "analyze",
"start_offset" : 11,
"end_offset" : 18,
"type" : "",
"position" : 1
},
{
"token" : "test",
"start_offset" : 19,
"end_offset" : 23,
"type" : "",
"position" : 2
}
]
}

다음 요청은 keyword 분석기를 사용해 주어진 텍스트를 분석해요. keyword 분석기는 텍스트 전체 값을 단일 토큰으로 반환해요.

GET /books/_analyze
{
"analyzer": "keyword",
"text": "OpenSearch analyze test"
}
response = client.indices.analyze(
index = "books",
body =   {
"analyzer": "keyword",
"text": "OpenSearch analyze test"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "OpenSearch analyze test",
"start_offset" : 0,
"end_offset" : 23,
"type" : "word",
"position" : 0
}
]
}

인덱스 필드에서 분석기 파생 (Derive the analyzer from an index field)

인덱스에 텍스트와 필드를 함께 전달할 수 있어요. 이 API는 해당 필드의 분석기를 찾아서 그 분석기로 텍스트를 분석해요.

매핑이 존재하지 않으면 API는 standard 분석기를 사용하며, 이 분석기는 모든 텍스트를 소문자로 변환하고 공백 기준으로 토큰화해요.

다음 요청은 name에 대한 매핑을 기준으로 분석을 수행해요.

GET /books2/_analyze
{
"field": "name",
"text": "OpenSearch analyze test"
}
response = client.indices.analyze(
index = "books2",
body =   {
"field": "name",
"text": "OpenSearch analyze test"
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "opensearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "",
"position" : 0
},
{
"token" : "analyze",
"start_offset" : 11,
"end_offset" : 18,
"type" : "",
"position" : 1
},
{
"token" : "test",
"start_offset" : 19,
"end_offset" : 23,
"type" : "",
"position" : 2
}
]
}

노멀라이저 지정 (Specify a normalizer)

keyword 필드 대신 인덱스에 연결된 노멀라이저를 사용할 수 있어요. 노멀라이저는 분석 결과가 단일 토큰을 만들도록 해요.

이 예제에서 books2 인덱스는 to_lower_fold_ascii라는 노멀라이저를 포함하며, 이 노멀라이저는 텍스트를 소문자로 변환하고 비-ASCII 텍스트를 ASCII로 변환해요.

다음 요청은 텍스트에 to_lower_fold_ascii를 적용해요.

GET /books2/_analyze
{
"normalizer": "to_lower_fold_ascii",
"text": "C'est le garçon qui m'a suivi."
}
response = client.indices.analyze(
index = "books2",
body =   {
"normalizer": "to_lower_fold_ascii",
"text": "C'est le garçon qui m'a suivi."
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "c'est le garcon qui m'a suivi.",
"start_offset" : 0,
"end_offset" : 30,
"type" : "word",
"position" : 0
}
]
}

토큰 필터와 캐릭터 필터로 커스텀 임시 노멀라이저를 만들 수도 있어요.

다음 요청은 uppercase 캐릭터 필터를 사용해 주어진 텍스트를 모두 대문자로 변환해요.

GET /_analyze
{
"filter": [
"uppercase"
],
"text": "That is the boy who followed me."
}
response = client.indices.analyze(
body =   {
"filter": [
"uppercase"
],
"text": "That is the boy who followed me."
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"tokens" : [
{
"token" : "THAT IS THE BOY WHO FOLLOWED ME.",
"start_offset" : 0,
"end_offset" : 32,
"type" : "word",
"position" : 0
}
]
}

토큰 세부 정보 가져오기 (Get token details)

explain 속성을 true로 설정하면 모든 토큰에 대한 추가 세부 정보를 얻을 수 있어요.

다음 요청은 standard 토크나이저와 함께 사용된 reverse 필터에 대한 상세 토큰 정보를 제공해요.

GET /_analyze
{
"tokenizer": "standard",
"filter": [
"reverse"
],
"text": "OpenSearch analyze test",
"explain": true,
"attributes": [
"keyword"
]
}
response = client.indices.analyze(
body =   {
"tokenizer": "standard",
"filter": [
"reverse"
],
"text": "OpenSearch analyze test",
"explain": true,
"attributes": [
"keyword"
]
}
)

이전 요청은 다음과 같은 필드를 반환해요.

{
"detail" : {
"custom_analyzer" : true,
"charfilters" : [ ],
"tokenizer" : {
"name" : "standard",
"tokens" : [
{
"token" : "OpenSearch",
"start_offset" : 0,
"end_offset" : 10,
"type" : "",
"position" : 0
},
{
"token" : "analyze",
"start_offset" : 11,
"end_offset" : 18,
"type" : "",
"position" : 1
},
{
"token" : "test",
"start_offset" : 19,
"end_offset" : 23,
"type" : "",
"position" : 2
}
]
},
"tokenfilters" : [
{
"name" : "reverse",
"tokens" : [
{
"token" : "hcraeSnepO",
"start_offset" : 0,
"end_offset" : 10,
"type" : "",
"position" : 0
},
{
"token" : "ezylana",
"start_offset" : 11,
"end_offset" : 18,
"type" : "",
"position" : 1
},
{
"token" : "tset",
"start_offset" : 19,
"end_offset" : 23,
"type" : "",
"position" : 2
}
]
}
]
}
}

토큰 한도 설정 (Set a token limit)

생성되는 토큰 수에 한도를 설정할 수 있어요. 더 낮은 값을 설정하면 노드의 메모리 사용량이 줄어들어요. 기본값은 10000이에요.

다음 요청은 토큰을 4개로 제한해요.

PUT /books2
{
"settings" : {
"index.analyze.max_token_count" : 4
}
}

위 요청은 analyze API가 아니라 인덱스 API예요. 자세한 내용은 Dynamic index settings를 참고하세요.

응답 본문 필드 (Response body fields)

텍스트 분석 엔드포인트는 다음과 같은 응답 필드를 반환해요.

| Field | Data type | Description | | tokens | Array | Array of tokens derived from the text. See token object. | | detail | Object | Details about the analysis and each token. Included only when you request token details. See detail object. |

Token object

| Field | Data type | Description | | token | String | The token’s text. | | start_offset | Integer | The token’s starting position within the original text string. Offsets are zero-based. | | end_offset | Integer | The token’s ending position within the original text string. | | type | String | Classification of the token: , , and so on. The tokenizer usually sets the type, but some filters define their own types. For example, the synonym filter defines the type. | | position | Integer | The token’s position within the tokens array. |

Detail object

| Field | Data type | Description | | custom_analyzer | Boolean | Whether the analyzer applied to the text is custom or built in. | | charfilters | Array | List of character filters applied to the text. | | tokenizer | Object | Name of the tokenizer applied to the text and a list of tokens* with content before the token filters were applied. | | tokenfilters | Array | List of token filters applied to the text. Each token filter includes the filter’s name and a list of tokens* with content after the filters were applied. Token filters are listed in the order they are specified in the request. |

토큰 필드 설명은 token object를 참고하세요.

필요한 권한 (Required permissions)

Security 플러그인을 사용한다면 적절한 권한을 확인하세요: indices:admin/analyze

  • Text analysis
  • Analyzers
  • Tokenizers
  • Token filters
  • Normalizers

더 알아보기 (Learn more)