Sections in ElasticSearch search template (mostache)

26 Views Asked by At

I'm writing an ElasticSearch (ES) search template and would like to conditionally render a section of the template based on the existence of a parameter. Using an example from the ES documentation it's possible to do the following:

POST _render/template
{
  "source": "{ \"query\": { \"bool\": { \"must\": {{#toJson}}clauses{{/toJson}} }}}",
  "params": {
    "clauses": [
      {
        "term": {
          "user.id": "kimchy"
        }
      },
      {
        "term": {
          "url.domain": "example.com"
        }
      }
    ]
  }
}

The documentation (under Sections on the same page linked above) also calls out the ability to conditionally render a section of the template based on the existence of a parameter. Using the above example I'm unable to render the following template (just adding a check for the parameter clauses {{#clauses}} {{/clauses}})

POST _render/template
{
  "source": "{ {{#clauses}} \"query\": { \"bool\": { \"must\": {{#toJson}}clauses{{/toJson}} }} {{#clauses}} }",
  "params": {
    "clauses": [
      {
        "term": {
          "user.id": "kimchy"
        }
      },
      {
        "term": {
          "url.domain": "example.com"
        }
      }
    ]
  }
}





Am I missing anything obvious here on why this is not a valid search template?

The rendered error is always a JSON parse exception: type" : "json_parse_exception", "reason" : "Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: (org.opensearch.common.io.stream.InputStreamStreamInput); line: 1, column: 98]"

0

There are 0 best solutions below