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

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.

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