# Exact Match with Synonyms

**URL:** <https://discuss.elastic.co/t/exact-match-with-synonyms/216735>\
**Category:** Elasticsearch\
**Created:** [January 27, 2020, 10:51pm UTC](https://discuss.elastic.co/t/exact-match-with-synonyms/216735 "2020-01-27T22:51:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![plawler92](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plawler92/32/61507_2.png) [@plawler92](https://discuss.elastic.co/u/plawler92)\
**Post date:** [January 27, 2020, 10:51pm UTC](https://discuss.elastic.co/t/exact-match-with-synonyms/216735/1 "2020-01-27T22:51:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [January 30, 2020, 9:24pm UTC](https://discuss.elastic.co/t/exact-match-with-synonyms/216735/2 "2020-01-30T21:24:45Z")

</div>

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:

```auto
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.

---

<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:** [February 27, 2020, 9:31pm UTC](https://discuss.elastic.co/t/exact-match-with-synonyms/216735/3 "2020-02-27T21:31:15Z")

</div>

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