Painless: check length field in each object of array

I want to filter my search results by using a script filter, to make sure all documents with a specific phonenumber field length are not showed up in the results:

"query": {
    "bool": {
        "filter": {
            "script": {
                 "script": {               
                     "source": "doc['identities.phonenumber.keyword'].toString().length() > params.min"
                     "params": {  
                         "min": 6
                     }
                 }
             }
         }
     }
}

The problem is that "identities" is an array of objects. So if there are two objects, each containing a "phonenumber" which length is equal to 4, it still shows up in the results (4 + 4 = 8).

Any thoughts how to fix this?

Guess I fixed it already, updated my query like this:

"query": {
    "bool": {
        "filter": {
            "script": {
                 "script": {               
                     "source": """
                         for(int i = 0; i < doc['identities.phonenumber.keyword'].values.length; i++){
                             if(doc['identities.phonenumber.keyword][i].toString().length() < params.min){
                                 return false;
                             }
                         }
                         return true;
                         """,
                     "params": {  
                         "min": 6
                     }
                 }
             }
         }
     }
}

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