# JSON parsing errors when using Mustache Conditional

**URL:** <https://discuss.elastic.co/t/json-parsing-errors-when-using-mustache-conditional/134109>\
**Category:** Elasticsearch\
**Created:** [May 31, 2018, 8:35pm UTC](https://discuss.elastic.co/t/json-parsing-errors-when-using-mustache-conditional/134109 "2018-05-31T20:35:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmbaughman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmbaughman/32/31391_2.png) [@dmbaughman](https://discuss.elastic.co/u/dmbaughman)\
**Post date:** [May 31, 2018, 8:35pm UTC](https://discuss.elastic.co/t/json-parsing-errors-when-using-mustache-conditional/134109/1 "2018-05-31T20:35:38Z")

</div>

Hey everyone. I'm new to Elasticsearch, and am trying to figure out how to use Search Templates. They seem like a very promising way to simplify client-size search requests. But when I try to upload a template that uses a Mustache section (e.g. `{{#propName}}`), it is obviously not valid JSON and the request fails.

So my question is, how am I supposed to upload a Mustache template? BTW, I'm using Elasticsearch on AWS, so storing a .mustache file isn't an option.

Thanks!

**Request**

```
curl -X PUT "https://hostname/_template/product_categories" -H 'Content-Type: application/json' -d'
{
  "template": {
    "from": 0,
    "size": 30,
    "stored_fields": [],
    "_source": ["category.name"],
    "query": {
      "function_score": {
        "functions": [
          {
            "field_value_factor": {
              "field": "popularity",
              "factor": 1.004,
              "missing": 0
            }
          },
          {
            "filter": { "match": { "is_accessory": true }},
            "weight": 0.48
          }
        ],
        "query": {
          "bool": {
            "must": {
              {{#searchTerm}}
              "multi_match": {
                "query": "{{ searchTerm }}",
                "fuzziness": "auto",
                "operator": "and",
                "type": "best_fields",
                "fields": [
                  "name.regular.{{ lang }}^2",
                  "description.short.{{ lang }}",
                  "keywords.{{ lang }}^0.5",
                  "description.long.{{ lang }}^0.25"
                ]
              }
              {{/searchTerm}}
              {{#points}}
              "term": {
                "points": {{ points }}
              }
              {{/points}}
            },
            "filter": {
              "bool": {
                "must": [
                  { "term": { "channel_id": 1 }},
                  { "term": { "doc_type": "product" }}
                ]
              }
            }
          }
        }
      }
    }
  }
}
'

```

**Response**

```
{
  "error": {
    "root_cause": [{
      "type": "parse_exception",
      "reason": "Failed to parse content to map"
    }],
    "type": "parse_exception",
    "reason": "Failed to parse content to map",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Unexpected character ('{' (code 123)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@1eff8d7e; line: 26, column: 16]"
    }
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [June 1, 2018, 7:45am UTC](https://discuss.elastic.co/t/json-parsing-errors-when-using-mustache-conditional/134109/2 "2018-06-01T07:45:51Z")

</div>

Welcome to the Elastic community! There's a couple of things wrong with your request.

First, you're trying to register the search template using the `/_template` endpoint, but that endpoint is for _index_ templates (a way of preconfiguring settings and mappings for new indexes). Search templates [need to be registered as mustache scripts using the `_scripts` endpoint](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#pre-registered-templates).

Second, if you use conditionals, the request is not valid JSON, as you already noticed. In that case, you need to register the template source as a string.

So, a valid request would look something like this:

```auto
curl -XPOST "http://hostname:9200/_scripts/product_categories" -H 'Content-Type: application/json' -d'
{
  "script": {
    "lang": "mustache",
    "source":" { \"from\": 0, \"size\": 30, \"stored_fields\": [], \"_source\": [\"category.name\"], \"query\": { \"function_score\": { \"functions\": [{ \"field_value_factor\": { \"field\": \"popularity\", \"factor\": 1.004, \"missing\": 0 } }, { \"filter\": { \"match\": { \"is_accessory\": true }}, \"weight\": 0.48 }], \"query\": { \"bool\": { \"must\": { {{#searchTerm}} \"multi_match\": { \"query\": \"{{ searchTerm }}\", \"fuzziness\": \"auto\", \"operator\": \"and\", \"type\": \"best_fields\", \"fields\": [\"name.regular.{{ lang }}^2\", \"description.short.{{ lang }}\", \"keywords.{{ lang }}^0.5\", \"description.long.{{ lang }}^0.25\"] } {{/searchTerm}} {{#points}} \"term\": { \"points\": {{ points }} } {{/points}} }, \"filter\": { \"bool\": { \"must\": [{ \"term\": { \"channel_id\": 1 }}, { \"term\": { \"doc_type\": \"product\" }}] } } } } } }}"
  }
}'

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 29, 2018, 7:46am UTC](https://discuss.elastic.co/t/json-parsing-errors-when-using-mustache-conditional/134109/3 "2018-06-29T07:46:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
