# Synonyms not being used in search results

**URL:** https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920
**Category:** Elasticsearch
**Created:** [January 25, 2023, 12:27pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920 "2023-01-25T12:27:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![appsol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/appsol/32/116381_2.png) [@appsol](https://discuss.elastic.co/u/appsol)
#### Post date: [January 25, 2023, 12:27pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/1 "2023-01-25T12:27:36Z")

</div>

Hello, I have an index for services named 'testing\_services':

```auto
'properties' => [
            'id' => ['type' => 'keyword'],
            'name' => [
                'type' => 'text',
                'analyzer' => 'english_analyser',
                'search_analyzer' => 'english_analyser',
                'fields' => [
                    'keyword' => ['type' => 'keyword'],
                ],
            ],
            'intro' => [
                'type' => 'text',
                'analyzer' => 'english_analyser',
                'search_analyzer' => 'english_analyser',
            ],
            'description' => [
                'type' => 'text',
                'analyzer' => 'english_analyser',
                'search_analyzer' => 'english_analyser',
            ],
            'wait_time' => ['type' => 'keyword'],
            'is_free' => ['type' => 'boolean'],
            'status' => ['type' => 'keyword'],
            'score' => ['type' => 'integer'],
            'organisation_name' => [
                'type' => 'text',
                'analyzer' => 'english_analyser',
                'search_analyzer' => 'english_analyser',
                'fields' => [
                    'keyword' => ['type' => 'keyword'],
                ],
            ],
            'taxonomy_categories' => [
                'type' => 'text',
                'analyzer' => 'english_analyser',
                'search_analyzer' => 'english_analyser',
                'fields' => [
                    'keyword' => ['type' => 'keyword'],
                ],
            ],
            'collection_categories' => ['type' => 'keyword'],
            'collection_personas' => ['type' => 'keyword'],
            'service_locations' => [
                'type' => 'nested',
                'properties' => [
                    'id' => ['type' => 'keyword'],
                    'location' => ['type' => 'geo_point'],
                ],
            ],
            'service_eligibilities' => [
                'type' => 'text',
                'analyzer' => 'english_analyser',
                'search_analyzer' => 'english_analyser',
                'fields' => [
                    'keyword' => ['type' => 'keyword'],
                ],
            ],
        ],

```

The settings for the index are:

```auto
'analysis' => [
                'analyzer' => [
                    'english_analyser' => [
                        'type' => 'custom',
                        'tokenizer' => 'standard',
                        'filter' => [
                            'lowercase',
                            'synonym',
                            'english_stop',
                            'stopwords'
                        ],
                    ],
                ],
                'filter' => [
                    'synonym' => [
                        'type' => 'synonym',
                        'synonyms' => $this->getThesaurus(),
                    ],
                    'english_stop' => [
                        'type' => 'stop',
                        'stopwords' => '_english_'
                    ],
                    'stopwords' => [
                        'type' => 'stop',
                        'stopwords' => $this->getStopWords(),
                    ],
                ],
            ],

```

The `getThesaurus()` and `getStopwords()` methods return an array of synonyms and stop words respectively.  
The test synonyms are:

```auto
autism,autistic,asd
not drinking,dehydration,
dehydration,thirsty,drought

```

A call to:

```auto
http://elasticsearch:9200/testing_services/_analyze
            [
                'field' => 'name',
                'text' => 'Helping asd'
            ]

```

Shows the synonyms are being used:

```auto
"tokens" => array:4 [
    0 => array:5 [
      "token" => "helping"
      "start_offset" => 0
      "end_offset" => 7
      "type" => "<ALPHANUM>"
      "position" => 0
    ]
    1 => array:5 [
      "token" => "asd"
      "start_offset" => 8
      "end_offset" => 11
      "type" => "<ALPHANUM>"
      "position" => 1
    ]
    2 => array:5 [
      "token" => "autism"
      "start_offset" => 8
      "end_offset" => 11
      "type" => "SYNONYM"
      "position" => 1
    ]
    3 => array:5 [
      "token" => "autistic"
      "start_offset" => 8
      "end_offset" => 11
      "type" => "SYNONYM"
      "position" => 1
    ]
  ]

```

If I create a service with a name 'Helping asd' and search with the query 'autism':

```auto
"body" => array:3 [
    "query" => array:1 [
      "function_score" => array:2 [
        "query" => array:1 [
          "bool" => array:4 [
            "must" => []
            "should" => array:5 [
              0 => array:1 [
                "match" => array:1 [
                  "name" => array:3 [
                    "query" => "autism"
                    "boost" => 3
                    "fuzziness" => "AUTO"
                  ]
                ]
              ]
              1 => array:1 [
                "match" => array:1 [
                  "organisation_name" => array:3 [
                    "query" => "autism"
                    "boost" => 3
                    "fuzziness" => "AUTO"
                  ]
                ]
              ]
              2 => array:1 [
                "match" => array:1 [
                  "intro" => array:3 [
                    "query" => "autism"
                    "boost" => 2
                    "fuzziness" => "AUTO"
                  ]
                ]
              ]
              3 => array:1 [
                "match" => array:1 [
                  "description" => array:3 [
                    "query" => "autism"
                    "boost" => 1.5
                    "fuzziness" => "AUTO"
                  ]
                ]
              ]
              4 => array:1 [
                "match" => array:1 [
                  "taxonomy_categories" => array:3 [
                    "query" => "autism"
                    "boost" => 1
                    "fuzziness" => "AUTO"
                  ]
                ]
              ]
            ]
            "filter" => array:1 [
              0 => array:1 [
                "term" => array:1 [
                  "status" => "active"
                ]
              ]
            ]
            "minimum_should_match" => 1
          ]
        ]
        "functions" => array:1 [
          0 => array:1 [
            "field_value_factor" => array:3 [
              "field" => "score"
              "missing" => 1
              "modifier" => "ln1p"
            ]
          ]
        ]
      ]
    ]
    "from" => 0
    "size" => 25
  ]

```

I get no results, yet a query for the term 'asd' returns the correct result.

It looks as thought the index is created correctly and the service data is mapped to the fields. The index analyzer is in place and assigned to the fields. A test of the analyzer shows it is using the supplied synonyms, but when I query the index with a synonym I get no results.  
Could you give me some insights into where I should look to resolve this?  
Thanks

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [January 25, 2023, 1:37pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/2 "2023-01-25T13:37:47Z")

</div>

Hi @appsol

Could you provide a sample doc that you expect when search by term "autism"

---

<div class="post-metadata">

### Author: ![appsol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/appsol/32/116381_2.png) [@appsol](https://discuss.elastic.co/u/appsol)
#### Post date: [January 25, 2023, 1:50pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/3 "2023-01-25T13:50:34Z")

</div>

Hello @RabBit_BR,  
thanks for looking. Sure the doc I would expect to be returned is:

```auto
"id" => "d59ddde4-4f3a-40e0-8a36-392d2198acb1"
  "name" => "Helping asd"
  "intro" => "Id et est ut rerum id vel cupiditate"
  "description" => "Ipsam ea et qui voluptatem quia excepturi"
  "wait_time" => null
  "is_free" => true
  "status" => "active"
  "score" => 1
  "organisation_name" => "Stevens Ltd dolorum 37063"
  "taxonomy_categories" => []
  "collection_categories" => []
  "collection_personas" => []
  "service_locations" => []
  "service_eligibilities" => array:7 [
    0 => "Age Group All"
    1 => "Disability All"
    2 => "Gender All"
    3 => "Income All"
    4 => "Language All"
    5 => "Ethnicity All"
    6 => "Housing All"
  ]

```

A search for 'asd' will return this doc, but a search for one of the synonyms, e.g. 'autism' does not.

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [January 25, 2023, 3:37pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/4 "2023-01-25T15:37:31Z")

</div>

Do you have access to devTools?  
I did a test with the given data and got the document when the search term is "autism".  
Maybe I forgot some configuration and that's why I can't reproduce your error.

Mapping

```auto
PUT test/
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "synonym",
              "english_stop"
            ]
          }
        },
        "filter": {
          "english_stop": {
            "type": "stop",
            "stopwords": [
              "_english_"
            ]
          },
          "synonym": {
            "type": "synonym",
            "synonyms": [
              "autism,autistic,asd",
              "not drinking,dehydration",
              "dehydration,thirsty,drought"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "my_analyzer",
        "search_analyzer": "my_analyzer"
      }
    }
  }
}

```

Document

```auto
POST test/_doc
{
  "id": "d59ddde4-4f3a-40e0-8a36-392d2198acb1",
  "name": "Helping asd",
  "intro": "Id et est ut rerum id vel cupiditate",
  "description": "Ipsam ea et qui voluptatem quia excepturi",
  "is_free": true,
  "status": "active",
  "score": 1
}

```

Query

```auto
GET test/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "status": "active"
              }
            }
          ],
          "minimum_should_match": 1, 
          "should": [
            {
              "match": {
                "name": {
                  "query": "autism",
                  "fuzziness": "AUTO"
                }
              }
            },
            {
              "match": {
                "organisation_name": {
                  "query": "autism",
                  "fuzziness": "AUTO"
                }
              }
            },
            {
              "match": {
                "intro": {
                  "query": "autism",
                  "fuzziness": "AUTO"
                }
              }
            },
            {
              "match": {
                "description": {
                  "query": "autism",
                  "fuzziness": "AUTO"
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "score",
            "missing": 1,
            "modifier": "none"
          }
        }
      ]
    }
  }
}

```

Response

```auto
"hits": [
      {
        "_index": "test",
        "_id": "zWVW6YUBQum6OxY14_ER",
        "_score": 0.32088596,
        "_source": {
          "id": "d59ddde4-4f3a-40e0-8a36-392d2198acb1",
          "name": "Helping asd",
          "intro": "Id et est ut rerum id vel cupiditate",
          "description": "Ipsam ea et qui voluptatem quia excepturi",
          "is_free": true,
          "status": "active",
          "score": 1
        }
      }
    ]

```

---

<div class="post-metadata">

### Author: ![appsol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/appsol/32/116381_2.png) [@appsol](https://discuss.elastic.co/u/appsol)
#### Post date: [January 25, 2023, 4:41pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/5 "2023-01-25T16:41:31Z")

</div>

@RabBit_BR Thank you so much! The fact that you could use my settings and successfully return the doc meant that I knew it wasn't a settings issue. So i dug into the implementation of the Elasticsearch, particularly how the models are exposed on the index and discovered that the model was not registering itself on the correct index when running tests. There was an index prefix for tests (i.e. 'testing\_services' rather than 'services') which was not being applied when the model was registering which index to add it to.  
Mea culpa!  
Thanks again.

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [January 25, 2023, 4:46pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/6 "2023-01-25T16:46:55Z")

</div>

Glad you got it right.

---

<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 22, 2023, 4:47pm UTC](https://discuss.elastic.co/t/synonyms-not-being-used-in-search-results/323920/7 "2023-02-22T16:47:16Z")

</div>

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