Search code examples
elasticsearchelasticsearch-dslelasticsearch-py

Elastic search query using bool instead of not


I'm trying to construct an elasticsearch query for not using bool, since not is deprecated. E.g. Match all documents that don't belong to foo@bar.com.

I tried these using elasticsearch-dsl (following this post https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query):

{'query': {
    'bool': {
        'must_not': [{
            'match': {
                'author': 'foo@bar.com'
            }
        }]
    }
}}

This returns no results although documents other than 'author': 'foo@bar.com' exist.

I also tried

{'query': {
    'bool': {
        'must_not': [{
            'term': {
                'author': 'foo@bar.com'
            }
        }]
    }
}}

This returns a mix of documents where some have 'author': 'foo@bar.com' and some don't.


Solution

  • This turned out to be an issue with Mapping changes in 5.0 (https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_50_mapping_changes.html)

    The following query worked as expected:

    {'query': {
        'bool': {
            'must_not': [{
                'term': {
                    'author.keyword': 'foo@bar.com'
                }
            }]
        }
    }}