# Query Template not working

**URL:** <https://discuss.elastic.co/t/query-template-not-working/28948>\
**Category:** Elasticsearch\
**Created:** [September 9, 2015, 4:24pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948 "2015-09-09T16:24:55Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 9, 2015, 4:24pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/1 "2015-09-09T16:24:55Z")

</div>

Hi,

I have created a query template, which involves multiple aggregations with default fields as below:

```auto
{ "template" : {
          "size": 0,
          "aggs": {
            "range": {
              "date_range": {
                "field": "{{time_field}}{{^time_field}}@timestamp{{/time_field}}",
                "format": "yyyy-MM-dd",
                "ranges": [
                  {
                    "from": "{{start_date}}{{^start_date}}now-1w/w{{/start_date}}",
                    "to": "{{end_date}}{{^end_date}}now/w{{/end_date}}"
                  }
                ]
              },
              "aggs": {
                "agg_by_interval": {
                  "date_histogram": {
                    "field": "{{time_field}}{{^time_field}}@timestamp{{/time_field}}",
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "interval": "{{interval}}{{^interval}}1w{{/interval}}"
                  },
                  "aggs": {
                    "groupby_field": {
                      "terms": {
                        "size": 0,
                        "field": "{{group_by_field}}"
                      },
                      "aggs": {
                        "percentiles_of_field": {
                          "percentiles": {
                            "field": "{{analytics_field}}",
                            "percents": ["{{#percents}}", "{{.}}", "{{/percents}}"]
			}
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
} 

```

When I run the template, it simply retrieves the first 10 documents and does not perform any aggregations.  
Whereas when I run the same query without using the template it works fine.

Am I missing something with the templates here?  
I have run other simpler templates which work fine for me.

Thanks.

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 9, 2015, 6:01pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/2 "2015-09-09T18:01:38Z")

</div>

I get a feeling that templates are not working with POST based searches. It is anything to do with that? If so how to overcome that.

Please help.

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 10, 2015, 5:00pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/3 "2015-09-10T17:00:54Z")

</div>

Hi,  
doesn't seem like the reason could be POST vs GET, how did you get to this conclusion? That said I am not sure yet what goes wrong with your request, nothing I can see at first glance.

The render search template endpoint would be great to use here, see [https://github.com/elastic/elasticsearch/issues/6821](https://github.com/elastic/elasticsearch/issues/6821) , but it comes with 2.0 beta 1 only. Maybe you can give it a try though.

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 10, 2015, 7:26pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/4 "2015-09-10T19:26:46Z")

</div>

Thanks @javanna  
Regarding POST vs GET:  
I am not entirely sure why but, as we know there are some searches that work with only POST and others with GET.  
The above query when I run with GET it get error. And with POST I get the correct result.  
But whereas when I use template to run the same query, I get error with POST, and GET does not give correct result. [Actually returns records without any aggregations]

So I wonder, how the query template is executed at the back end to handle POST and GET based queries.

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 11, 2015, 7:05am UTC](https://discuss.elastic.co/t/query-template-not-working/28948/5 "2015-09-11T07:05:42Z")

</div>

Can you please post the error that you get back when you use GET?

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 11, 2015, 4:45pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/6 "2015-09-11T16:45:10Z")

</div>

Sorry @javanna , I do not get error with GET. The query returns the documents but not aggregated results. Similar to the result when I use GET with template.

I get the following error with POST using the template:

**Query**

```auto
{
  "template": {
    "id": "PERCENTILES_OF_FIELD_IN_DATE_RANGE"
  },
  "params": {
    "start_time": "2015-08-17||-1w/w",
    "end_time": "2015-08-17||+1w/w",
    "group_by_field": "apiname-short",
    "analytics_field": "response-time (ms)",
    "percents" : [90, 99]
  }
}

```

**Error**

```auto
"error": "SearchPhaseExecutionException[Failed
 to execute phase [query], all shards failed; shardFailures 
{[b_iKE2bJStC-yX1HRFfvow][logstash-enabler-2015.07.01][0]: 
SearchParseException[[logstash-enabler-2015.07.01][0]: 
from[-1],size[-1]: Parse Failure [Failed to parse source 
[{"aggs":{"range":{"aggs":{"agg_by_interval":{"aggs":{"groupby_field":{"aggs":{"percentiles_of_field":{"percentiles":{"field":"response-time
 
(ms)","percents":["","90","","99",""]}}},"terms":{"field":"apiname-short","size":0}}},"date_histogram":{"field":"@timestamp","interval":"1w","format":"yyyy-MM-dd
 
HH:mm:ss"}}},"date_range":{"field":"@timestamp","ranges":[{"to":"now/w","from":"now-1w/w"}],"format":"yyyy-MM-dd"}}},"size":0}]]];
 nested: NumberFormatException[empty String]; }
...

, "status": 400
}

```

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 18, 2015, 4:04pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/8 "2015-09-18T16:04:41Z")

</div>

Hi @javanna  
Can you please look into the error posted above.  
Still trying to figure out what is wrong here.

Thanks.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 18, 2015, 4:08pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/9 "2015-09-18T16:08:36Z")

</div>

> [@vilas](#):
>
> "percents":["","90","","99",""]}}}

This is what is causing the error. The percents passed into the percentiles agg need to be numbers and `""` is not a number

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 18, 2015, 4:59pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/10 "2015-09-18T16:59:02Z")

</div>

Yes @colings86  
I observed the same. But the `""` are inserted by template.  
I tried with input as:

```auto
 "percents" : [99]

```

Still it inserts `""` and generates this:

```auto
"percents":["","99",""]

```

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [September 18, 2015, 5:19pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/11 "2015-09-18T17:19:21Z")

</div>

FYI I am using Elasticsearch 1.5.2

---

<div class="post-metadata">

**Author:** ![vilas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vilas/32/613_2.png) [@vilas](https://discuss.elastic.co/u/vilas)\
**Post date:** [November 16, 2015, 9:42pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/12 "2015-11-16T21:42:25Z")

</div>

Please let me know if this issue is identified and resolved?

---

<div class="post-metadata">

**Author:** ![leethaksor](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@leethaksor](https://discuss.elastic.co/u/leethaksor)\
**Post date:** [November 19, 2015, 10:42am UTC](https://discuss.elastic.co/t/query-template-not-working/28948/13 "2015-11-19T10:42:20Z")

</div>

I don't really have a good solution for you, this is more like a "me too" type of reply.

Anyway I also ran into this issue a while ago and I just couldn't get the template to work. It seemed that the mustache template parser always inserts those quotes. My issue was with document ids. When I tried to make a query that searched for something based on a list of ids I couldn't get it to work. A single id worked just fine, but when I tried to specify a list of anything I always got the quotes. I noticed that the same template works if I put it in a file in the scripts folder in the node. I lived with that for a while, but eventually I got frustrated with how awkward it is to maintain the template files and I got rid of them. So now I have given up on templates and I simply have my own templating system based on constant strings that I fill with required details. It's far from ideal but what can you do...

So the template thing would be great if it worked, but as it stands it seems broken.

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 19, 2015, 10:51am UTC](https://discuss.elastic.co/t/query-template-not-working/28948/14 "2015-11-19T10:51:04Z")

</div>

Without having dug too deeply into the template query you posted this sounds a lot like the following issue:

> <https://github.com/elastic/elasticsearch/issues/10992>

Can you check you are still having this issue with ES 2.0?

---

<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:** [July 5, 2017, 10:12pm UTC](https://discuss.elastic.co/t/query-template-not-working/28948/16 "2017-07-05T22:12:21Z")

</div>


