# Painless test on Array length

**URL:** <https://discuss.elastic.co/t/painless-test-on-array-length/116494>\
**Category:** Elasticsearch\
**Created:** [January 22, 2018, 2:25pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494 "2018-01-22T14:25:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgruhier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sgruhier/32/26802_2.png) [@sgruhier](https://discuss.elastic.co/u/sgruhier)\
**Post date:** [January 22, 2018, 2:25pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/1 "2018-01-22T14:25:09Z")

</div>

Hi,  
I'm trying to display only documents with more than one value in an array.  
My naïve approach was to do

```
    {
        "query": {
            "bool": {
                "must": {
                    "script": {
                        "script": {
                            "inline": "doc['srcPortsMap'].values.length > 1 ",
                            "lang": "painless"
                        }
                    }
                }
            }
        }
    }

```

But I got that error: No field found for [srcPortsMap] in mapping with types []

but mapping exists

```
      "srcPortsMap": {
        "properties": {
          "count": {
            "type": "long"
          },
          "port": {
            "type": "long"
          }
        }
      },

```

any idea?  
Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2018, 2:55pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/2 "2018-01-22T14:55:41Z")

</div>

Hey Sebastien!

I'd compute that at index time instead of having to compute that at search time which is going to be slow as it has to go through all the hits. Specifically here that you have a `match_all` query.

You can do that with ingest and an ingest script processor on on your side (even better).

BTW does your field exist for all documents?

---

<div class="post-metadata">

**Author:** ![sgruhier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sgruhier/32/26802_2.png) [@sgruhier](https://discuss.elastic.co/u/sgruhier)\
**Post date:** [January 22, 2018, 4:20pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/3 "2018-01-22T16:20:20Z")

</div>

Thanks for the advice

Yes it exists on all documents, but it can be a single value or an array of values

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 22, 2018, 4:47pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/4 "2018-01-22T16:47:58Z")

</div>

You should double check all docs actually have the srcPortMap field. It will only show as missing if the doc being evaluated does not have a value for the field.

But calculating this at index time will be _much_ more efficient, as David suggested.

---

<div class="post-metadata">

**Author:** ![sgruhier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sgruhier/32/26802_2.png) [@sgruhier](https://discuss.elastic.co/u/sgruhier)\
**Post date:** [January 23, 2018, 7:52am UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/5 "2018-01-23T07:52:37Z")

</div>

Thanks for your help  
I'll do it at index time. In the mean time, is there a way in painless to check if field srcPortsMap exists?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 23, 2018, 5:29pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/6 "2018-01-23T17:29:17Z")

</div>

Yes. `doc` is a Java `Map`, so you can use `containsKey`:

```auto
if (doc.containsKey('srcPortsMap')) {
   /* do something with doc['srcPortsMap'] */
} else {
  /* field does not exist */
}

```

---

<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:** [February 20, 2018, 5:29pm UTC](https://discuss.elastic.co/t/painless-test-on-array-length/116494/7 "2018-02-20T17:29:26Z")

</div>

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