Search templates

You can convert your full-text queries into a search template to accept user input and dynamically insert it into your query.

For example, if you use OpenSearch as a backend search engine for your application or website, you can take in user queries from a search bar or a form field and pass them as parameters into a search template. That way, the syntax to create OpenSearch queries is abstracted from your end users.

When you’re writing code to convert user input into OpenSearch queries, you can simplify your code with search templates. If you need to add fields to your search query, you can just modify the template without making changes to your code.

Search templates use the Mustache language. For a list of all syntax options, see the Mustache manual.

Create search templates

A search template has two components: the query and the parameters. Parameters are user-inputted values that get placed into variables. Variables are represented with double braces in Mustache notation. When encountering a variable like {{var}} in the query, OpenSearch goes to the params section, looks for a parameter called var, and replaces it with the specified value.

You can code your application to ask your user what they want to search for and then plug that value into the params object at runtime.

This command defines a search template to find a play by its name. The {{play_name}} in the query is replaced by the value Henry IV:

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

This template runs the search on your entire cluster. To run this search on a specific index, add the index name to the request:

  1. GET shakespeare/_search/template

Specify the from and size parameters:

  1. GET _search/template
  2. {
  3. "source": {
  4. "from": "{{from}}",
  5. "size": "{{size}}",
  6. "query": {
  7. "match": {
  8. "play_name": "{{play_name}}"
  9. }
  10. }
  11. },
  12. "params": {
  13. "play_name": "Henry IV",
  14. "from": 10,
  15. "size": 10
  16. }
  17. }

To improve the search experience, you can define defaults so the user doesn’t have to specify every possible parameter. If the parameter is not defined in the params section, OpenSearch uses the default value.

The syntax for defining the default value for a variable var is as follows:

  1. {{var}}{{^var}}default value{{/var}}

This command sets the defaults for from as 10 and size as 10:

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

Save and execute search templates

After the search template works the way you want it to, you can save the source of that template as a script, making it reusable for different input parameters.

When saving the search template as a script, you need to specify the lang parameter as mustache:

  1. POST _scripts/play_search_template
  2. {
  3. "script": {
  4. "lang": "mustache",
  5. "source": {
  6. "from": "{{from}}{{^from}}0{{/from}}",
  7. "size": "{{size}}{{^size}}10{{/size}}",
  8. "query": {
  9. "match": {
  10. "play_name": ""
  11. }
  12. }
  13. },
  14. "params": {
  15. "play_name": "Henry IV"
  16. }
  17. }
  18. }

Now you can reuse the template by referring to its id parameter. You can reuse this source template for different input values.

  1. GET _search/template
  2. {
  3. "id": "play_search_template",
  4. "params": {
  5. "play_name": "Henry IV",
  6. "from": 0,
  7. "size": 1
  8. }
  9. }

Sample output

  1. {
  2. "took": 7,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 6,
  6. "successful": 6,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3205,
  13. "relation": "eq"
  14. },
  15. "max_score": 3.641852,
  16. "hits": [
  17. {
  18. "_index": "shakespeare",
  19. "_type": "_doc",
  20. "_id": "4",
  21. "_score": 3.641852,
  22. "_source": {
  23. "type": "line",
  24. "line_id": 5,
  25. "play_name": "Henry IV",
  26. "speech_number": 1,
  27. "line_number": "1.1.2",
  28. "speaker": "KING HENRY IV",
  29. "text_entry": "Find we a time for frighted peace to pant,"
  30. }
  31. }
  32. ]
  33. }
  34. }

If you have a stored template and want to validate it, use the render operation:

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

Sample output

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

The following render operations are supported:

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

Advanced parameter conversion with search templates

You have a lot of different syntax options in Mustache to transpose the input parameters into a query. You can specify conditions, run loops, join arrays, convert arrays to JSON, and so on.

Conditions

