# Requesting help with synonym's combined with a query string

**URL:** <https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066>\
**Category:** Elasticsearch\
**Created:** [August 11, 2021, 12:52pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066 "2021-08-11T12:52:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BramFr](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@BramFr](https://discuss.elastic.co/u/BramFr)\
**Post date:** [August 11, 2021, 12:52pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066/1 "2021-08-11T12:52:13Z")

</div>

Hi everybody,

Just stepping in to the world of ES so I'm sorry if I am a bit of a newbie.

Currently I'm stuck at a point in ES and I can't seem to find an awser online, perhaps one of you could be of assistance.

What I'm trying is the following, whilst using a synonym list I would like to use a query string.

As an example:

Input = 'corporation' + 'garage'  
Synonym = corporation \> company  
Result = 'The garage company'

At this time I've got the synonym list working using the code below.

```auto
PUT company
{
  "settings": {
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym",
          "synonyms_path" : "analysis/synonyms.txt",
          "updateable": true
        }
      },
      "analyzer": {
        "my_synonyms": {
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "my_synonym_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text_field": {
        "type": "text",
        "analyzer": "standard",
        "search_analyzer": "my_synonyms"
      }
    }
  }
}

```

But when I try to use the following code in a query I dont get any results.

```auto
GET company/_search
{

    "query" : {
      "query_string" : {
          "query" : "(corporation) AND (garage)",
          "default_field": "COMBINED_FIELD",
          "fuzziness": "AUTO",
          "analyzer": "my_synonyms"
        }
    }
}

```

Can any of you please help me?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 1:14pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066/2 "2021-08-11T13:14:01Z")

</div>

Hi Bram,  
Can you supply a full recreation script with mapping, synonyms, and example doc?  
I don't see the "COMBINED\_FIELD" mentioned in your query in your mapping, for instance.

This script is working for me:

```auto
DELETE company
PUT company
{
  "settings": {
    "analysis": {
      "filter": {
          "my_synonym_filter": {
            "type": "synonym",
            "lenient": true,
            "synonyms": ["corporation, company => company"]
          }
      },
      "analyzer": {
        "my_synonyms": {
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "my_synonym_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text_field": {
        "type": "text",
        "analyzer": "standard",
        "search_analyzer": "my_synonyms"
      }
    }
  }
}
POST company/_doc/1
{
  "text_field":"company garage"
}
GET company/_search
{

    "query" : {
      "query_string" : {
          "query" : "corporation AND garage",
          "default_field": "text_field",
          "fuzziness": "AUTO",
          "analyzer": "my_synonyms"
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![BramFr](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@BramFr](https://discuss.elastic.co/u/BramFr)\
**Post date:** [August 11, 2021, 1:25pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066/3 "2021-08-11T13:25:45Z")

</div>

Thanks for the quick reply Mark.

Am I right in thinking you are requesting the mappings that are provided when executing

```auto
GET company/

```

In that case I get the following response.

```auto
#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security.
{
  "company" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "COMBINED_FIELD" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "null" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "text_field" : {
          "type" : "text",
          "analyzer" : "standard",
          "search_analyzer" : "my_synonyms"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "company",
        "creation_date" : "1628684658444",
        "analysis" : {
          "filter" : {
            "my_synonym_filter" : {
              "type" : "synonym",
              "synonyms_path" : "analysis/synonyms.txt",
              "updateable" : "true"
            }
          },
          "analyzer" : {
            "my_synonyms" : {
              "filter" : [
                "lowercase",
                "my_synonym_filter"
              ],
              "tokenizer" : "keyword"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "Nz4PJn5xSJ-Jq0hNso5FMQ",
        "version" : {
          "created" : "7130299"
        }
      }
    }
  }
}

```

At this time I'm unable to provide the data in the COMBINED\_FIELD as it contains some sensitive information that I'm not permitted to share.

The synonyms file currently consists of the follow words:

```auto
"Company","Business firm","Organisation","Corporation"

```

The input requests and output for the "COMBINED\_FIELD" can contain any of the four synonyms mentioned above.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 1:42pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066/4 "2021-08-11T13:42:49Z")

</div>

In the spirit of the old "teach a man to fish..." mantra, check out the "analyze" api to see what your choice of analyzer does to document and query text.

```auto
GET company/_analyze
{
  "analyzer": "my_synonyms",
  "text": ["company garage"]
}

```

What it reveals may make you want to change your mapping and the `tokenizer` used for the synonym filter. It was keyword but it looks like you might want `standard`:

```auto
        "my_synonyms": {
          "tokenizer": "standard"

```

Again, experiment with the analyze API.  
When it comes to debugging how the query is processed the `explain` API is your friend - give it the index and doc ID of a doc you expect to match and it will tell you why the query does or does not match that doc:

```auto
GET company/_doc/1/_explain
{

    "query" : {
      "query_string" : {
          "query" : "corporation AND garage",
          "default_field": "text_field",
          "fuzziness": "AUTO",
          "analyzer": "my_synonyms"
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![BramFr](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@BramFr](https://discuss.elastic.co/u/BramFr)\
**Post date:** [August 11, 2021, 1:57pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066/5 "2021-08-11T13:57:19Z")

</div>

Thank you!

With your suggestion of using a different tokenizer along with the /\_analyze suggestion I was able to figure it out.

---

<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:** [September 8, 2021, 2:09pm UTC](https://discuss.elastic.co/t/requesting-help-with-synonyms-combined-with-a-query-string/281066/7 "2021-09-08T14:09:18Z")

</div>

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