使用无痛弹性消除嵌套对象

时间:2019-08-30 10:39:51

标签: elasticsearch

假设我们有以下索引:

PUT test
{
  "mappings": {
    "properties": {
      "message": {
        "type": "nested" 
      }
    }
  }
}

PUT test/_doc/1
{
  "message" : [
    {
      "id" : "123",
      "body" : "hello"
    },
    {
      "id" : "124",
      "body" :  "world"
    }
  ]
}

PUT test/_doc/2
{
  "message" : [
    {
      "id" : "125",
      "body" : "hello"
    },
    {
      "id" : "126",
      "body" :  "world"
    }
  ]
}

什么是有效的无痛脚本来删除message字段内的对象?例如,我要删除第一个文档中带有"id": "123"的所有消息。我目前正在使用此脚本(作为存储脚本,但下面将其作为嵌入式脚本包含在内)

POST test/_update/1
{
    "script" : {
        "source": "if (ctx._source.containsKey(params[\"field\"])) {ctx._source[params[\"field\"]] = ctx._source[params[\"field\"]].stream().filter(x -> x[\"id\"] != params[\"value\"]).collect(Collectors.toList())}",
        "lang": "painless",
        "params" : {
            "field" : "message",
            "value" : "123"
        }
    }
}

但是我想知道是否还有其他选择。它的操作似乎有些冗长。

0 个答案:

没有答案