是否有一种使用脚本动态添加字段的方法?我正在运行一个脚本来检查字段是否存在。如果没有,然后创建它。Elasticsearch - 如果不存在,则使用脚本创建字段
我尝试:
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 }'
除非,除非我在_source在我的情况明确指定attending
有一个字段,我得到:
[Error: ElasticsearchIllegalArgumentException[failed to execute script];
nested: PropertyAccessException[
[Error: could not access: attending; in class: java.util.LinkedHashMap]
谢谢你将在我有机会的时候测试一下。有没有一个像'contains'和'containsKey'这样的所有方法的网站?如果我知道在哪里看,我会尝试他们。 – Maruf
我不知道任何单一资源 - 我的信息来自各种论坛。当我遇到这个问题时,我最终决定将这些字段存储在一个单独的子文档中 - 这看起来更加健壮和简单。 –