1

I'm trying to use the Update API via the elasticsearch-py python client on ES 2.1.1 and I'm having trouble.

es.index(index='boston', doc_type='stem_map', id=111, body={'word': 'showing', 'counter': 29})
es.get(index='boston', doc_type='stem_map', id=111)

{'_id': '111',
 '_index': 'boston',
 '_source': {'counter': 29, 'word': 'showing'},
 '_type': 'stem_map',
 '_version': 1,
 'found': True}

upscript = {
    'script': {
        'inline': 'ctx._source.counter += count', 
        'params': {
            'count': 100
        }
    }
}

Then I tried both of the following:

es.update(index='boston', doc_type='stem_map', id=111, body=upscript)
es.update(index='boston', doc_type='stem_map', id=111, script=upscript)

I'm getting the following error:

RequestError: TransportError(400, 'illegal_argument_exception', '[John Walker][127.0.0.1:9300][indices:data/write/update[s]]')

Does anybody know what I'm doing wrong?

UPDATE: This also does not work

es.index(index='boston', doc_type='stem_map', id='111', body={'word': 'showing', 'counter': 29})

{'_id': '111',
 '_index': 'boston',
 '_shards': {'failed': 0, 'successful': 1, 'total': 2},
 '_type': 'stem_map',
 '_version': 1,
 'created': True}

upscript = {
   "script" : "ctx._source.counter += count",
   "params" : {
      'count': 100
   }
}

es.update(index='boston', doc_type='stem_map', id='111', body=upscript)

WARNING:elasticsearch:POST /boston/stem_map/111/_update [status:400 request:0.002s]
...
RequestError: TransportError(400, 'illegal_argument_exception', '[Atum][127.0.0.1:9300][indices:data/write/update[s]]')

ANSWER: My mistake. I didn't realize I had to enable scripting on ES first by changing the config/elasticsearch.yml file. My original code works after enabling scripting.

2

2 Answers 2

2

I think your upscript is the wrong format. Try this

upscript = {
   "script" : "ctx._source.counter += count",
   "params" : {
      'count': 100
   }
}

And use body=upscript.

Using script=upscript doesn't work because that requires a url-encoded string of ctx._source.counter += count with the body being {"params" : { "count" : 100 }}.

Sign up to request clarification or add additional context in comments.

3 Comments

upscript = { "script" : "ctx._source.counter += count", "params" : { 'count': 100 } } es.update(index='boston', doc_type='stem_map', id=111, body=upscript) This still gives me an error: RequestError: TransportError(400, 'illegal_argument_exception', '[Atum][127.0.0.1:9300][indices:data/write/update[s]]')
Your id parameter should be a string, not an int. Other than that, are you sure the index and doc_type are correct?
Yep. Still doesn't work. See my update. It clearly shows that the document is indexing correctly with the same index and doc_type.
0

My mistake. I did not realize that I had to first enable scripting on elasticsearch. My original code works after enabling scripting.

2 Comments

Oops. Just saw this. Was your original code correct, or was I also right with the upscript?
Both your version of the upscript and my version worked with the body parameter, so we were both right!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.