简体   繁体   中英

Elasticsearch-py update API with script

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM