# Match query unexpected behavior with token filter

**URL:** <https://discuss.elastic.co/t/match-query-unexpected-behavior-with-token-filter/66289>\
**Category:** Elasticsearch\
**Created:** [November 16, 2016, 6:54pm UTC](https://discuss.elastic.co/t/match-query-unexpected-behavior-with-token-filter/66289 "2016-11-16T18:54:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![redshadow](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@redshadow](https://discuss.elastic.co/u/redshadow)\
**Post date:** [November 16, 2016, 6:54pm UTC](https://discuss.elastic.co/t/match-query-unexpected-behavior-with-token-filter/66289/1 "2016-11-16T18:54:33Z")

</div>

I have an analyzer that analyzes to create redundant tokens. For instance:

Original string: "TokyoJapan Samurai"  
After tokenizing--\>

1. "TokyoJapan"
2. "Samurai"  
After token filter--\>
3. "tokyo", "japan", "tokyojapan"
4. "samurai"  
therefore, we get a total of 4 tokens ie ("tokyo", "japan", "tokyojapan", "samurai")

Now if i issue an ANDed match query using the same analyzer to match the string "OsakaJapan Samurai":  
after token filter (just like above), the tokens generated should be--\>

1. "osaka", "japan", "osakajapan"
2. "samurai"

However, this matches a field containing "TokyoJapan Samurai" as well. The reason is that even with the AND for match queries, it is internally looking for:  
MATCH any of tokens generated in (1) ie ("osaka" OR "japan" OR "osakajapan")  
AND any of tokens generated in (2) ie ("samurai")

ie ("osaka" OR "japan" OR "osakajapan") AND ("samurai")

I would have ideally liked it to be:  
MATCH (("osaka" and "japan") OR "osakajapan") AND "samurai"

This way the following docs would have matched/notmatched --\>

1. "OsakaJapan Samurai" -matches obviously
2. "osakAjapan Samurai" - matches only because osakajapan matches, and not because "osak" "Ajapan" exists
3. "Osaka Samurai" - matches because 'osaka' and 'samurai' match.
4. "TokyoJapan samurai" -- doesnt match because neither (tokyo and japan) nor (tokyojapan) match.

Any idea how I can achieve this behavior?

I believe I'd have to use a different search analyzer from the indexing analyzer, but I may be wrong.

---

<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:** [December 14, 2016, 6:54pm UTC](https://discuss.elastic.co/t/match-query-unexpected-behavior-with-token-filter/66289/2 "2016-12-14T18:54:49Z")

</div>

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