附加到 Elasticsearch 中的数组

Append to array in Elasticsearch

我目前正在为如何在 elasticsearch 中将值附加到数组而苦苦思索。

文档看起来像这样:

  {
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "iethreads",
        "_type": "thread",
        "_id": "AVRk6WRMU5h_y_zwo4s0",
        "_score": 1,
        "fields": {
          "links": [
            "[\"https://somelink123.net/thread-714222&page=1\", \"https://somelink123.net/thread-714222&page=2\", \"https://somelink123.net/thread-714222&page=3\", \"https://somelink123.net/thread-714222&page=4\"]"
          ]
        }
      }
    ]
  }
}

然后我运行下面的更新查询

POST _update
{
  "script" : "ctx._source.links+=new_posts",
  "params" : {
    "new_posts":"blabliblub"
  }
}

我明白了:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "iethreads",
        "_type": "thread",
        "_id": "AVRk6WRMU5h_y_zwo4s0",
        "_score": 1,
        "fields": {
          "links": [
            "[\"https://somelink123.net/thread-714222&page=1\", \"https://somelink123.net/thread-714222&page=2\", \"https://somelink123.net/thread-714222&page=3\", \"https://somelink123.net/thread-714222&page=4\"]blabliblub"
          ]
        }
      }
    ]
  }
}

所以对我来说,这看起来像将数组视为字符串,它只是附加字符串 - 这不是我想要的。

如何将 "blabliblub" 作为新元素附加到数组?

您的 links 字段似乎实际上有一个元素作为字符串而不是数组。为了您的更新成功,您的结构必须是这样的:

"fields": {
      "links": [
        "https://somelink123.net/thread-714222&page=1", 
        "https://somelink123.net/thread-714222&page=2",
        "https://somelink123.net/thread-714222&page=3", 
        "https://somelink123.net/thread-714222&page=4"
      ]
    }