# How to do index-time synonyms

**URL:** <https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405>\
**Category:** Elasticsearch\
**Created:** [June 19, 2018, 4:15am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405 "2018-06-19T04:15:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chung/32/49315_2.png) [@chung](https://discuss.elastic.co/u/chung)\
**Post date:** [June 19, 2018, 4:15am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/1 "2018-06-19T04:15:02Z")

</div>

I am trying to make a synonym for a category so that `voucher` and `gift card` mean the same thing in index time. So when users type in `voucher` or `gift card`, `elasticsearch` returns the same results.

For some reasons, the settings and mappings I am using don't work at all.

What did I miss in the configs?

settings

```auto
{
  "number_of_replicas": 0,
  "analysis": {
    "filter": {
      "category_synonym": {
        "type": "synonym",
        "tokenizer": "keyword",
        "synonyms": [
          "voucher, gift card"
        ]
      }
    },
    "analyzer": {
      "keyword_analyzer": {
        "type": "custom",
        "tokenizer": "keyword",
        "filter": [
          "lowercase",
          "category_synonym"
        ]
      }
    }
  }
}

```

mapping

```auto
{
  "properties": {
    "category": {
      "type": "keyword",
      "analyzer": "keyword_analyzer"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 19, 2018, 5:10am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/2 "2018-06-19T05:10:19Z")

</div>

What is the result of :

```
GET /index/_analyze
{
  "analyzer" : "keyword_analyzer",
  "text" : "voucher"
}
```

---

<div class="post-metadata">

**Author:** ![chung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chung/32/49315_2.png) [@chung](https://discuss.elastic.co/u/chung)\
**Post date:** [June 19, 2018, 6:10am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/3 "2018-06-19T06:10:50Z")

</div>

I got

```auto
{
  "tokens": [
    {
      "token": "voucher",
      "start_offset": 0,
      "end_offset": 7,
      "type": "word",
      "position": 0
    },
    {
      "token": "gift card",
      "start_offset": 0,
      "end_offset": 7,
      "type": "SYNONYM",
      "position": 0
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 19, 2018, 7:00am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/4 "2018-06-19T07:00:00Z")

</div>

As you can see, if your document contains "voucher", it will be indexed with "voucher" and "gift card", as you defined in your mapping, so for me it's all good.

---

<div class="post-metadata">

**Author:** ![chung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chung/32/49315_2.png) [@chung](https://discuss.elastic.co/u/chung)\
**Post date:** [June 20, 2018, 2:11am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/5 "2018-06-20T02:11:21Z")

</div>

I am not sure why it didn't work. Do I have to add something special to the query?

```auto
GET myindex/_search 
{
    "query": {
        "bool" : {
            "must" : {
                "query_string" : {
                    "query" : "gift card"
                }
            }
        }
    }
}
returned correct results

```

```auto
GET myindex/_search
{
    "query": {
        "bool" : {
            "must" : {
                "query_string" : {
                    "query" : "voucher" # or Voucher
                }
            }
        }
    }
}
returned nothing

```

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 20, 2018, 5:51am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/6 "2018-06-20T05:51:12Z")

</div>

No the query looks fine, so you need to debug this, and try to understand the reason why the second query doesnt return anything.

Add the `explain` parameter to understand how the score is calculated, and search only on the `category` field to be sure, the results of the first query are coming from this field.

```
POST index/_search
{
  "explain": true,
  "_source": [
    "category"
  ],
  "query": {
    "query_string": {
      "fields": [
        "category"
      ],
      "query": "gift card"
    }
  }
}

POST index/_search
{
  "explain": true,
  "_source": [
    "category"
  ],
  "query": {
    "query_string": {
      "fields": [
        "category"
      ],
      "query": "voucher"
    }
  }
}
```

---

<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 18, 2018, 5:51am UTC](https://discuss.elastic.co/t/how-to-do-index-time-synonyms/136405/7 "2018-07-18T05:51:15Z")

</div>

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