有没有一种方法可以使用脚本动态添加字段?我正在运行一个检查字段是否存在的脚本。如果没有,则创建它。
我正在尝试:
script: 'if (ctx._source.attending == null) { ctx._source.attending = { events: newField } } else if (ctx._source.attending.events == null) { ctx._source.attending.events = newField } else { ctx._source.attending.events += newField }'
除非attending
在我的案例中_source中有一个明确命名的字段,否则我将得到:
[Error: ElasticsearchIllegalArgumentException[failed to execute script];
nested: PropertyAccessException[
[Error: could not access: attending; in class: java.util.LinkedHashMap]
要检查字段是否存在,请使用ctx._source.containsKey
函数,例如:
curl -XPOST "http://localhost:9200/myindex/message/1/_update" -d'
{
"script": "if (!ctx._source.containsKey(\"attending\")) { ctx._source.attending = newField }",
"params" : {"newField" : "blue" },
"myfield": "data"
}'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句