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

Term Vectors API (텀 벡터 API)

원문 보기 위키 갱신

_termvectors API는 단일 문서의 텀 벡터(term vector) 정보를 가져오는 API예요. 텀 벡터는 문서 안의 텀(단어)에 대한 상세 정보를 제공하는데, 여기에는 텀 빈도(term frequency), 위치(positions), 오프셋(offsets), 페이로드(payloads) 등이 포함됩니다. 관련성 점수 계산, 하이라이팅, 유사도 계산 같은 애플리케이션에서 유용하게 쓰일 수 있어요. 자세한 내용은 Term vector parameter 문서를 참고하세요.

출처: OpenSearch 문서

본문

Endpoints

GET  /{index}/_termvectors
POST /{index}/_termvectors
GET  /{index}/_termvectors/{id}
POST /{index}/_termvectors/{id}

Path parameters

다음 표는 사용할 수 있는 경로 파라미터 목록이에요.

| Parameter | Required | Data type | Description | | index | Required | String | The name of the index containing the document. | | id | Optional | String | The unique identifier of the document. |

Query parameters

다음 표는 사용할 수 있는 쿼리 파라미터 목록이에요. 모든 쿼리 파라미터는 선택 사항입니다.

| Parameter | Data type | Description | | field_statistics | Boolean | If true, the response includes the document count, sum of document frequencies, and sum of total term frequencies. (Default: true) | | fields | List or String | A comma-separated list or a wildcard expression specifying the fields to include in the statistics. Used as the default list unless a specific field list is provided in the completion_fields or fielddata_fields parameters. | | offsets | Boolean | If true, the response includes term offsets. (Default: true) | | payloads | Boolean | If true, the response includes term payloads. (Default: true) | | positions | Boolean | If true, the response includes term positions. (Default: true) | | preference | String | Specifies the node or shard on which the operation should be performed. See preference query parameter for a list of available options. By default the requests are routed randomly to available shard copies (primary or replica), with no guarantee of consistency across repeated queries. | | realtime | Boolean | If true, the request is real time as opposed to near real time. (Default: true) | | routing | List or String | A custom value used to route operations to a specific shard. | | term_statistics | Boolean | If true, the response includes term frequency and document frequency. (Default: false) | | version | Integer | If true, returns the document version as part of a hit. | | version_type | String | The specific version type. Valid values are: - external: The version number must be greater than the current version. - external_gte: The version number must be greater than or equal to the current version. - force: The version number is forced to be the given value. - internal: The version number is managed internally by OpenSearch. |

Request body fields

다음 표는 요청 본문에서 지정할 수 있는 필드 목록이에요.

| Field | Data type | Description | | doc | Object | A document to analyze. If provided, the API does not retrieve an existing document from the index but uses the provided content. | | fields | Array of strings | A list of field names for which to return term vectors. | | offsets | Boolean | If true, the response includes character offsets for each term. (Default: true) | | payloads | Boolean | If true, the response includes payloads for each term. (Default: true) | | positions | Boolean | If true, the response includes token positions. (Default: true) | | field_statistics | Boolean | If true, the response includes statistics such as document count, sum of document frequencies, and sum of total term frequencies. (Default: true) | | term_statistics | Boolean | If true, the response includes term frequency and document frequency. (Default: false) | | routing | String | A custom routing value used to identify the shard. Required if custom routing was used during indexing. | | version | Integer | The specific version of the document to retrieve. | | version_type | String | The type of versioning to use. Valid values: internal, external, external_gte, force. | | filter | Object | Allows filtering of tokens returned in the response (for example, by frequency or position). See Filtering terms for available options. | | per_field_analyzer | Object | Specifies a custom analyzer to use per field. Format: { "field_name": "analyzer_name" }. | | preference | String | Specifies shard or node routing preferences. See preference query parameter. |

Filtering terms

요청 본문의 filter 객체를 사용하면 텀 벡터 응답에 포함할 토큰을 필터링할 수 있어요. filter 객체는 다음 필드들을 지원합니다.

| Field | Data type | Description | | max_num_terms | Integer | The maximum number of terms to return. | | min_term_freq | Integer | The minimum term frequency in the document required for a term to be included. | | max_term_freq | Integer | The maximum term frequency in the document required for a term to be included. | | min_doc_freq | Integer | The minimum document frequency across the index required for a term to be included. | | max_doc_freq | Integer | The maximum document frequency across the index required for a term to be included. | | min_word_length | Integer | The minimum length of the term to be included. | | max_word_length | Integer | The maximum length of the term to be included. |

