# Why doesn't this Synonym work?

**URL:** <https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576>\
**Category:** Elasticsearch\
**Created:** [October 21, 2016, 7:19am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576 "2016-10-21T07:19:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 7:19am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/1 "2016-10-21T07:19:49Z")

</div>

Ok, so I created my index with the following:

```
PUT product
{
  "settings": {
    "analysis": {
      "char_filter": {
        "&_to_and": {
          "type": "mapping",
          "mappings": [
            "& => and"
          ]
        }
      },
      "filter": {
        "productSynonyms":{
          "type": "synonym",
          "synonyms" : [
            "kfc,kentucky fried chicken",
            "dairy,milk",
            "dairy,cheese",
            "dairy,cream",
            "dairy,butter"
            ]
        }
      }, 
      "analyzer": {
        "productsAnalyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "stop",
            "snowball"
          ]
        }
      }
    }
  }
}

```

However, issuing the following search brings no results.

```auto
GET product/_search
{
  "query": {
    "match": {
      "brandName": "kfc"
    }
  }
}

```

But, the following does:

```auto
GET product/_search
{
  "query": {
    "match": {
      "brandName": "kentucky fried chicken"
    }
  }
}

```

Any ideas why this might be?  
TIA.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 21, 2016, 7:31am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/2 "2016-10-21T07:31:58Z")

</div>

What is your mapping?

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 7:40am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/3 "2016-10-21T07:40:58Z")

</div>

Hi David,

I had let elastic take care of the mapping automatically when indexing the documents. Brandname is mapped to a string.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 21, 2016, 7:41am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/4 "2016-10-21T07:41:46Z")

</div>

So it uses the `standard` analyzer. Not yours I guess.

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 7:42am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/5 "2016-10-21T07:42:57Z")

</div>

Ah, yes I suppose that's right. So I'd need to specify the mapping to use my analyzer at index creation time??

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [October 21, 2016, 7:43am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/6 "2016-10-21T07:43:16Z")

</div>

And you didn't set `productSynonyms` to `productsAnalyzer`.

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 7:47am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/7 "2016-10-21T07:47:17Z")

</div>

Ok, thanks for that Jun. As you might have guessed, I'm new to Elastic!

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 8:04am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/8 "2016-10-21T08:04:28Z")

</div>

Ok, so I've dropped and created the index (this is only on Test so I can do that!) with the mapping set to use the correct analyzer and the productSynonyms using productsAnalyzer.

However, issuing a search for kfc in the brandName brings back results where the word Chicken appears, not the entire phrase Kentucky Fried Chicken, if that makes sense?

How do I configure the analyzer/synonyms/mappings to work in this way?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 21, 2016, 8:09am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/9 "2016-10-21T08:09:08Z")

</div>

Ideally send a full recreation script [as explained here](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) so we can play with it and help you fixing it.

And please format your code using `</>` icon. It will make your post more readable.  
I updated your original post.

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 8:23am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/10 "2016-10-21T08:23:25Z")

</div>

Ok, here's my script. Thanks for your help.

```
DELETE productstest
PUT productstest
{
  "settings": {
    "analysis": {
      "char_filter": {
        "&_to_and": {
          "type": "mapping",
          "mappings": [
            "& => and"
          ]
        }
      },
      "filter": {
        "productSynonyms":{
          "type": "synonym",
          "synonyms" : [
            "kfc,kentucky fried chicken"
            ]
        }
      }, 
      "analyzer": {
        "productsAnalyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "stop",
            "snowball",
            "productSynonyms"
          ]
        }
      }
    }
  }
}

PUT /productstest/_mapping/*?update_all_types
{
  "properties":{
    "brandName":{
      "type":"string",
      "analyzer":"productsAnalyzer"
    }
  }
}

POST productstest/product
{
  "brandName": "Kentucky Fried Chicken",
  "productFullName": "Zinger Tower Burger"
}

POST productstest/product
{
  "brandName": "Chicken Tonight",
  "productFullName":"Sweet and Sour Sauce"
}

POST productstest/product
{
  "brandName": "Fried dot com",
  "productFullName":"Refried beans from Mexico"
}

GET productstest/_search
{
  "query": {
    "match": {
      "brandName": "kfc"
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [October 21, 2016, 9:20am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/11 "2016-10-21T09:20:35Z")

</div>

In you case, you should move `productSynonyms` filter before `stop` filter in you analyzer setting.

```auto
...
"filter": [
  "lowercase",
  "productSynonyms",
  "stop", 
  "snowball"
...

```

"Kentucky Fried Chicken" isn't expanded to "kfc" because `snowball` synonym filter change it to "kentucki fri chicken".

You can see how to work the analysis chain [using `analyze` API.](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/analysis-intro.html#analyze-api)  
example :

```auto

POST /productstest/_analyze
{
  "text": "Kentucky Fried Chicken",
  "field": "brandName"
}

```

See docs : [Synonyms and analysis chain](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/synonyms-analysis-chain.html)

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 9:33am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/12 "2016-10-21T09:33:34Z")

</div>

Thanks for your help on this Jun. I've moved the filter up the chain, but still get the Chicken Tonight product back when searching for kfc - is there any way to only return the Kentucky Fried Chicken product when searching for kfc?

I will read up on this via the link you sent. Again, thanks for your input.

---

<div class="post-metadata">

**Author:** ![LeeSalter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leesalter/32/12635_2.png) [@LeeSalter](https://discuss.elastic.co/u/LeeSalter)\
**Post date:** [October 21, 2016, 11:22am UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/13 "2016-10-21T11:22:27Z")

</div>

Figured it out!!  
I needed to use a uni-directional synonym like this:

```
PUT productstest
{
  "settings": {
    "analysis": {
      "char_filter": {
        "&_to_and": {
          "type": "mapping",
          "mappings": [
            "& => and"
          ]
        }
      },
      "filter": {
        "productSynonyms": {
          "type": "synonym",
          "synonyms": [
            "kentucky fried chicken=>kfc"
          ]
        }
      },
      "analyzer": {
        "productsAnalyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "productSynonyms",
            "stop"
          ]
        }
      }
    }
  }
}

```

Now, the query:

```
GET productstest/_search
{
  "query": {
    "match": {
      "brandName": "kfc"
    }
  }
}

```

returns only the Kentucky Fried Chicken result.

---

<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:** [July 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/why-doesnt-this-synonym-work/63576/14 "2017-07-05T22:10:35Z")

</div>


