Search code examples
elasticsearchupdateselasticsearch-painless

ES: save current doc into subfield as historic revision


I am trying to implement store older versions within the same document using painless scripting:

curl -XPOST http://127.1:9200/index1/type1/docid/_update -d \
{"script": {"inline": "ctx._source.previous.add(ctx._source)"}}

The response is

{
    "error": {
        "root_cause": [{
            "type": "remote_transport_exception",
            "reason": "[DJxHoBx][172.17.0.2:9300][indices:data/write/update[s]]"
        }],
        "type": "illegal_argument_exception",
        "reason": "Object has already been built and is self-referencing itself"
    },
    "status": 400
}

So how can I make this work?


Solution

  • solved by manually enumerate each keys:

    ctx._source['previous'].add({'x': ctx._source.x})