# Strange behavior with match query and synonyms

**URL:** <https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481>\
**Category:** Elasticsearch\
**Created:** [June 19, 2013, 6:53pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481 "2013-06-19T18:53:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_T](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jorge\_T](https://discuss.elastic.co/u/Jorge_T)\
**Post date:** [June 19, 2013, 6:53pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/1 "2013-06-19T18:53:28Z")

</div>

Hi all,

I have a problem when trying to do a match query with synonyms. I can see  
my synonym filter working, and the query being expanded using Inquisitor.  
Also, I can see that my match query works when using the _or_ operator:

"match": {  
"autocomplete.ngram": {  
"query": "saint john",  
"slop": 0,  
"operator": "or"  
}  
}

Explain line:

- description: ConstantScore(autocomplete.ngram:st  
autocomplete.ngram:st. autocomplete.ngram:saint autocomplete.ngram:john),  
product of:

However, setting it to _and_ means I get no results. I think that the issue  
is that match is not respecting the token positions for the synonyms. I can  
see them all as token #1 in Inquisitor, and john as token #2 as expected,  
but clearly the query is doing something different. So the question is, am  
I doing something incorrectly? Is there another query type I should be  
looking at instead of match? I really appreciate anyone who can help.

Thanks,

Jorge

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 19, 2013, 7:10pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/2 "2013-06-19T19:10:23Z")

</div>

Are you applying the synonyms during indexing and querying, or only during  
indexing?

--  
Ivan

On Wed, Jun 19, 2013 at 11:53 AM, Jorge T  
[jorge.alberto.trujillo@gmail.com](mailto:jorge.alberto.trujillo@gmail.com)wrote:

