| endpoint | render_search_template |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.render_search_template() to preview the query that a
Mustache template produces without executing it. This is invaluable
for debugging templates:
response = client.render_search_template(
source='{"query": {"match": {"{{field}}": "{{value}}"}}, "size": {{size}}}',
params={"field": "category", "value": "electronics", "size": 10},
)
print(response["template_output"])Use Mustache conditionals to build optional query clauses. Render the template to verify the output with and without the parameter:
template = '{"query": {"bool": {"must": [{"match": {"category": "{{category}}"}}{{#min_price}}, {"range": {"price": {"gte": {{min_price}}}}}{{/min_price}}]}}}'
with_filter = client.render_search_template(
source=template,
params={"category": "electronics", "min_price": 100},
)
print("With filter:", with_filter["template_output"])
without_filter = client.render_search_template(
source=template,
params={"category": "electronics"},
)
print("Without filter:", without_filter["template_output"])