# Search templates - give entire query as param

**URL:** <https://discuss.elastic.co/t/search-templates-give-entire-query-as-param/167506>\
**Category:** Elasticsearch\
**Created:** [February 7, 2019, 6:43pm UTC](https://discuss.elastic.co/t/search-templates-give-entire-query-as-param/167506 "2019-02-07T18:43:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roxanatest](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@Roxanatest](https://discuss.elastic.co/u/Roxanatest)\
**Post date:** [February 7, 2019, 6:43pm UTC](https://discuss.elastic.co/t/search-templates-give-entire-query-as-param/167506/1 "2019-02-07T18:43:02Z")

</div>

I want the entire elasticsearch template query to be given as parameter. The template to be something like

```auto
    {
        "lang": "mustache",
        "source": "\{\"query\": {{query_string}}"
      }

```

And for the query\_string to have for example `"match_all":{}`

However, this does not work. How can it be configured to work?

---

<div class="post-metadata">

**Author:** ![glenacota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glenacota/32/41367_2.png) [@glenacota](https://discuss.elastic.co/u/glenacota)\
**Post date:** [March 4, 2019, 12:06pm UTC](https://discuss.elastic.co/t/search-templates-give-entire-query-as-param/167506/2 "2019-03-04T12:06:08Z")

</div>

Hi,  
it would be helpful to know what is the exact error you get. But from your snippet, I'd say that you're getting a `json_parse_exception`, because Mustache is trying to escape the `{{query_string}}` tag.  
If this is the case, you need to avoid automatic escaping by using the triple mustache (see: [https://mustache.github.io/mustache.5.html](https://mustache.github.io/mustache.5.html)), like in the snippet below:

```auto
{
  "lang": "mustache",
  "source": "{\"query\": {{{query_string}}} }"
}

```

**Update** :  
[According to the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/search-template.html#_converting_parameters_to_json), this will also work

```auto
{
  "lang": "mustache",
  "source": "{\"query\": {{#toJson}}query_string{{/toJson}} }"
}

```

---

<div class="post-metadata">

**Author:** ![Roxanatest](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@Roxanatest](https://discuss.elastic.co/u/Roxanatest)\
**Post date:** [March 4, 2019, 8:09pm UTC](https://discuss.elastic.co/t/search-templates-give-entire-query-as-param/167506/3 "2019-03-04T20:09:15Z")

</div>

The issue was a different one - by default, mustache will escape the html characters. So, we just need to unescape them back. With {{#... }}

---

<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:** [April 1, 2019, 8:09pm UTC](https://discuss.elastic.co/t/search-templates-give-entire-query-as-param/167506/4 "2019-04-01T20:09:21Z")

</div>

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