Search template: Only aggregate with param condition

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

{
  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.

{
  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.

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