# Range filter with array field

**URL:** <https://discuss.elastic.co/t/range-filter-with-array-field/42330>\
**Category:** Elasticsearch\
**Created:** [February 21, 2016, 11:36am UTC](https://discuss.elastic.co/t/range-filter-with-array-field/42330 "2016-02-21T11:36:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maks\_Materkov](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Maks\_Materkov](https://discuss.elastic.co/u/Maks_Materkov)\
**Post date:** [February 21, 2016, 11:36am UTC](https://discuss.elastic.co/t/range-filter-with-array-field/42330/1 "2016-02-21T11:36:01Z")

</div>

Hi,

I've created many documents like this one:

```
{
    "a": [1,5,10,15,20,25,30]
}

```

Now, I want to get all documents, where "at least X elements of `a` between Y and Z".  
So, this example document will match "1 element between 1 and 9" and "2 elements between 1 and 9", but won't match "3 elements between 1 and 9".

I think I should use Range filter first, but what shoud I do next? What filter shoud I use to do this?

Thank you.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [February 21, 2016, 8:15pm UTC](https://discuss.elastic.co/t/range-filter-with-array-field/42330/2 "2016-02-21T20:15:34Z")

</div>

Elasticsearch doesn't support running such a query directly. You could potentially do it if you stored every value in a different field, using a bool query and min\_should\_match. This might however not work well for you depending how many values a document may have at most.

---

<div class="post-metadata">

**Author:** ![Maks\_Materkov](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Maks\_Materkov](https://discuss.elastic.co/u/Maks_Materkov)\
**Post date:** [February 22, 2016, 6:15am UTC](https://discuss.elastic.co/t/range-filter-with-array-field/42330/3 "2016-02-22T06:15:24Z")

</div>

Adrien, thank you for helping.

I'll try to explain the real task a little bit more. We've got a 'users' index with collection of 'user'. Users can perform 'event'. (there are many event types: event1, event2, ...).  
The real task is to find users, who perform event A at least B times between time C and D.

In my first attempt, I've tried to save event timestamps as array for each event type (integers are timestamps) at index 'users'.

We already have users index (about 20 millions of users) and about 500 millions of events in our system right now. I don't want to create another index 'events' with 500 millions of records just for this new query.

Time window usually in days, so I thought about data structure something like this for optimization:

```
[
  {"time_window": "2016-02-01", "event": "event1", "count": 1},
  {"time_window": "2016-02-01", "event": "event2", "count": 5},
  {"time_window": "2016-02-02", "event": "event1", "count": 6},
  {"time_window": "2016-02-02", "event": "event2", "count": 3}
]

```

Any thoughts or suggestions?

Thank you.

---

<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:14pm UTC](https://discuss.elastic.co/t/range-filter-with-array-field/42330/4 "2017-07-05T23:14:33Z")

</div>


