ProgramSpree
ProgramSpree

Reputation: 402

Find length (size) of the array type field in each doc

I have a field in my index which is an integer array. I want to add another field which contains the count of the number of elements.

Even an API call that gives the count is fine.

I have found something relevant : "script" : "doc['MyField'].values.size()"

But I don't know how & where to use it. Please help!

Upvotes: 0

Views: 3280

Answers (1)

Val
Val

Reputation: 217314

You can do it like this using the Update by query API:

POST index/_update_by_query
{
    "query": {
        "match_all": {}
    }, 
    "script": {
       "source": "ctx._source.arrayLength = ctx._source.MyField.size()"
    }
}

Upvotes: 3

Related Questions