Render Template

The Render Template API renders a search template as a search query.

Paths and HTTP methods

  1. GET /_render/template
  2. POST /_render/template
  3. GET /_render/template/<id>
  4. POST /_render/template/<id>

Path parameters

The Render Template API supports the following optional path parameter.

ParameterTypeDescription
idStringThe ID of the search template to render.

Request options

The following options are supported in the request body of the Render Template API.

ParameterRequiredTypeDescription
idConditionalStringThe 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.
paramsNoObjectA 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.
sourceConditionalObjectAn inline search template to render if a search template is not specified. Supports the same parameters as a Search API request and Mustache variables.

Example request

Both of the following request examples use the search template with the template ID play_search_template:

  1. {
  2. "source": {
  3. "query": {
  4. "match": {
  5. "play_name": "{{play_name}}"
  6. }
  7. }
  8. },
  9. "params": {
  10. "play_name": "Henry IV"
  11. }
  12. }

Render template using template ID

The following example request validates a search template with the ID play_search_template:

  1. POST _render/template
  2. {
  3. "id": "play_search_template",
  4. "params": {
  5. "play_name": "Henry IV"
  6. }
  7. }

copy

Render template using _source

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:

  1. {
  2. "source": {
  3. "from": "{{from}}{{^from}}0{{/from}}",
  4. "size": "{{size}}{{^size}}10{{/size}}",
  5. "query": {
  6. "match": {
  7. "play_name": "{{play_name}}"
  8. }
  9. }
  10. },
  11. "params": {
  12. "play_name": "Henry IV"
  13. }
  14. }

copy

Example response

OpenSearch responds with information about the template’s output:

  1. {
  2. "template_output": {
  3. "from": "0",
  4. "size": "10",
  5. "query": {
  6. "match": {
  7. "play_name": "Henry IV"
  8. }
  9. }
  10. }
  11. }