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 [email protected].

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': '[email protected]'
            }
        }]
    }
}}

This returns no results although documents other than 'author': '[email protected]' exist.

I also tried

{'query': {
    'bool': {
        'must_not': [{
            'term': {
                'author': '[email protected]'
            }
        }]
    }
}}

This returns a mix of documents where some have 'author': '[email protected]' 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': '[email protected]'
                }
            }]
        }
    }}