# Elasticsearch filtering by the size of a nested field that is an array

**URL:** https://discuss.elastic.co/t/elasticsearch-filtering-by-the-size-of-a-nested-field-that-is-an-array/28915
**Category:** Elasticsearch
**Created:** [September 9, 2015, 8:58am UTC](https://discuss.elastic.co/t/elasticsearch-filtering-by-the-size-of-a-nested-field-that-is-an-array/28915 "2015-09-09T08:58:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Juan\_Diaz\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Juan\_Diaz\_Gonzalez](https://discuss.elastic.co/u/Juan_Diaz_Gonzalez)
#### Post date: [September 9, 2015, 8:58am UTC](https://discuss.elastic.co/t/elasticsearch-filtering-by-the-size-of-a-nested-field-that-is-an-array/28915/1 "2015-09-09T08:58:44Z")

</div>

How can I filter documents that have a nested field which is an array and has more than N elements?

For example:

```
"oid": "oid:user:my_user",
"updated_at": "2015-09-09T08:27:58.121216",
"versions": [
                  {  
                      properties version 1
                     },
                  { 
                     properties version 2
                  }
               ]
            }

```

How can I filter documents that have a field which is an empty array?

Is facets the solution? If so, how?

---

<div class="post-metadata">

### Author: ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)
#### Post date: [September 10, 2015, 5:27pm UTC](https://discuss.elastic.co/t/elasticsearch-filtering-by-the-size-of-a-nested-field-that-is-an-array/28915/2 "2015-09-10T17:27:56Z")

</div>

Could you please clarify what type of filter you would need? also not sure what you mean when you say that the array has more than N elements. Can you clarify that too?

---

<div class="post-metadata">

### Author: ![Juan\_Diaz\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Juan\_Diaz\_Gonzalez](https://discuss.elastic.co/u/Juan_Diaz_Gonzalez)
#### Post date: [September 11, 2015, 6:45am UTC](https://discuss.elastic.co/t/elasticsearch-filtering-by-the-size-of-a-nested-field-that-is-an-array/28915/3 "2015-09-11T06:45:18Z")

</div>

yes, I would llike the filter gives me inside the type the elements (that they are composed by arrays with N elements) the elements that have more than 1 element for example.

Finally I decided by using the script filter putting my script inside the scripts dir, that it is inside config elasticsearch dir.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-filter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-filter.html)

---

<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: [July 5, 2017, 11:50pm UTC](https://discuss.elastic.co/t/elasticsearch-filtering-by-the-size-of-a-nested-field-that-is-an-array/28915/4 "2017-07-05T23:50:57Z")

</div>


