# It is possible to set phrase queries on certain multi term synonyms?

**URL:** <https://discuss.elastic.co/t/it-is-possible-to-set-phrase-queries-on-certain-multi-term-synonyms/134255>\
**Category:** Elasticsearch\
**Created:** [June 1, 2018, 4:06pm UTC](https://discuss.elastic.co/t/it-is-possible-to-set-phrase-queries-on-certain-multi-term-synonyms/134255 "2018-06-01T16:06:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imranazad](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@imranazad](https://discuss.elastic.co/u/imranazad)\
**Post date:** [June 1, 2018, 4:06pm UTC](https://discuss.elastic.co/t/it-is-possible-to-set-phrase-queries-on-certain-multi-term-synonyms/134255/1 "2018-06-01T16:06:37Z")

</div>

Hi,

I have the following synonyms mapping on the query side:

ckd, chronic kidney disease

On the index side I have the following:

chronic kidney disease =\> ckd, chronic kidney disease

1. If someones searches for 'ckd' it should only search for ckd and the phrase 'chronic kidney disease'

2. If someone searches for chronic kidney disease it should search for 'ckd' and chronic kidney disease both as a phrase and as conjoined terms so +chronic +kidney +disease anywhere in the document

Everything works great expect point 1, in order to get this to work I have to set auto\_generate\_synonyms\_phrase\_query to true but when I do this I lose the requirements in point 2 and lose the docs where the terms chronic, kidney and disease appear separately.

Is there any way around this?

Thanks

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [June 4, 2018, 8:21pm UTC](https://discuss.elastic.co/t/it-is-possible-to-set-phrase-queries-on-certain-multi-term-synonyms/134255/2 "2018-06-04T20:21:44Z")

</div>

You are running into an interesting challenge with multi-word synonyms, and that is that if you use a multi-word query for a phrase that has been defined as a synonym, in a `match` query it will be treated as a phrase. This breaks the usual behavior of `match` queries, where each of the search terms is treated as being in a simple OR query.

The easiest way to work around this, is to use query time synonyms instead of index time synonyms (preferably using [the `synonym_graph` token filter](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/analysis-synonym-graph-tokenfilter.html)). This will allow you to rewrite your query to a bool query, that searches for the terms with synonyms and without synonyms at the same time. As a result, you will find documents containing synonyms as well as documents that contain the individual search terms.

So, given an index with the following settings and mappings:

```auto
PUT ckd
{
  "settings": {
    "analysis": {
      "filter": {
        "my_synonyms": {
          "type": "synonym_graph",
          "synonyms": [
            "ckd, chronic kidney disease"
          ]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_synonyms"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "my_field": {
          "type": "text",
          "analyzer": "standard",
          "search_analyzer": "my_analyzer"
        }
      }
    }
  }
}

```

The following queries should return the desired results:

```auto
GET ckd/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "my_field": {
              "query": "ckd"
            }
          }
        },
        {
          "match": {
            "my_field": {
              "query": "ckd",
              "analyzer": "standard",
              "operator": "and"
            }
          }
        }
      ]
    }
  }
}

GET ckd/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "my_field": {
              "query": "chronic kidney disease"
            }
          }
        },
        {
          "match": {
            "my_field": {
              "query": "chronic kidney disease",
              "analyzer": "standard",
              "operator": "and"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![imranazad](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@imranazad](https://discuss.elastic.co/u/imranazad)\
**Post date:** [June 21, 2018, 3:59pm UTC](https://discuss.elastic.co/t/it-is-possible-to-set-phrase-queries-on-certain-multi-term-synonyms/134255/3 "2018-06-21T15:59:30Z")

</div>

Thanks @abdon I shall give this a go!

---

<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 19, 2018, 3:59pm UTC](https://discuss.elastic.co/t/it-is-possible-to-set-phrase-queries-on-certain-multi-term-synonyms/134255/4 "2018-07-19T15:59:33Z")

</div>

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