11

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

Consider:

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"
    }
  ]
}

When I run a painless script like:

ctx._source.user.add({
    "first" : "Foo",
    "last" : "Bar"
})

It is not working. I tried but cannot find any related documents or discussion started by other.

0

2 Answers 2

16

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

You can create a map like ['first': 'Foo', 'last': 'Bar', 'sthElse': 100] then add it.

So:

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

Please note that map in painless can be mixed type.

Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

No I need to create my nested object dynamically.
Please elaborate what you want to do.
I have solution already as mentioned below. stackoverflow.com/a/49378001/2736817
I like this. Could my params.object be a JsonData.of(myObject) where myObject is a Java record, for example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.