Home > Blockchain >  Merge incoming array document to existing document with elasticseach script in logstash
Merge incoming array document to existing document with elasticseach script in logstash

Time:11-01

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

CodePudding user response:

You just need to change your script to this:

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