> Hi all,
> 
> I have a problem when trying to do a match query with synonyms. I can see  
> my synonym filter working, and the query being expanded using Inquisitor.  
> Also, I can see that my match query works when using the _or_ operator:
> 
> "match": {  
> "autocomplete.ngram": {  
> "query": "saint john",  
> "slop": 0,  
> "operator": "or"  
> }  
> }
> 
> Explain line:
> 
> - description: ConstantScore(autocomplete.ngram:st  
> autocomplete.ngram:st. autocomplete.ngram:saint autocomplete.ngram:john),  
> product of:
> 
> However, setting it to _and_ means I get no results. I think that the  
> issue is that match is not respecting the token positions for the synonyms.  
> I can see them all as token #1 in Inquisitor, and john as token #2 as  
> expected, but clearly the query is doing something different. So the  
> question is, am I doing something incorrectly? Is there another query type  
> I should be looking at instead of match? I really appreciate anyone who can  
> help.
> 
> Thanks,
> 
> Jorge
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jorge\_T](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jorge\_T](https://discuss.elastic.co/u/Jorge_T)\
**Post date:** [June 19, 2013, 7:21pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/3 "2013-06-19T19:21:19Z")

</div>

Hi Ivan,

Synonyms are currently applied only at query time, the reason being that  
the content will be very large and it won't be feasible to reindex for  
every synonym change.

Thanks,

Jorge

On Wednesday, June 19, 2013 3:10:23 PM UTC-4, Ivan Brusic wrote:

> Are you applying the synonyms during indexing and querying, or only during  
> indexing?
> 
> --  
> Ivan
> 
> On Wed, Jun 19, 2013 at 11:53 AM, Jorge T \<[jorge.alber...@gmail.com](mailto:jorge.alber...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi all,
> > 
> > I have a problem when trying to do a match query with synonyms. I can see  
> > my synonym filter working, and the query being expanded using Inquisitor.  
> > Also, I can see that my match query works when using the _or_ operator:
> > 
> > "match": {  
> > "autocomplete.ngram": {  
> > "query": "saint john",  
> > "slop": 0,  
> > "operator": "or"  
> > }  
> > }
> > 
> > Explain line:
> > 
> > - description: ConstantScore(autocomplete.ngram:st  
> > autocomplete.ngram:st. autocomplete.ngram:saint autocomplete.ngram:john),  
> > product of:
> > 
> > However, setting it to _and_ means I get no results. I think that the  
> > issue is that match is not respecting the token positions for the synonyms.  
> > I can see them all as token #1 in Inquisitor, and john as token #2 as  
> > expected, but clearly the query is doing something different. So the  
> > question is, am I doing something incorrectly? Is there another query type  
> > I should be looking at instead of match? I really appreciate anyone who can  
> > help.
> > 
> > Thanks,
> > 
> > Jorge
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [June 19, 2013, 8:41pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/4 "2013-06-19T20:41:42Z")

</div>

Then AND is not working for you because query time synonyms are simply not in your index

For example you have "milk cow" indexed and say milk has dairy as a synonym. Your match AND query will effectively expand your "milk" query to "milk AND dairy" which is not in your index

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jorge\_T](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jorge\_T](https://discuss.elastic.co/u/Jorge_T)\
**Post date:** [June 19, 2013, 9:16pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/5 "2013-06-19T21:16:44Z")

</div>

Hey Alex,

Thanks, that confirms what I believed was the problem. That said, isn't  
this behavior the opposite of what it should be? If I have I have two  
documents, "milk cow" and "dairy cow", the whole point in my view is to  
enable the user to type "dairy cow" and get both results. What this means  
is that query time synonyms are not useful, at least with match queries. In  
my view, the correct and logical behavior should be match expanding to  
"(milk OR dairy) AND cow".

I'll play with bool and term or match queries with individual terms now to  
see if I can get the desired results, but this seems like an unnecessary  
workaround.

Regards,

Jorge

On Wednesday, June 19, 2013 4:41:42 PM UTC-4, AlexR wrote:

> Then AND is not working for you because query time synonyms are simply not  
> in your index
> 
> For example you have "milk cow" indexed and say milk has dairy as a  
> synonym. Your match AND query will effectively expand your "milk" query to  
> "milk AND dairy" which is not in your index

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [June 19, 2013, 9:37pm UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/6 "2013-06-19T21:37:46Z")

</div>

well, I guess match query just gets tokens and either does AND or OR on it  
it has no knowledge on how they were produced. It would have been nice if  
it could treat same position tokens as OR

I had similar issues when stemming even though I kept booth stemmed and  
original words (minus dups) in the same field.  
Guess you could analyze your search phrase to get it expanded with synonyms  
and then produce your query yourself using

On Wed, Jun 19, 2013 at 5:16 PM, Jorge T  
[jorge.alberto.trujillo@gmail.com](mailto:jorge.alberto.trujillo@gmail.com)wrote:

> Hey Alex,
> 
> Thanks, that confirms what I believed was the problem. That said, isn't  
> this behavior the opposite of what it should be? If I have I have two  
> documents, "milk cow" and "dairy cow", the whole point in my view is to  
> enable the user to type "dairy cow" and get both results. What this means  
> is that query time synonyms are not useful, at least with match queries. In  
> my view, the correct and logical behavior should be match expanding to  
> "(milk OR dairy) AND cow".
> 
> I'll play with bool and term or match queries with individual terms now to  
> see if I can get the desired results, but this seems like an unnecessary  
> workaround.
> 
> Regards,
> 
> Jorge
> 
> On Wednesday, June 19, 2013 4:41:42 PM UTC-4, AlexR wrote:
> 
> > Then AND is not working for you because query time synonyms are simply  
> > not in your index
> > 
> > For example you have "milk cow" indexed and say milk has dairy as a  
> > synonym. Your match AND query will effectively expand your "milk" query to  
> > "milk AND dairy" which is not in your index
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/jXFtmWh60mI/unsubscribe](https://groups.google.com/d/topic/elasticsearch/jXFtmWh60mI/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_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:** [July 6, 2017, 2:30am UTC](https://discuss.elastic.co/t/strange-behavior-with-match-query-and-synonyms/12481/7 "2017-07-06T02:30:20Z")

</div>


