Multi-search Template API
Multi-search Template API는 단일 API 요청에서 여러 검색 템플릿 요청을 실행해요.
출처: OpenSearch 문서
본문
엔드포인트 (Endpoints)
Multi-search Template API는 다음 경로를 사용해요.
GET /_msearch/template
POST /_msearch/template
GET /{index}/_msearch/template
POST /{index}/_msearch/template
쿼리 매개변수와 메타데이터 옵션 (Query parameters and metadata options)
모든 매개변수는 선택 사항이에요. 일부는 각 메타데이터 줄의 일부로 검색별로 적용할 수도 있어요.
| Parameter | Type | Description | Supported in metadata | | allow_no_indices | Boolean | Specifies whether to ignore wildcards that don’t match any indexes. Default is true. | Yes | | cancel_after_time_interval | Time | The interval of time after which the search request will be canceled. Supported at both parent and child request levels. The order of precedence is child-level parameter, parent-level parameter, and cluster setting. Default is -1. | Yes | | css_minimize_roundtrips | Boolean | Specifies whether OpenSearch should try to minimize the number of network round trips between the coordinating node and remote clusters (only applicable to cross-cluster search requests). Default is true. | No | | expand_wildcards | Enum | Expands wildcard expressions to concrete indexes. Combine multiple values with commas. Supported values are all, open, closed, hidden, and none. Default is open. | Yes | | ignore_unavailable | Boolean | If an index or shard from the index list doesn’t exist, then this setting specifies whether to ignore the missing index or shard rather than fail the query. Default is false. | Yes | | max_concurrent_searches | Integer | The maximum number of concurrent searches. The default depends on your node count and search thread pool size. Higher values can improve performance, but there may be a risk of overloading the cluster. | No | | max_concurrent_shard_requests | Integer | The maximum number of concurrent shard requests that each search executes per node. Default is 5. Higher values can improve performance, but there may be a risk of overloading the cluster. | No | | pre_filter_shard_size | Integer | Default is 128. | No | | rest_total_hits_as_int | String | Specifies whether the hits.total property is returned as an integer (true) or an object (false). Default is false. | No | | search_type | String | Affects the relevance score. Valid options are query_then_fetch and dfs_query_then_fetch. query_then_fetch scores documents using term and document frequencies for a single shard (faster, less accurate), whereas dfs_query_then_fetch uses term and document frequencies across all shards (slower, more accurate). Default is query_then_fetch. | Yes | | typed_keys | Boolean | Specifies whether to prefix aggregation names with their internal types in the response. Default is false. | No |
메타데이터 전용 옵션 (Metadata-only options)
일부 옵션은 전체 요청에 대한 매개변수로 적용할 수 없어요. 대신 각 메타데이터 줄의 일부로 검색별로 적용할 수 있어요. 모두 선택 사항이에요.
| Option | Type | Description | | index | String, string array | If you don’t specify an index or multiple indexes as part of the URL (or want to override the URL value for an individual search), you can include it under this option. Examples include "logs-*" and ["my-store", "sample_data_ecommerce"]. | | preference | String | Specifies the nodes or shards on which you want to perform the search. This setting can be useful for testing, but in most situations, the default behavior provides the best search latencies. Options include _local, _only_local, _prefer_nodes, _only_nodes, and _shards. These last three options accept a list of nodes or shards. Examples include "_only_nodes:data-node1,data-node2" and "_shards:0,1. | | request_cache | Boolean | Specifies whether to cache results, which can improve latency for repeated searches. Default is to use the index.requests.cache.enable setting for the index (which defaults to true for new indexes). | | routing | String | Comma-separated custom routing values, for example, "routing": "value1,value2,value3". |
요청 본문 (Request body)
multi-search 템플릿 요청 본문은 Multi-search API 패턴과 비슷한 패턴을 따라요.
Metadata\n
Query\n
Metadata\n
Query\n
- 메타데이터 줄에는 검색할 인덱스와 검색 유형 같은 옵션이 포함돼요.
- 쿼리 줄은 Query DSL을 사용해요.
bulk 작업과 마찬가지로 JSON은 최소화(민화)할 필요가 없어요. 공백은 괜찮지만, 한 줄에 있어야 해요. OpenSearch는 새줄 문자를 사용해 multi-search 요청을 파싱하며, 요청 본문이 새줄 문자로 끝나야 해요.
예제 요청 (Example request)
다음 msearch/template API 예제 요청은 line_search_template과 play_search_template이라는 여러 템플릿을 사용해 단일 인덱스에 대해 쿼리를 실행해요.
GET /_msearch/template
{"index":"shakespeare"}
{"id":"line_search_template","params":{"text_entry":"All the world's a stage","limit":false,"size":2}}
{"index":"shakespeare"}
{"id":"play_search_template","params":{"play_name":"Henry IV"}}
response = client.msearch_template(
body = '''
{"index":"shakespeare"}
{"id":"line_search_template","params":{"text_entry":"All the world's a stage","limit":false,"size":2}}
{"index":"shakespeare"}
{"id":"play_search_template","params":{"play_name":"Henry IV"}}
'''
)
예제 응답 (Example response)
OpenSearch는 multi-search 템플릿 요청과 같은 순서로 각 검색 결과를 담은 배열을 반환해요.
{
"took": 5,
"responses": [
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"status": 200
},
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"status": 200
}
]
}