Is there any response.to_json() method in es python client?

I'm using es python client elasticsearch-dsl-py.

def post()
    query = Search().query(some_query)
    response = query.execute()
    return response

The code raised errors because response was a generator, while I hope it returned a json object.
So I wanna know if there was a method to serialize response into json.

Not into json per say, but you can always get the underlying dict by calling response.to_dict() just as you can for any DSL object like Search or Query.

out of curiosity, can I ask why you need the json? Thanks!

1 Like

Hi @honzakral
I am writing APIs.So I need json response for the front-end.

I know to_dict() method. What I am using now is json.dumps(response.to_dict()). I supposed it's a bit verbose, which packed es json response into py dict and by next turn it back into json. Why can't I get the json response directly?

We don't keep the json around in python. If you just want the json back what you'd need to do is use the low-level API (elasticsearch-py) and supply your own serializer that doesn't actually deserialize the json and just passes it through. To still get the benefits of the dsl library you can always serialize the Search object (by calling to_dict on it) and passing it into search method manually like .execute does.