4

我有以下文件:

{
  "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"
  }
4

2 回答 2

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"] 
}

我们认为的对象数组数据类型是 Nest 数据类型。

于 2017-06-22T12:24:25.890 回答
1

尝试这个

ctx._source['likes']['data'].size()
于 2019-01-04T07:00:15.300 回答