# Search templates with nested query

**URL:** <https://discuss.elastic.co/t/search-templates-with-nested-query/330745>\
**Category:** Elasticsearch\
**Created:** [April 25, 2023, 1:30pm UTC](https://discuss.elastic.co/t/search-templates-with-nested-query/330745 "2023-04-25T13:30:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [April 25, 2023, 1:30pm UTC](https://discuss.elastic.co/t/search-templates-with-nested-query/330745/1 "2023-04-25T13:30:57Z")

</div>

Hi everyone.

I'm trying to make a search template with bool query. This bool query uses 'should' operator, which searches data throgh 4 fields of index. These results than must be filtered by two fields, so I try to use 'must' operator outside the 'should' operator. As a result, I have docs that suites both conditions of 'should' and 'must', but after a sum of these conditions I have other docs which suites only the 'must' operator conditions. I use the following script:

```auto
PUT _scripts/search_template
{
"script": {
"lang": "mustache",
"source": {
"query": {
"bool": {
"must": [
{"match": {"field_1": "true"}},
{"match": {"field_2": "true"}}
],
"should": [
{
"match": {
"field_3": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 1
}
}
},
{
"match": {
"field_4": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 2
}
}
},
{
"match": {
"field_5": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 3
}
}
},
{
"match": {
"field_6": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 1
}
}
}
]
}
},
"from": "{{from}}{{^from}}0{{/from}}",
"size": "{{size}}{{^size}}10{{/size}}"
},
"params": {
"query_string": "Query string"
}
}
}

```

---

<div class="post-metadata">

**Author:** ![canelia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canelia/32/141857_2.png) [@canelia](https://discuss.elastic.co/u/canelia)\
**Post date:** [April 26, 2023, 7:52am UTC](https://discuss.elastic.co/t/search-templates-with-nested-query/330745/2 "2023-04-26T07:52:50Z")

</div>

```auto
PUT _scripts/search_template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "field_1": "true"
              }
            },
            {
              "match": {
				"field_2": "true"
              }
            },
			{
				"bool": {
					"should": [
					{
					  "match": {
						"field_3": {
						  "query": "{{query_string}}",
						  "fuzziness": "1",
						  "boost": 1
						}
					  }
					},
					{
					  "match": {
						"field_4": {
						  "query": "{{query_string}}",
						  "fuzziness": "1",
						  "boost": 2
						}
					  }
					},
					{
					  "match": {
						"field_5": {
						  "query": "{{query_string}}",
						  "fuzziness": "1",
						  "boost": 3
						}
					  }
					},
					{
					  "match": {
						"field_6": {
						  "query": "{{query_string}}",
						  "fuzziness": "1",
						  "boost": 1
						}
					  }
					}
				  ]
				}				
			}
          ]          
        }
      },
      "from": "{{from}}{{^from}}0{{/from}}",
      "size": "{{size}}{{^size}}10{{/size}}"
    },
    "params": {
      "query_string": "Query string"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [April 26, 2023, 11:25am UTC](https://discuss.elastic.co/t/search-templates-with-nested-query/330745/3 "2023-04-26T11:25:41Z")

</div>

Great thanks to you, @canelia! Now it works correctly

---

<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:** [May 24, 2023, 11:26am UTC](https://discuss.elastic.co/t/search-templates-with-nested-query/330745/4 "2023-05-24T11:26:13Z")

</div>

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