# Painless: check length field in each object of array

**URL:** https://discuss.elastic.co/t/painless-check-length-field-in-each-object-of-array/161699
**Category:** Elasticsearch
**Tags:** painless
**Created:** [December 20, 2018, 12:46pm UTC](https://discuss.elastic.co/t/painless-check-length-field-in-each-object-of-array/161699 "2018-12-20T12:46:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ErikvdVen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erikvdven/32/24602_2.png) [@ErikvdVen](https://discuss.elastic.co/u/ErikvdVen)
#### Post date: [December 20, 2018, 12:46pm UTC](https://discuss.elastic.co/t/painless-check-length-field-in-each-object-of-array/161699/1 "2018-12-20T12:46:03Z")

</div>

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:

```auto
"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?

---

<div class="post-metadata">

### Author: ![ErikvdVen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erikvdven/32/24602_2.png) [@ErikvdVen](https://discuss.elastic.co/u/ErikvdVen)
#### Post date: [December 20, 2018, 1:09pm UTC](https://discuss.elastic.co/t/painless-check-length-field-in-each-object-of-array/161699/2 "2018-12-20T13:09:39Z")

</div>

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

```auto
"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
                     }
                 }
             }
         }
     }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 17, 2019, 1:09pm UTC](https://discuss.elastic.co/t/painless-check-length-field-in-each-object-of-array/161699/3 "2019-01-17T13:09:43Z")

</div>

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