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

tom*_*271 6 elasticsearch elasticsearch-painless

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

考虑:

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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

当我运行无痛脚本时:

ctx._source.user.add({
    "first" : "Foo",
    "last" : "Bar"
})
Run Code Online (Sandbox Code Playgroud)

它不工作。我试过了,但找不到任何相关文件或其他人发起的讨论。

tom*_*271 11

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"
])
Run Code Online (Sandbox Code Playgroud)

请注意,无痛地图可以是混合类型的。