# Conditional “must” (only if both query and result have at least one tokens)

**URL:** <https://discuss.elastic.co/t/conditional-must-only-if-both-query-and-result-have-at-least-one-tokens/118216>\
**Category:** Elasticsearch\
**Created:** [February 2, 2018, 11:07am UTC](https://discuss.elastic.co/t/conditional-must-only-if-both-query-and-result-have-at-least-one-tokens/118216 "2018-02-02T11:07:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lleeoo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lleeoo/32/41624_2.png) [@lleeoo](https://discuss.elastic.co/u/lleeoo)\
**Post date:** [February 2, 2018, 11:07am UTC](https://discuss.elastic.co/t/conditional-must-only-if-both-query-and-result-have-at-least-one-tokens/118216/1 "2018-02-02T11:07:23Z")

</div>

I would like to write a (compound) query where all the results "must" match if both the query and the result both have at least one token from that analyzer, but where a result can also be returned if either the query or the result do not have a token from that analyzer.

**For example:**  
I am using Elasticsearch and created a custom analyzer where I filter out major city names. My analyzer extracts city names from strings:

`Department of Health, Milan` --\> `['Milan']`  
`Some text Health, Milan` --\> `['Milan']`  
`Some text Health, Paris` --\> `['Paris']`

I am trying to get the following behaviors from the following queries:

Search for `Department of Health, Milan` **can return** `Some text Health in Milan` (query and result have a common token)  
Search for `Department of Health, Milan` **can return** `Some text Health` (result has no token)  
Search for `Department of Health, Milan` **cannot return** `Some text Health in Paris` (query and result both have a token but have no common token)  
Search for `Department of Health` **can return** `Some text Health in Milan` (query has no token)

Any ideas how to achieve this ?

Thank you for your help.

---

<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:** [March 2, 2018, 11:07am UTC](https://discuss.elastic.co/t/conditional-must-only-if-both-query-and-result-have-at-least-one-tokens/118216/2 "2018-03-02T11:07:27Z")

</div>

Closed on March 2, 2018, 11:07am UTC

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