0

How can I remove 'match_all' from the the following query:

es = Elasticsearch()
s = Search(es)   
s = s.filter("term", status="Free")
s.to_dict()
{'query': {'filtered': {'filter': {'term': {'status': 'Free'}}, 'query': {'match_all': {}}}}}
Shipra
  • 1,259
  • 2
  • 14
  • 26

1 Answers1

1

The match_all query is optional here, it is part of the filtered query:

{
    'query': {
        'filtered': {
            'filter': {
                'term': {
                    'status': 'Free'
                }
            },
            'query': {
                'match_all': {}
            }
        }
    }
}

According to the spec you can remove it, match_all is the default: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#_filtering_without_a_query

A way to remove a key from a dictionary in python is the pop method:

d = s.to_dict()
d['query']['filtered'].pop('query')

You don't have to remove the key before sending the query, the server will just ignore it.

fafl
  • 7,222
  • 3
  • 27
  • 50