title: Search Template in Elastic for filter and should
body: <p>I am setting up a search template to get a list of bool filters within a bool should query. I have 3 fields on es [A,B,C]. The query should return back buckets of combination of these three fields. i should be able to give [a1,b1,c1] [a2,b2,c2] ... [an,bn,cn] as params to the template</p>
  
  <p>I tried creating a template that has a should query with a {{#toJson}}clauses{{/toJson}} to create the inner term filter objects like   </p>
  
  <pre><code>"clauses":[
  {"term": {"fieldA": 30}},
  {"term": {"fieldB": 0}},
  {"term": {"fieldC": 1}}
  ]
  </code></pre>
  
  <p>I was hoping to have a section around it inside the "should" :
  <code>[{{#shouldBlock}}{{#toJson}}clauses{{/toJson}}{{/ShouldBlock}}]</code></p>
  
  <p>is there any way to have like a filter inside the should that takes the "<em>toJson</em>"</p>
  
  <p>Like </p>
  
  <pre><code>"bool":{
  "should":[
  {
            // this loops for 100 times   
            "bool":{
                    "filter":[
                            {term for fieldA},
                            {term for fieldB},  // three terms for three fields
                            {term for fieldC}
                             ] 
                   }
            // end of loop
  }
  ]
  </code></pre>
  
  <p>This is my query. and this is expected as a response for the render</p>
  
  <pre><code>GET adn/_search
  {
    "size": 10,
    "query": {
         "bool": {
           "minimum_should_match": 1, 
           "should": [
             {
               "bool": {
                 "filter": [
                   {"term": {"fieldA": 30}},
                   {"term": {"fieldB": 0}},
                   {"term": {"fieldC": 1}}
                 ]
             }
            },
              {
               "bool": {
                 "filter": [
                   {"term": {"fieldA": 0}},
                   {"term": {"fieldB": 1}},
                   {"term": {"fieldC": 0}}
                 ]
             }
            }
           ]
         }
       }
  }
  </code></pre>
  
  <p>My first try was to add the inside condition for should as text inside a section </p>
  
  <pre><code>GET _render/template
  {
      "source": "{\"query\": {\"bool\": {\"should\":[ {{#section}} {\"bool\":{\"filter\": {{#toJson}}clauses{{/toJson}} } } {{/section}} }}}",
      "params": {
        "section":{
          "shouldBlock":[]
        },
        "clauses": [
              { "term": { "breakId" : 1 } },
              { "term": { "offset" : 0 } }
          ]
  
      }
  }
  </code></pre>
  
  <p>that gave an error</p>
  
  <pre><code>{
    "error": {
      "root_cause": [
        {
          "type": "json_parse_exception",
          "reason": "Unexpected close marker '}': expected ']' (for Array starting at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 30])\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 102]"
        }
      ],
      "type": "json_parse_exception",
      "reason": "Unexpected close marker '}': expected ']' (for Array starting at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 30])\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 102]"
    },
    "status": 500
  }
  </code></pre>
  
  <p>I tried some thing like a section with a param to hold</p>
  
  <pre><code>GET _render/template
  {
      "source": "{\"query\": {\"bool\": {\"should\":[ {{#section}} {{shouldBlock}} {{#toJson}}clauses{{/toJson}} }] {{/section}} } } }",
      "params": {
        "clauses": [
              { "term": { "fieldA" : 1 } },
              { "term": { "fieldB" : 0 } }
          ]
  
      }
  }
  </code></pre>
  
  <p>Things I had to keep in mind.
  section that loops : 100 times </p>
  
  <pre><code>"bool":{
         "filter":[
                   {"term": {"fieldA": a1}},
                   {"term": {"fieldB": b1}},
                   {"term": {"fieldC": c1}},
                  ]
  }
  </code></pre>
  
  <p>Each one object of the above 100 has a a different filter block with combination of 3fields with different values for those 3 fields [fieldA,fieldB,fieldC]</p>
  
  <p>I read that the array for "filter"  can be achieved by the 'toJson' function of elastic search but the outer part of it is difficult for me to decipher</p>
  
  <p>It would be great if some one could help me out</p>