A Dev
A Dev

Reputation: 321

elastic search partial update to nested document

I have the following document:

{
            {
                "_index": "demo_processinglog",
                "_type": "status",
                "_id": "1498130124024a",
                "_score": 1,
                "_source": {
                    "startTime": 1411230604024,
                    "clientName": "fastlog",
                    "projectName": "demo",
                    "sourcePath": "/nfs/clients/client1/projects/project1/",
                    "sourceFiles": [
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt",
                            "fileSize": 3563
                            "status": "123"
                        },
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/XML/data.xml",
                            "fileSize": 51893940,
                            "status": 234
                        },
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/XML/data2.xml",
                            "fileSize": 1665,
                            "status": 345
                        },
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/XML/data3.xml",
                            "fileSize": 5799680,
                            "status": 456
                        }
                    ]
                }
            }

What I want to do is update the status field for a particular source file. I have tried doing an update using the "_update" endpoint, however if I try to update the "status" field for just the source file:

"sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt"

It deletes everything else that is in the "sourceFiles" array. How can I update only the status field for a particular "sourceFile" and leave the other data intact? I also tried the update using the "doc_as_upsert" option and it still deletes everything else.

UPDATE

I just tied the following script, which runs, however nothing is updated:

{
    "script": "for ( int i = 0; i < ctx._source.sourceFiles.size(); i ++ ) {if ( ctx._source.sourceFiles[ i ].fileName == params.sourceFile) {ctx._source.sourceFiles[ i ].status = params.status;} }",
    "lang": "painless",
    "params": {
            "status": "completed",
            "sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt"
    }
}

Thanks!

Upvotes: 0

Views: 697

Answers (1)

A Dev
A Dev

Reputation: 321

So, the script does exactly what I wanted, I just need to call it using, "inline", so the final working script is:

{
    "script":{
"inline":  "for ( int i = 0; i < ctx._source.sourceFiles.size(); i ++ ) {if ( ctx._source.sourceFiles[ i ].fileName == params.sourceFile) {ctx._source.sourceFiles[ i ].status = params.status;} }",
    "lang": "painless",
    "params": {
            "status": "completed",
            "sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt"
    }
  }
}

Upvotes: 1

Related Questions