# Search template: Only aggregate with param condition

**URL:** <https://discuss.elastic.co/t/search-template-only-aggregate-with-param-condition/310350>\
**Category:** Elasticsearch\
**Created:** [July 21, 2022, 11:00pm UTC](https://discuss.elastic.co/t/search-template-only-aggregate-with-param-condition/310350 "2022-07-21T23:00:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emporea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emporea/32/101071_2.png) [@Emporea](https://discuss.elastic.co/u/Emporea)\
**Post date:** [July 21, 2022, 11:00pm UTC](https://discuss.elastic.co/t/search-template-only-aggregate-with-param-condition/310350/1 "2022-07-21T23:00:59Z")

</div>

Maybe I am misunderstanding the obvious. But how am i able to use a search template (mustache) and only aggregate certain aggs if a param is set to true?

For example I have this search template

```auto
{
  id: "test_template",
  body: {
    script: {
      lang: "mustache",
      source: {
        query: {
          query_string: {
            query: "{{query_string}}",
          },
        },
        aggs: {
          min_date: { min: { field: "date" } },
          max_date: { max: { field: "date" } },
          title:
            {terms: { field: "title.keyword", size: 10 }},
        },
      },
    },
  },
}

```

My goal is to be able to do an aggregation or not ( mostly to save on computational time ), controlled by the "title\_bool" parameter.

```auto
{
  id: "test_template",
  params: {
    query_string: "teststring",
    title_bool: false,
  },
}

```

My Idea was something like this but it doesnt work and i dont understand how to make it work.

`{{#title_bool}} {terms: { field: "title.keyword", size: 10 }} {{/title_bool}}`

Help appreciated. I still think I am missing the obvious. I shouldnt be the first one to ask this question, but somehow i dont find anything online.

---

<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:** [August 18, 2022, 11:01pm UTC](https://discuss.elastic.co/t/search-template-only-aggregate-with-param-condition/310350/2 "2022-08-18T23:01:36Z")

</div>

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