3

I have the following code

body: {
    script: {
        source: 'ctx._source.permissions.add(params.permission);',
        params: {
            permission
        }
    }
}

Permission object is an object being appended to array of permissions. Permission object will have some id property. So permission object would be something like:

{
  id: 'some unique value',
  name: 'permission name
}

How do I only add it, if there is no permission object in that array with the property id, so If that array has an object with the params.id value I am trying to add, the add should fail...

1 Answer 1

4

Try Below source :

"source":"List perm=ctx._source.permissions; if(perm!=null && !perm.stream().anyMatch(p-> p.id.equals(params.permission.id))){ctx._source.permissions.add(params.permission)}

I would advice you go through painless document.

Sign up to request clarification or add additional context in comments.

1 Comment

I believe I am using only update..single doc needs to be updated only... I'll mark it as answered anyways. Thank you a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.