Scenario: have a document A on ElasticSearch and I want concatenate on this document some fields existing on document B.
Document A:
{
"id": 121423,
"name": "Sample Name",
"timestamp": "2020-10-01T00:12:00",
"age": 24
}
Document B:
{
"city": "New York",
"job": "programmer"
}
To write on this document I can use the write ES path and a body like it:
{"update":{"_index":"test-index","_id":"121423"}}
{"script": {"source":"if( ctx._source.containsKey('id') ){ ctx._source = params.param1; }","lang":"painless","params":{"param1":{"city": "New York", "job": "programmer"}}},"upsert":{"id":121423,"name": "Sample Name","timestamp": "2020-10-01T00:12:00","age": 24}}
But, if document A exists on ES, like check do (if( ctx._source.containsKey('id') )
), the document is completely overwrite. I can replace the attributed params to pick each element by step like:
{ ctx._source.city = params.param1.city; ctx._source.job = params.param1.job }
That would solve my problem, BUT this get me a problem, the logic can't be static, because on the real world, the document have many (Very many) fields, and the support of application gonna be hard. The desired document on the last update must be something like:
{
"id": 121423,
"name": "Sample Name",
"timestamp": "2020-10-01T00:12:00",
"age": 24,
"city": "New York",
"job": "programmer"
}
So, the question is, How I can update the document appending the new fields with little steps or using only one operator?