在Elasticsearch

时间:2016-05-01 11:44:03

标签: elasticsearch append sense

我目前正在努力解决如何将值附加到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"作为数组的新元素?

1 个答案:

答案 0 :(得分:3)

看来你的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"
      ]
    }