aparo / pyes

Python connector for ElasticSearch - the pythonic way to use ElasticSearch
BSD 3-Clause "New" or "Revised" License
607 stars 270 forks source link

Delete_by_query is broken #427

Closed unikoid closed 10 years ago

unikoid commented 10 years ago

Hello. I've encountered the following problem. I've updated elasticsearch and pyes and got the following elasticsearch error when doing delete_by_query request:

 Failed to execute [delete_by_query {[typed][_all], query [{"query": "{\"ids\": {\"values\": [\"103896\", \"401778\", \"510752\", \"449521\", \"449522\", \"510753\", \"994690\", \"994682\", \"994683\", \"994684\", \"994691\", \"994692\", \"1001224\"]}}"}]}]
org.elasticsearch.index.query.QueryParsingException: [typed] [_na] query malformed, must start with start_object

As you can see, query is malformed because request body contains string as a query object instead of the object itself. This can be fixed by simply replacing line

body = {"query":self._encode_query(query)}

to

body = '{"query":%s}'%self._encode_query(query)

in the delete_by_query method of ES object (es.py) (Sorry, I'm too lazy to provide a pull request and I'm not certain whether this solution breaks something or not)

And also in my opinion this method should determine somehow whether it's necessary to wrap provided query into {"query":query} object or not. Otherwise when you test your queries with curl or something you should start them with top-level "query" field and when you write python code - you shouldn't. It is not the obvious nor documented behaviour.

aparo commented 10 years ago

It was already fixed on the latest versions. It's: body = {"query": query.serialize()}