Elasticsearch [size] query malformed, no start_object after query name'

Advertisements

I have a query I was using in Postman. It is:

{ "size": 1, "sort": { "event_date": "desc"}, "query": { "match_all": {} } }

This returns the latest date in event_date for everything ingested and performs just like I want it to.

When I go to Python, I get the error though:

elasticsearch.exceptions.RequestError: RequestError(400, ‘parsing_exception’, ‘[size] query malformed, no start_object after query name’)

The call:

last_date_query = {
   "size": 1,
   "sort": { "event_date": "desc"},
   "query": {
      "match_all": {}
   }
}

if es is None:
    es = connect()

search_result = es.search(index=index, query = last_date_query)

How do I get the same query results when calling elasticsearch from python for my rather simple query?

I tried the above code in Python.

>Solution :

The query argument should only contain the query part, size and sort are other parameters to the search function.
You need to do it this way

sort = { "event_date": "desc"}
last_date_query = {
   "match_all": {}
}

if es is None:
    es = connect()

search_result = es.search(index=index, query = last_date_query, size = 1, sort = sort)

Leave a ReplyCancel reply