Bulk update with Python's elasticsearch client

Obj3ctiv3_C_88 picture Obj3ctiv3_C_88 · Feb 3, 2016 · Viewed 11.8k times · Source

I'm attempting to do a bulk update based on state change on a document property. Create works fine but bulk is freaking out. I'm getting an error to the effect of "script or doc is missing" but everything looks good.

Here is how I am attempting the bulk update:

frequency_cleared = [
    {
        "_id": result['_id'], 
        "_type": "the-type", 
        "_index": "the-index", 
        "_source": result['_source'],
        "_op_type": 'update'
    } 
    for result in search_results['hits']['hits']
]

The reason I'm iterating over my results is that I use an if in my list comprehension but since I'm able to see the results I get back I know that isn't the issue. I can't show results and had to change property names since this is for the company I work at.

Here is the traceback:

Elasticsearch.exceptions.RequestError: 
TransportError(400, 'action_request_validation_exception',
  'Validation Failed: 1: script or doc is missing...') 

The ellipses represent it showing the same error for every element in the list fails.

Answer

Obj3ctiv3_C_88 picture Obj3ctiv3_C_88 · Feb 3, 2016

It was difficult to tell based on the docs but I found out the issue. If you want to do a bulk update you need to wrap your source in a dictionary with the key being "doc". Here is the correct example, hope this helps!

frequency_cleared = [
    {
        '_id': result['_id'], 
        "_type": "the-type", 
        "_index": "the-index", 
        "_source": {'doc': result['_source']}, 
        '_op_type': 'update'
    } 
    for result in search_results['hits']['hits']
]

Notice the slight change is "_source" to {'doc': result['_source']}