我想在ES文档中有一个对象数组。文档映射的一部分是
mapping_data = {
"mappings": {
config.ES_PAIRS: {
"properties": {
"id": {"type": "string"},
"decisions": {
"properties": {
"user": {"type": "string"},
"decision": {"type": "string"}
}
}
}
}
}
决策对象应该是一个数组,用于存储由决策和做出决策的用户组成的不同决策。 我希望能够使用脚本更新文档。
body = {
"script": "ctx._source.decisions.user += user; ctx._source.decisions.decision += decision",
"params": {
"decision": user,
"user": user
}
}
es.update(index=INDEX_NAME, doc_type=DOC_TYPE, id=ID, body=body)
我正在使用python客户端。 我得到了一个无法执行脚本错误。请帮忙。
File "/Users/..ve/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 83, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, u'RemoteTransportException[[Hannibal King][inet[/10.7.103.39:9300]][update]]; nested: ElasticSearchIllegalArgumentException[failed to execute script]; nested: PropertyAccessException[[Error: could not access: user; in class: java.util.ArrayList]\n[Near : {... ctx._source.decisions.user+use ....}]\n ^\n[Line: 1, Column: 1]]; ')
答案 0 :(得分:0)
如果存在,则向用户和决策对象添加检查,然后更新。
body = {
"script": "if (ctx._source.decisions['user'] != null && ctx._source.decisions['decision'] != null) {ctx._source.decisions.user += user; ctx._source.decisions.decision += decision }",
"params": {
"decision": user,
"user": user
}
}
es.update(index=INDEX_NAME, doc_type=DOC_TYPE, id=ID, body=body)