Search code examples
elasticsearchlogstasharray-merge

Merge incoming array document to existing document with elasticseach script in logstash


I want to merge new document to existing document. My existing document is like that:

{
    "_source": {
        "name": "xxx",
        "recall": [
            { 
                "title": "xxx-a",
                "date": "2020-12-10"
                "otherB": "abc"
            }
        ]
    }
}

the new inserted document is like that:

{
    "_source": {
        "name": "xxx",
        "recall": [
            { 
                "title": "xxx-b",
                "date": "2021-12-10"
                "otherB": "abcd"
            }
        ]
    }
}

I want the output as:

{
    "_source": {
        "name": "xxx",
        "recall": [
            { 
                "title": "xxx-a",
                "date": "2020-12-10"
                "otherB": "abc"
            },
            { 
                "title": "xxx-b",
                "date": "2021-12-10"
                "otherB": "abcd"
            }
        ]
    }
}

I am using logstash to load data to ES. My current script is this:

script_lang => "painless"
script =>"if (ctx._source.containsKey('recall'))ctx._source.recall.addAll(params.event.recall);}"

But it gives me this output which is append the inserted doc to existing not merge:

{
    "_source": {
        "name": "xxx",
        "recall": [
            { 
                "title": "xxx-a",
                "date": "2020-12-10"
                "otherB": "abc"
            },
          [
            { 
                "title": "xxx-b",
                "date": "2021-12-10"
                "otherB": "abcd"
            }
          ]
        ]
    }
}

Solution

  • You just need to change your script to this:

    script =>"if (ctx._source.containsKey('recall'))ctx._source.recall.addAll(Arrays.asList(params.event.recall));}"