# How Aggs use template like "template query"?

**URL:** <https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898>\
**Category:** Elasticsearch\
**Created:** [October 13, 2016, 6:38am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898 "2016-10-13T06:38:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 13, 2016, 6:38am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/1 "2016-10-13T06:38:43Z")

</div>

How Aggs use template like "template query"?

---

<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:** [October 13, 2016, 7:58am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/2 "2016-10-13T07:58:47Z")

</div>

Could you provide some details on what you are actually trying to do here?

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 13, 2016, 8:08am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/3 "2016-10-13T08:08:31Z")

</div>

this is my template query demo;

```auto
client.preparePutIndexedScript("mustache","first_template",
                "{\n" +
                " \"template\":{\n" +
                " \"query\": {\n" +
                " \"term\": {\n" +
                " \"event_type\": {\n" +
                " \"value\": \"{{event_type}}\"\n" +
                " }\n" +
                " }\n" +
                " }\n" +
                " }\n" +
                "}").get();

```

how can I do this:

```auto
        client.preparePutIndexedScript("mustache","first_template_aggs",
                "{\n" +
                        " \"template\":{\n" +
                        " \"aggs\": {\n" +
                        " \"test\": {\n" +
                        " \"terms\": {\n" +
                        " \"field\": \"event_type\",\n" +
                        " \"size\": 10\n" +
                        " }\n" +
                        " }\n" +
                        " }\n" +
                        " }\n" +
                        "}").get();

```

---

<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:** [October 13, 2016, 8:32am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/4 "2016-10-13T08:32:56Z")

</div>

Why are you wanting to template that request? It doesn't contain any placeholder values. In any case, you can template the whole of the search request using the Search Templates feature: [https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-template.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-template.html)

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 13, 2016, 9:05am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/5 "2016-10-13T09:05:50Z")

</div>

this happened error!

```auto
client.prepareSearch("index")
                .setQuery(QueryBuilders.templateQuery("first_template_aggs", ScriptService.ScriptType.INDEXED, map)).setSize(0);

```

---

<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:** [October 13, 2016, 9:21am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/6 "2016-10-13T09:21:13Z")

</div>

That is the template query. You need to use the search template feature. You need to use the `.setTemplate()` method on the `SearchRequestBuilder`if you are using the Java API.

I'm unclear though why you are wanting to use search templates with the JAVA API. It would be much easier and more performant for you to build the full search request (not using templates) in Java code and execute that. The use case for search templates is really for the rest type clients.

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 13, 2016, 9:40am UTC](https://discuss.elastic.co/t/how-aggs-use-template-like-template-query/62898/7 "2016-10-13T09:40:27Z")

</div>

thanks ! I think it is more flexible,The future need only modify the template, do not need to modify the Java code

---

<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/how-aggs-use-template-like-template-query/62898/8 "2017-07-05T22:12:40Z")

</div>