Use the section tag in Mustache to represent conditions:

  1. {{#var}}var{{/var}}

When var is a boolean value, this syntax acts as an if condition. The {{#var}} and {{/var}} tags insert the values placed between them only if var evaluates to true.

Using section tags would make your JSON invalid, so you must write your query in a string format instead.

This command includes the size parameter in the query only when the limit parameter is set to true. In the following example, the limit parameter is true, so the size parameter is activated. As a result, you would get back only two documents.

  1. GET _search/template
  2. {
  3. "source": "{ {{#limit}} \"size\": \"{{size}}\", {{/limit}} \"query\":{\"match\":{\"play_name\": \"{{play_name}}\"}}}",
  4. "params": {
  5. "play_name": "Henry IV",
  6. "limit": true,
  7. "size": 2
  8. }
  9. }

You can also design an if-else condition. This command sets size to 2 if limit is true. Otherwise, it sets size to 10.

  1. GET _search/template
  2. {
  3. "source": "{ {{#limit}} \"size\": \"2\", {{/limit}} {{^limit}} \"size\": \"10\", {{/limit}} \"query\":{\"match\":{\"play_name\": \"{{play_name}}\"}}}",
  4. "params": {
  5. "play_name": "Henry IV",
  6. "limit": true
  7. }
  8. }

Loops

You can also use the section tag to implement a for each loop:

  1. {{#var}}{{.}}}{{/var}}

When var is an array, the search template iterates through it and creates a terms query.

  1. GET _search/template
  2. {
  3. "source": "{\"query\":{\"terms\":{\"play_name\":[\"{{#play_name}}\",\"{{.}}\",\"{{/play_name}}\"]}}}",
  4. "params": {
  5. "play_name": [
  6. "Henry IV",
  7. "Othello"
  8. ]
  9. }
  10. }

This template is rendered as:

  1. GET _search/template
  2. {
  3. "source": {
  4. "query": {
  5. "terms": {
  6. "play_name": [
  7. "Henry IV",
  8. "Othello"
  9. ]
  10. }
  11. }
  12. }
  13. }

Join

You can use the join tag to concatenate values of an array (separated by commas):

  1. GET _search/template
  2. {
  3. "source": {
  4. "query": {
  5. "match": {
  6. "text_entry": "{{#join}}{{text_entry}}{{/join}}"
  7. }
  8. }
  9. },
  10. "params": {
  11. "text_entry": [
  12. "To be",
  13. "or not to be"
  14. ]
  15. }
  16. }

Renders as:

  1. GET _search/template
  2. {
  3. "source": {
  4. "query": {
  5. "match": {
  6. "text_entry": "{0=To be, 1=or not to be}"
  7. }
  8. }
  9. }
  10. }

Convert to JSON

You can use the toJson tag to convert parameters to their JSON representation:

  1. GET _search/template
  2. {
  3. "source": "{\"query\":{\"bool\":{\"must\":[{\"terms\": {\"text_entries\": {{#toJson}}text_entries{{/toJson}} }}] }}}",
  4. "params": {
  5. "text_entries": [
  6. { "term": { "text_entry" : "love" } },
  7. { "term": { "text_entry" : "soldier" } }
  8. ]
  9. }
  10. }

Renders as:

  1. GET _search/template
  2. {
  3. "source": {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "terms": {
  9. "text_entries": [
  10. {
  11. "term": {
  12. "text_entry": "love"
  13. }
  14. },
  15. {
  16. "term": {
  17. "text_entry": "soldier"
  18. }
  19. }
  20. ]
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. }
  27. }

Multiple search templates

You can bundle multiple search templates and send them to your OpenSearch cluster in a single request using the msearch operation. This saves network round trip time, so you get back the response more quickly as compared to independent requests.

  1. GET _msearch/template
  2. {"index":"shakespeare"}
  3. {"id":"if_search_template","params":{"play_name":"Henry IV","limit":false,"size":2}}
  4. {"index":"shakespeare"}
  5. {"id":"play_search_template","params":{"play_name":"Henry IV"}}

Manage search templates

To list all scripts, run the following command:

  1. GET _cluster/state/metadata?pretty&filter_path=**.stored_scripts

To retrieve a specific search template, run the following command:

  1. GET _scripts/<name_of_search_template>

To delete a search template, run the following command:

  1. DELETE _scripts/<name_of_search_template>