# Can't Add Terms Query to a Search Template

**URL:** <https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038>\
**Category:** Elasticsearch\
**Created:** [February 8, 2023, 2:58pm UTC](https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038 "2023-02-08T14:58:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [February 8, 2023, 2:58pm UTC](https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038/1 "2023-02-08T14:58:23Z")

</div>

I have started work on a search template and hit a snag when trying to add a terms query to the template. Here is the template definition:

```auto
POST _scripts/establishments_search
{
  "script": {
    "lang": "mustache",
    "source": {
      "size": "{{size}}{{^size}}3{{/size}}",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "cityID": {
                  "query": "{{cityID}}{{^cityID}}*{{/cityID}}",
                  "_name": "CityID"
                }
              }
            },
            {
              "match": {
                "countryAffiliation": {
                  "query": "{{countryAffiliation}}{{^countryAffiliation}}*{{/countryAffiliation}}",
                  "_name": "CountryAffiliation"
                }
              }
            },
            {
              "terms": {
                "_name": "EstablishmentAliases",
                "establishmentAliases": "{{establishmentAliases}}{{^establishmentAliases}}*{{/establishmentAliases}}"
              }
            }
          ],
          "should": [
            {
              "match": {
                "overtFunction.keyword": {
                  "query": "{{overtFunction}}{{^overtFunction}}*{{/overtFunction}}",
                  "_name": "OvertFunction",
                  "boost": "{{overtFunctionBoost}}{{^overtFunctionBoost}}1{{/overtFunctionBoost}}"
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

Here is the sample record I am testing against:

```auto
PUT establishments/_doc/1
{
 "establishmentAliases": ["acme", "acme inc", "acme incorporated"],
 "cityID": 1234,
 "countryAffiliation": "US",
 "overtFunction": "Sales",
 "requiredMatches": 1,
 "Addresses": [
    {
      "Address": "1917 Blair Ave",
      "City": "Hanover",
      "State": "MD"
    },
    {
      "Address": "111 East Bell Ave",
      "City": "Johnstown",
      "State": "PA"
    }
  ]
}

```

The problem with the template is the establishmentAliases field (which is defined as a keyword field in my mapping) but I can run this query directly against the index and get the expected result:

```auto

GET establishments/_search
{
  "query": {
    "nested": {
      "path": "Addresses",
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "Addresses.Address": "1917"
              }
            },
            {
              "match": {
                "Addresses.City": "Hanover"
              }
            },
            {
              "match": {
                "Addresses.State": "MD"
              }
            },
            {
              "terms": {
                "establishmentAliases": [
                  "Acme",
                  "Acme inc."
                ]
              }
            }
          ]
        }
      }
    }
  }
}

```

However, this template search generates the error below:

```auto
GET establishments/_search/template
{
  "id": "establishments_search",
  "params": {
    "cityID": 1234,
    "establishmentAliases": ["acme", "acme inc"],
    "countryAffiliation": "US",
    "size": 3,
    "overtFunction": "Sales",
    "overtFunctionBoost": 1
  }
}

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[terms] query does not support [establishmentAliases]",
        "line": 1,
        "col": 234
      }
    ],
    "type": "x_content_parse_exception",
    "reason": "[1:234] [bool] failed to parse field [must]",
    "caused_by": {
      "type": "parsing_exception",
      "reason": "[terms] query does not support [establishmentAliases]",
      "line": 1,
      "col": 234
    }
  },
  "status": 400
}

```

Anyone have any ideas on how to wire in a terms query into a search template? They way I have it wired in does not look right but I cannot figure out the correct format. Lastly, if I remove the establishmentAliases terms piece from the search template the template works as expected. Any help that anyone could provide would be appreciated. Thanks.

Kevin

---

<div class="post-metadata">

**Author:** ![Matthew\_Isett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_isett/32/57770_2.png) [@Matthew\_Isett](https://discuss.elastic.co/u/Matthew_Isett)\
**Post date:** [February 8, 2023, 7:42pm UTC](https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038/2 "2023-02-08T19:42:30Z")

</div>

> [@krmathieu](#):
>
> `"establishmentAliases": ["acme", "acme inc", "acme incorporated"],`

I believe the field eA (establishmentAliases) is NOT nested within the Addresses path.  
So the nested path parameter calls out the Addresses object, which contains Addresses.City and Addresses.State. Whereas eA is held within the top level of the document.

Did you mean to place eA outside the nested Addresses object?

If so, you will need to move the eA terms query to another bool query section.

Let me know if that makes sense.

> **[Nested query | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)**

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [February 8, 2023, 7:59pm UTC](https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038/3 "2023-02-08T19:59:02Z")

</div>

Thanks for the response, you are correct, my search example is incorrect and the reason it is returning the result anyways is because the of the should query. However, the real issue is executing the search template, the terms piece of it causes the "[terms] query does not support [establishmentAliases]" error. Basically, I want to be able to pass in one or more establishmentAliases as an array to essentially do set-based searching to find the most relevant document. I believe the issue is they way I am defining the terms query in the search template. My mapping is below for reference. Any additional help that you might be able to provide would be appreciated. Thanks.

Kevin

```auto
PUT establishments
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
        "Addresses" : {
          "type": "nested", 
          "properties" : {
            "Address" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "City" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "State" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "Country": {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "countryAffiliation": {
          "type": "text"
        },
        "establishmentAliases": {
          "type": "keyword",
          "normalizer": "my_normalizer"
        },
        "overtFunction": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256,
              "normalizer": "my_normalizer"
            }
          }
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Matthew\_Isett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_isett/32/57770_2.png) [@Matthew\_Isett](https://discuss.elastic.co/u/Matthew_Isett)\
**Post date:** [February 8, 2023, 8:37pm UTC](https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038/4 "2023-02-08T20:37:31Z")

</div>

So this is the rendered search template - and it causes the same error.

I haven't had time to dig into it - but it doesn't create the same search as you have shown above.

I think you should create a simpler search template just on terms query on eA field.

```auto
POST _render/template
{
  "id": "establishments_search",
  "params": {
    "cityID": 1234,
    "establishmentAliases": ["acme", "acme inc"],
    "countryAffiliation": "US",
    "size": 3,
    "overtFunction": "Sales",
    "overtFunctionBoost": 1
  }
}

GET establishments/_search
{
  "query": {
      "bool": {
        "must": [
          {
            "match": {
              "cityID": {
                "query": "1234",
                "_name": "CityID"
              }
            }
          },
          {
            "match": {
              "countryAffiliation": {
                "query": "US",
                "_name": "CountryAffiliation"
              }
            }
          },
          {
            "terms": {
              "_name": "EstablishmentAliases",
              "establishmentAliases": "{0=acme, 1=acme inc}"
            }
          }
        ],
        "should": [
          {
            "match": {
              "overtFunction.keyword": {
                "query": "Sales",
                "_name": "OvertFunction",
                "boost": "1"
              }
            }
          }
        ]
      }
    }
}

```

---

<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:** [March 8, 2023, 8:37pm UTC](https://discuss.elastic.co/t/cant-add-terms-query-to-a-search-template/325038/5 "2023-03-08T20:37:46Z")

</div>

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