首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch-py使用脚本更新API

Elasticsearch-py使用脚本更新API
EN

Stack Overflow用户
提问于 2016-01-28 12:28:16
回答 2查看 3.3K关注 0票数 1

我试图通过ES 2.1.1上的elasticsearch-py python客户端使用Update API,但遇到了问题。

代码语言:javascript
复制
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
        }
    }
}

然后我尝试了以下两种方法:

代码语言:javascript
复制
es.update(index='boston', doc_type='stem_map', id=111, body=upscript)
es.update(index='boston', doc_type='stem_map', id=111, script=upscript)

我得到以下错误:

代码语言:javascript
复制
RequestError: TransportError(400, 'illegal_argument_exception', '[John Walker][127.0.0.1:9300][indices:data/write/update[s]]')

有人知道我做错了什么吗?

更新:这也不起作用

代码语言:javascript
复制
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]]')

答案:我的错误。我没有意识到我必须首先通过更改config/config ticsearch.yml文件来启用ES上的脚本。在启用脚本后,我的原始代码可以正常工作。

EN

回答 2

Stack Overflow用户

发布于 2016-01-28 12:32:50

我觉得你的upscript格式不对。尝尝这个

代码语言:javascript
复制
upscript = {
   "script" : "ctx._source.counter += count",
   "params" : {
      'count': 100
   }
}

并使用body=upscript

使用script=upscript是行不通的,因为这需要一个url编码的ctx._source.counter += count字符串,其正文为{"params" : { "count" : 100 }}

票数 2
EN

Stack Overflow用户

发布于 2016-01-28 13:27:54

是我的错。我没有意识到我必须首先在elasticsearch上启用脚本。在启用脚本后,我的原始代码可以正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35052885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档