Or *_*ger 4 elasticsearch elasticsearch-painless
我有以下文件:
{
"likes": {
"data": [
{
"name": "a"
},
{
"name": "b"
},
{
"name": "c"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行一个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"
}
}
Run Code Online (Sandbox Code Playgroud)
但是收到此错误消息:
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
小智 5
尝试 ctx._source['likes.data.name'].length
根据 https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html,ES 中的对象数组被展平为
{
"likes.data.name" :["a", "b", "c"]
}
Run Code Online (Sandbox Code Playgroud)
我们认为的对象数组数据类型是 Nest 数据类型。
| 归档时间: |
|
| 查看次数: |
7776 次 |
| 最近记录: |