Can't Add Terms Query to a Search Template

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:

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:

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:


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:

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

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.

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

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"
            }
          }
        }
      }
    }
  }

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.

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"
              }
            }
          }
        ]
      }
    }
}

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