Copy values from one field to another within a nested object

I have a document that has the structure with an field object with a nested field internally. The nested field is responsible for storing all interactions that occurred in an internal communication.
It happens that I need to create a new field inside the nested field, with a new type that will now be used to store the old field with a new parser.

How can I copy the data from the old field to the new field inside the nested field?

My document:


curl -XPUT 'localhost:9200/problems?pretty' -H 'Content-Type: application/json' -d '
{
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "problem": {
            "properties": {
                "problemid": {
                    "type": "long"
                },
                "subject": {
                    "type": "text",
                    "index": true
                },
                "usermessage": {
                    "type": "object",
                    "properties": {
                        "content": {
                            "type": "nested",
                            "properties": {
                                "messageid": {
                                    "type": "long",
                                    "index": true
                                },
                                "message": {
                                    "type": "text",
                                    "index": true
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}'

My New Field:

curl -XPUT 'localhost:9200/problems/_mapping/problem?pretty' -H 'Content-Type: application/json' -d '
{
    "properties": {
        "usermessage": {
            "type": "object",
            "properties": {
                "content": {
                    "type": "nested",
                    "properties": {
                        "message_accents" : {
                            "type" : "text",
                            "analyzer" : "ignoreaccents"
                        }
                    }
                }
            }
        }
    }
}
'

Data Example:

{
    "problemid": 1,
    "subject": "Test",
    "usermessage": [
        {
            "messageid": 1
            "message": "Hello"
        },
        {
            "messageid": 2
            "message": "Its me"
        },
    ]
}'

My script to copy fields:

curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_all": {
        }
    },
    "script": "ctx._source.usermessage.content.message_accents = ctx._source.usermessage.content.message"
}'

My script should probably go through the entire array, but I don't know how to do this.

Edit:

I tried the code below but it didn't work, it returns an error.

curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_all": {
        }
    },
    "script": "ctx._source.usermessage.content.each { elm -> elm.message_accents = elm.message }"
}
'

"script":"ctx._source.usermessage.content.each { elm -> elm.message_accents = elm.message }","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"unexpected token ['{'] was expecting one of [{, ';'}]."}},"status":500}%

Solved:

curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_all": {
        }
    },
    "script": "for(int i = 0; i < ctx._source.usermessage[0].content.length; i++) {ctx._source.usermessage[0].content[i].message_accents = ctx._source.usermessage[0].content[i].message}"
}
'

Due to my lack of knowledge, the usermessage field is an array, and within that array there is another array (content) which is where the values ​​are, so I must set index 0 by default.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.