layout | title | nav_order |
---|---|---|
default |
Render Template |
82 |
The Render Template API renders a search template as a search query.
GET /_render/template
POST /_render/template
GET /_render/template/<id>
POST /_render/template/<id>
The Render Template API supports the following optional path parameter.
Parameter | Type | Description |
---|---|---|
id |
String | The ID of the search template to render. |
The following options are supported in the request body of the Render Template API.
Parameter | Required | Type | Description |
---|---|---|---|
id |
Conditional | String | The ID of the search template to render. Is not required if the ID is provided in the path or if an inline template is specified by the source . |
params |
No | Object | A list of key-value pairs that replace Mustache variables found in the search template. The key-value pairs must exist in the documents being searched. |
source |
Conditional | Object | An inline search template to render if a search template is not specified. Supports the same parameters as a Search API request and Mustache variables. |
Both of the following request examples use the search template with the template ID play_search_template
:
{
"source": {
"query": {
"match": {
"play_name": "{% raw %}{{play_name}}{% endraw %}"
}
}
},
"params": {
"play_name": "Henry IV"
}
}
The following example request validates a search template with the ID play_search_template
:
POST _render/template
{
"id": "play_search_template",
"params": {
"play_name": "Henry IV"
}
}
{% include copy.html %}
If you don't want to use a saved template, or want to test a template before saving, you can test a template with the _source
parameter using Mustache variables, as shown in the following example:
{
"source": {
"from": "{% raw %}{{from}}{{^from}}0{{/from}}{% endraw %}",
"size": "{% raw %}{{size}}{{^size}}10{{/size}}{% endraw %}",
"query": {
"match": {
"play_name": "{% raw %}{{play_name}}{% endraw %}"
}
}
},
"params": {
"play_name": "Henry IV"
}
}
{% include copy.html %}
OpenSearch responds with information about the template's output:
{
"template_output": {
"from": "0",
"size": "10",
"query": {
"match": {
"play_name": "Henry IV"
}
}
}
}