Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 1.23 KB

File metadata and controls

42 lines (33 loc) · 1.23 KB
endpoint render_search_template
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 render_search_template endpoint (Python example)

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"])

Conditional sections

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"])