# Query\_string not applying synonyms filter

**URL:** <https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758>\
**Category:** Elasticsearch\
**Created:** [May 6, 2019, 11:49am UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758 "2019-05-06T11:49:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [May 6, 2019, 11:49am UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/1 "2019-05-06T11:49:22Z")

</div>

Hi

I am facing one issue while using **query\_string**. For strings having space it's not applying synonyms filter but same thing is working with **match query**.

index creation script-

> ```
> PUT my_index1
> {
> "settings": {
> "analysis": {
> "filter": {
> "content_synonyms": {
> "type": "synonym",
> "synonyms": [
> "air print => airprint"
> ]
> }
> },
> "analyzer": {
> "search_a": {
> "filter": [
> "lowercase",
> "content_synonyms"
> ],
> "type": "custom",
> "tokenizer": "standard"
> },
> "index_a": {
> "filter": [
> "lowercase"
> ],
> "type": "custom",
> "tokenizer": "standard"
> }
> }
> }
>         
> },
> "mappings": {
> "test": {
> "properties": {
> "body": {
> "type": "text",
> "analyzer": "index_a",
> "search_analyzer": "search_a",
> "search_quote_analyzer": "index_a"
> }
> }
> }
> }
> }
> 
> PUT my_index1/test/3
> {"body": "air"}
> 
> PUT my_index1/test/2
> {"body": "print"}
> 
> PUT my_index1/test/1
> {"body": "airprint"}
> 
> ```

query\_string I am using-

> ```
> GET my_index1/test/_search?explain
> {
> "size": 10,
> "query": {
> "query_string": {
> "query": "body:(air print)"
> }
> }
> }
> 
> ```

query\_string response -

> ```
> [
> {
> "_index": "my_index1",
> "_type": "test",
> "_id": "2",
> "_score": 0.2876821,
> "_source": {
> "body": "print"
> }
> },
> {
> "_index": "my_index1",
> "_type": "test",
> "_id": "3",
> "_score": 0.2876821,
> "_source": {
> "body": "air"
> }
> }
> ]
> 
> ```

I was expecting the 1st doc with **"airprint"**

In explain of query response(**weight(body:print in 0) & weight(body:air in 0)**) we can see it's only splitting as per tokenizer but not applying **"air print =\> airprint"** synonyms but while using same with **match query** synonyms are applied.

match query-

> ```
> GET my_index1/test/_search?explain
> {
> "query": {
> "match": {
> "body": "air print"
> }
> }
> }
> 
> ```

response-

> ```
> [
> {
> "_index": "my_index1",
> "_type": "test",
> "_id": "1",
> "_score": 0.2876821,
> "_source": {
> "body": "airprint"
> }
> }
> ]
> 
> ```

Here it's applying **"air print =\> airprint"** synonyms & returning the 1st docs with "airprint" & applying the synonyms. We can check the explain of this query(**weight(body:airprint in 0)**).

It's kind weird to me why synonyms (when strings with space) not working with **query\_string** but same thing works with **match query**. I was thinking both **(match query & query\_string)** internally worked almost similar.

Can anyone explain this please?

I am using **ES version 5.6.1**

---

<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:** [May 6, 2019, 12:05pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/2 "2019-05-06T12:05:08Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [May 6, 2019, 12:20pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/3 "2019-05-06T12:20:01Z")

</div>

Hi @dadoonet,

I have added all the settings & query scripts in my post.

---

<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:** [May 6, 2019, 12:24pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/4 "2019-05-06T12:24:21Z")

</div>

It could be useful to have a full script that we can just copy and paste to play with. Here people needs to write manually the script to reproduce it.  
Look again at the example I shared in the post I linked to. It's a complete one that we can just copy, paste and play.

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [May 6, 2019, 12:51pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/5 "2019-05-06T12:51:24Z")

</div>

Hi @dadoonet

I have edited my post with running scripts. I think this will work.

---

<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:** [May 6, 2019, 1:22pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/6 "2019-05-06T13:22:47Z")

</div>

So I tried your script on 7.0 and adapted it a bit:

```auto
DELETE my_index1
PUT my_index1
{
  "settings": {
    "analysis": {
      "filter": {
        "content_synonyms": {
          "type": "synonym",
          "synonyms": [
            "air print => airprint"
          ]
        }
      },
      "analyzer": {
        "search_a": {
          "filter": [
            "lowercase",
            "content_synonyms"
          ],
          "type": "custom",
          "tokenizer": "standard"
        },
        "index_a": {
          "filter": [
            "lowercase"
          ],
          "type": "custom",
          "tokenizer": "standard"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "body": {
        "type": "text",
        "analyzer": "index_a",
        "search_analyzer": "search_a",
        "search_quote_analyzer": "index_a"
      }
    }
  }
}

PUT my_index1/_doc/3
{"body": "air"}
PUT my_index1/_doc/2
{"body": "print"}
PUT my_index1/_doc/1
{"body": "airprint"}

GET my_index1/_search?explain=true
{
  "size": 10,
  "query": {
    "query_string": {
      "query": "body:(air print)"
    }
  }
}

```

This gives:

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808292,
    "hits" : [
      {
        "_shard" : "[my_index1][0]",
        "_node" : "bT2ZhV62S9WFG16Hx2mDzQ",
        "_index" : "my_index1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.9808292,
        "_source" : {
          "body" : "airprint"
        },
        "_explanation" : {
          "value" : 0.9808292,
          "description" : "weight(body:airprint in 0) [PerFieldSimilarity], result of:",
          "details" : [
            {
              "value" : 0.9808292,
              "description" : "score(freq=1.0), product of:",
              "details" : [
                {
                  "value" : 2.2,
                  "description" : "boost",
                  "details" : []
                },
                {
                  "value" : 0.98082924,
                  "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                  "details" : [
                    {
                      "value" : 1,
                      "description" : "n, number of documents containing term",
                      "details" : []
                    },
                    {
                      "value" : 3,
                      "description" : "N, total number of documents with field",
                      "details" : []
                    }
                  ]
                },
                {
                  "value" : 0.45454544,
                  "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                  "details" : [
                    {
                      "value" : 1.0,
                      "description" : "freq, occurrences of term within document",
                      "details" : []
                    },
                    {
                      "value" : 1.2,
                      "description" : "k1, term saturation parameter",
                      "details" : []
                    },
                    {
                      "value" : 0.75,
                      "description" : "b, length normalization parameter",
                      "details" : []
                    },
                    {
                      "value" : 1.0,
                      "description" : "dl, length of field",
                      "details" : []
                    },
                    {
                      "value" : 1.0,
                      "description" : "avgdl, average length of field",
                      "details" : []
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

```

Which is what your are expecting.  
I did not test on 5.6 though.

So I'd recommend upgrading to 7.0. May be a more recent version of 5.x would fix this though?

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [May 6, 2019, 1:36pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/7 "2019-05-06T13:36:30Z")

</div>

Thanks @dadoonet for your quick response.

Yeah just tried in 7.0, it's working fine over there. But we can't upgrade to 7.0.

Can you please check if we can do some trick with in the query or settings for the quick fix in 5.6.1?

---

<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:** [May 6, 2019, 1:48pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/8 "2019-05-06T13:48:10Z")

</div>

At least I'd try with 5.6.15: [https://www.elastic.co/fr/downloads/past-releases/elasticsearch-5-6-15](https://www.elastic.co/fr/downloads/past-releases/elasticsearch-5-6-15)

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [May 7, 2019, 5:54am UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/9 "2019-05-07T05:54:13Z")

</div>

Hi @dadoonet,

I tried in ES 5.6.16, it's not working there.

---

<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:** [May 7, 2019, 6:12am UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/10 "2019-05-07T06:12:38Z")

</div>

I'd upgrade. But may be @jimczi has another idea for 5.x series.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [May 7, 2019, 6:35am UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/11 "2019-05-07T06:35:19Z")

</div>

In 5.x whitespaces are considered as operators in the `query_string` query:  
The query string parser would interpret your query as a search for `"air OR print"` , while the token stored in your index is actually `"airprint"` . The option `split_on_whitespace=false` will protect it from being touched by the query string parser and will let the analysis run on the entire input ( `"air print"` ). Starting 6.x `split_on_whitespace` is always set to `false` which is why your query is working in this version. See the warning in the documentation regarding this behavior:  
[https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-query-string-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-query-string-query.html)

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [May 7, 2019, 1:45pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/12 "2019-05-07T13:45:36Z")

</div>

Thanks, @jimczi for your help. 😍

Yeah, I have gone through the documentation it worked. 👍

---

<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:** [June 4, 2019, 1:59pm UTC](https://discuss.elastic.co/t/query-string-not-applying-synonyms-filter/179758/13 "2019-06-04T13:59:32Z")

</div>

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