Removing items from an ArrayList with script

I'm attempting to try to remove items from an ArrayList on a doc, but not having any luck.

        source: "for(item in params.tags) { if(ctx._source.tag_list.indexOf(item) !== -1) { ctx._source.tag_list.remove(item) } }",
        params: {
          tags: tags
        }

The code runs, but the indexOf always returns -1 even though the document contains the tags I am trying to remove

can you provide a fully reproducible minimal example including index and document creation?

--Alex

@spinscale sure.

Document already indexed in my ES index:

{
    "_index": "my_index_development",
    "_type": "my_type",
    "_id": "1",
    "_version": 20,
    "_routing": "2",
    "found": true,
    "_source": {
        "first_name": "hello",
        "last_name": "world",
        "full_name": "hello world",
        "email": "fu@bar.com",
        "created_at": "2019-10-15T19:45:52.000Z",
        "user_id": 3,
        "account_id": 3,
        "lists": [],
        "tag_list": [
            [
                "challenger",
                "ram"
            ]
        ]
    }
} 

What I am trying to do is remove tags from the tag_list attribute.

POST /my_index/my_type/1/_update
{
    "script": {
        source: "for(item in params.tags) { if(ctx._source.tag_list.indexOf(item) !== -1) { ctx._source.tag_list.remove(item) } }",
        params: {
          tags: ['challenger']
        }
     }
}

I figured out my problem. It was working all along, but the tag_list attribute for this document got saved with a nested array which is why I kept running into problems.

The for loop works like its supposed too

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.