如何在Elasticsearch中创建嵌套对象并将其添加到嵌套字段中?

时间:2018-03-20 06:30:58

标签: elasticsearch elasticsearch-painless

github

考虑:

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "user": {
          "type": "nested" 
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

当我运行像:

这样的无痛脚本时
ctx._source.user.add({
    "first" : "Foo",
    "last" : "Bar"
})

它不起作用。我试过但找不到任何其他相关文件或讨论。

2 个答案:

答案 0 :(得分:3)

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/modules-scripting-painless-syntax.html#painless-maps

您可以创建一个类似['first': 'Foo', 'last': 'Bar', 'sthElse': 100]的地图,然后添加它。

所以:

ctx._source.user.add([
    "first" : "Foo",
    "last" : "Bar"
])

请注意,无痛地图可以混合使用。

答案 1 :(得分:0)

POST my_index/_doc/1/_update
{
   "script": {
      "lang": "painless",
      "inline": "ctx._source.user.add(params.object)",
      "params": {
         "object": {
            "first" : "Foo",
            "last" : "Bar"
         }
      }
   }
}
相关问题