Example requests

인덱스를 만듭니다.

PUT /my-index
{
"mappings": {
"properties": {
"text": {
"type": "text",
"term_vector": "with_positions_offsets_payloads"
}
}
}
}

copy

문서를 인덱싱합니다.

REST Python

POST /my-index/_doc/1
{
"text": "OpenSearch is a search engine."
}

Copy Copy as cURL

response = client.index(
index = "my-index",
id = "1",
body =   {
"text": "OpenSearch is a search engine."
}
)

Copy

Example request

텀 벡터를 가져옵니다.

REST Python

GET /my-index/_termvectors/1
{
"fields": [
"text"
],
"term_statistics": true
}

Copy Copy as cURL

response = client.termvectors(
index = "my-index",
id = "1",
body =   {
"fields": [
"text"
],
"term_statistics": true
}
)

Copy 또는 fields와 term_statistics를 쿼리 파라미터로 제공할 수도 있어요.

REST Python

GET /my-index/_termvectors/1?fields=text&term_statistics=true

Copy Copy as cURL

response = client.termvectors(
index = "my-index",
id = "1",
params = { "fields": "text", "term_statistics": "true" },
body = { "Insert body here" }
)

Copy

Example response

응답은 텀 벡터 정보를 보여줍니다.

{
"_index": "my-index",
"_id": "1",
"_version": 1,
"found": true,
"took": 1,
"term_vectors": {
"text": {
"field_statistics": {
"sum_doc_freq": 5,
"doc_count": 1,
"sum_ttf": 5
},
"terms": {
"a": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 2,
"start_offset": 14,
"end_offset": 15
}
]
},
"engine": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 4,
"start_offset": 23,
"end_offset": 29
}
]
},
"is": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 1,
"start_offset": 11,
"end_offset": 13
}
]
},
"opensearch": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 0,
"start_offset": 0,
"end_offset": 10
}
]
},
"search": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 3,
"start_offset": 16,
"end_offset": 22
}
]
}
}
}
}
}

Response body fields

다음 표는 모든 응답 본문 필드 목록이에요.

| Field | Data type | Description | | term_vectors | Object | Contains term vector data for each specified field. | | term_vectors.text | Object | Contains term vector details for the text field. | | term_vectors.text.field_statistics | Object | Contains statistics for the entire field. Present only if field_statistics is true. | | term_vectors.text.field_statistics.doc_count | Integer | The number of documents that contain at least one term in the specified field. | | term_vectors.text.field_statistics.sum_doc_freq | Integer | The sum of document frequencies for all terms in the field. | | term_vectors.text.field_statistics.sum_ttf | Integer | The sum of total term frequencies (including repetitions) for all terms in the field. | | term_vectors.text.terms | Object | A map, in which each key is a term and each value contains details about that term. | | term_vectors.text.terms..term_freq | Integer | The number of times the term appears in the document. | | term_vectors.text.terms..doc_freq | Integer | The number of documents containing the term. Present only if term_statistics is true. | | term_vectors.text.terms..ttf | Integer | The total term frequency across all documents. Present only if term_statistics is true. | | term_vectors.text.terms..tokens | Array | A list of token objects providing information about individual term instances. | | term_vectors.text.terms..tokens[].position | Integer | The position of the token within the text. Present only if positions is true. | | term_vectors.text.terms..tokens[].start_offset | Integer | The start character offset of the token. Present only if offsets is true. | | term_vectors.text.terms..tokens[].end_offset | Integer | The end character offset of the token. Present only if offsets is true. | | term_vectors.text.terms..tokens[].payload | String (Base64) | Optional payload data associated with the token. Present only if payloads is true and available. |

Required permissions

Security 플러그인을 사용한다면 indices:data/read/tv 권한이 있는지 확인하세요.

더 알아보기 (Learn more)

텀 벡터를 사용하려면 먼저 필드 매핑에서 term_vector 설정을 활성화해야 정확한 위치·오프셋·페이로드 정보를 얻을 수 있어요. 검색 하이라이팅이나 유사 문서 탐색 로직을 만들 때 텀 벡터가 어떻게 활용되는지 OpenSearch의 supported field types 텍스트 문서를 함께 보면 도움이 됩니다.