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.

plam
  • 1,305
  • 3
  • 15
  • 24

2 Answers2

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 }}.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • `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]]')` – plam Jan 28 '16 at 04:58
  • Your id parameter should be a string, not an int. Other than that, are you sure the index and doc_type are correct? – OneCricketeer Jan 28 '16 at 05:02
  • Yep. Still doesn't work. See my update. It clearly shows that the document is indexing correctly with the same index and doc_type. – plam Jan 28 '16 at 05:17
0

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

plam
  • 1,305
  • 3
  • 15
  • 24