我有以下文件:
{
"likes": {
"data": [
{
"name": "a"
},
{
"name": "b"
},
{
"name": "c"
}
]
}
}
我试图运行一个update_by_query
来添加一个名为' like_count'的字段。使用likes.data
了解并非所有文档都包含likes.data
对象,这一点非常重要。
我试过这个:
POST /facebook/post/_update_by_query
{
"script": {
"inline": "if (ctx._source.likes != '') { ctx._source.like_count = ctx._source.likes.data.length }",
"lang": "painless"
}
}
但收到此错误消息:
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source.like_count = ctx._source.likes.data.length }",
" ^---- HERE"
],
"script": "if (ctx._source.likes != '') { ctx._source.like_count = ctx._source.likes.data.length }",
"lang": "painless"
}
答案 0 :(得分:2)
尝试ctx._source['likes.data.name'].length
根据https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html,ES中的对象数组被展平为
{
"likes.data.name" :["a", "b", "c"]
}
我们认为的对象数组数据类型是Nest数据类型。
答案 1 :(得分:0)
尝试一下
ctx._source['likes']['data'].size()