Exact Match with Synonyms

Hi,

I have a field containing city names that I would like to get the exact city name out of while still utilizing synonyms. So If I searched for either "St Louis" or "Saint Louis" I would get back "Saint Louis" but not something like "Saint Louis Park" or "Park Saint Louis". Is this possible?

Assuming the field only contains the city name, you can use a keyword tokenizer on that field, so all the text ends up on one token. You can also define an analyzer containing synonyms for that field, so sth. like this:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "my_synonyms"
          ]
        }
      },
      "filter": {
        "my_synonyms": {
          "type": "synonym",
          "synonyms": [
            "St Louis, Saint Louis"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "city_name": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}

That won't work if you also need to do more like classic full text search on that field, but in that case you typically also want "Saint Louis Park" show up when searching for "Staint Louis". If you need both, you probably need two fields and query both of them, giving more or less weight (boost) to the exact field.

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