# Querying synonyms

**URL:** <https://discuss.elastic.co/t/querying-synonyms/102161>\
**Category:** Elasticsearch\
**Created:** [September 28, 2017, 5:44pm UTC](https://discuss.elastic.co/t/querying-synonyms/102161 "2017-09-28T17:44:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiqizheng](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@shiqizheng](https://discuss.elastic.co/u/shiqizheng)\
**Post date:** [September 28, 2017, 5:44pm UTC](https://discuss.elastic.co/t/querying-synonyms/102161/1 "2017-09-28T17:44:45Z")

</div>

I'm trying to retrieve all documents using synonyms:

PUT /my\_index  
{  
"settings": {  
"analysis": {  
"filter": {  
"my\_synonym\_filter": {  
"type": "synonym",  
"synonyms": [  
"british,english",  
"queen,monarch monarch"  
]  
}  
},  
"analyzer": {  
"my\_synonyms": {  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"my\_synonym\_filter"  
]  
}  
}  
}  
}  
}

PUT [http://localhost:9200/my\_index/\_bulk](http://localhost:9200/my_index/_bulk)  
{"index":{"\_index":"my\_index","\_type":"test"}}  
{"name":"queen"}  
{"index":{"\_index":"my\_index","\_type":"test"}}  
{"name":"monarch monarch"}

POST [http://localhost:9200/my\_index/\_search?size=500](http://localhost:9200/my_index/_search?size=500)  
{  
"query":{  
"query\_string":{  
"query":"monarch monarch",  
"analyzer":"my\_synonyms"  
}  
}

}  
Analyzer result for "monarch monarch"

{  
"tokens": [  
{  
"token": "monarch",  
"start\_offset": 0,  
"end\_offset": 7,  
"type": "",  
"position": 0  
},  
{  
"token": "queen",  
"start\_offset": 0,  
"end\_offset": 15,  
"type": "SYNONYM",  
"position": 0  
},  
{  
"token": "monarch",  
"start\_offset": 8,  
"end\_offset": 15,  
"type": "",  
"position": 1  
}  
]  
}  
These are curl commands from Sense. When querying "monarch monarch", it's only returning the "monarch monarch" document. I was hoping to get the document containing "queen" too since they are defined as synonyms.

---

<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:** [September 28, 2017, 9:48pm UTC](https://discuss.elastic.co/t/querying-synonyms/102161/2 "2017-09-28T21:48:07Z")

</div>

By default, the query\_string query splits the query on whitespace before analysis. That means, the analyzer does not receive "monarch monarch" as input (which would result in a query for the synonym "queen" also), but two separate inputs "monarch" and "monarch" (which do not have a synonym defined individually).

There are two solutions. Set `split_on_whitespace` to `false`:

```
POST my_index/_search?size=500
{
  "query": {
    "query_string": {
      "query": "monarch monarch",
      "analyzer": "my_synonyms",
      "split_on_whitespace": false
    }
  }
}

```

Or use a `match` query instead:

```
POST my_index/_search?size=500
{
  "query": {
    "match": {
      "name": {
        "query": "monarch monarch",
        "analyzer": "my_synonyms"
      }
    }
  }
}
```

---

<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:** [October 26, 2017, 9:48pm UTC](https://discuss.elastic.co/t/querying-synonyms/102161/3 "2017-10-26T21:48:16Z")

</div>

Closed on October 26, 2017, 9:48pm UTC

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