0

this is a groovy script. The problem is the ctx._source.academies.remove(index) because ctx is not visible there. The index variable is the right... but i cant use the ctx there. Any suggestions?

{
    "script" : "ctx._source.academies.eachWithIndex { it, index -> if(it['academy_id'] == academy_id) ctx._source.academies.remove(index) }",
    "params": {
        "academy_id": 344
    }
}

2 Answers 2

1

Have you tried this?

ctx._source.academies.removeAll { it['academy_id'] == academy_id }

In case you need to check academy_id matching a list of items, then below can be used: (The closure should satisfy a criteria)

ctx._source.academies.removeAll { it['academy_id'] in [academy_id, some_other_id] }

From an elastic search perspective, I suppose the script can be rewritten as below:

{
    "script" : "ctx._source.academies.removeAll { it['academy_id'] in academy_ids }",
    "params": {
        "academy_ids": [344, 345, 346]
    }
}
2
  • You are perfect!!!!!! Thank you.... How can we make the academy_id array? Can we give to params the academies as array? So we can delete multiple academies with multiple ids at once
    – Michalis
    Aug 26, 2016 at 20:32
  • Yes of course. All you need is a boolean returning from closure. See the updated answer.
    – dmahapatro
    Aug 26, 2016 at 20:36
0

is this the best solution?

def findIndex; ctx._source.academies.eachWithIndex { it, index -> if(it['academy_id'] == academy_id) findIndex = index }; ctx._source.academies.remove(findIndex)

but this works only for the last found object

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.