# Range Filter Aggregation on array

**URL:** <https://discuss.elastic.co/t/range-filter-aggregation-on-array/20105>\
**Category:** Elasticsearch\
**Created:** [October 7, 2014, 8:41am UTC](https://discuss.elastic.co/t/range-filter-aggregation-on-array/20105 "2014-10-07T08:41:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Remi\_Nonnon](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@Remi\_Nonnon](https://discuss.elastic.co/u/Remi_Nonnon)\
**Post date:** [October 7, 2014, 8:41am UTC](https://discuss.elastic.co/t/range-filter-aggregation-on-array/20105/1 "2014-10-07T08:41:03Z")

</div>

Hi all,

I have some troubles when I try to use Range Filter aggregation on an array.

Example of 1 document :

{  
"\_index": "test",  
"\_type": "values",  
"\_id": "1",  
"\_version": 1,  
"found": true,  
"\_source": {  
"array": [927,425,455,120]  
}  
}

For all my "values" document, I'd like to count, on "array" field, how many  
numbers are less than 200 and how many are greater than 500.

I tried this aggregation :

GET /test/values/\_search

{  
"aggs" : {  
"less" : {  
"filter":{"range":{"array":{ "lt" : 200}}}  
},  
"greater" : {  
"filter":{"range":{"array":{ "gt" : 500}}}  
}  
}  
}

But the 1st filter count the number of documents which have an array  
containing a value \<200 and the 2nd how many have a value \>500. What I'd  
like is to count, for all documents, how many values (not how many  
document) are \<200 and how many are \>500.

If I make a sum / min / max aggregation, it will be on each value in arrays  
but not with a filter. Do you have an idea how to do that thing?

I did it with 2 script filters, it works, but the computing time is too bad  
:

{  
"aggs" : {  
"less" : {  
"sum":{  
"script":" def sum = 0; doc['array'].values.each(){if(it \< 200) sum++}; return sum;"}  
},  
"greater" : {  
"sum":{  
"script":"def sum = 0; doc['array'].values.each(){if(it \> 500) sum++}; return sum;"}  
}  
}

Any Idea? Thanks!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [October 7, 2014, 9:53am UTC](https://discuss.elastic.co/t/range-filter-aggregation-on-array/20105/2 "2014-10-07T09:53:06Z")

</div>

Hi,

Aggregations can only count documents. So if you want to count values, you  
need to model your data in such a way that each value is going to be a  
document, for instance by using nested documents. Here is an example how  
you could do that:

DELETE test

PUT test  
{  
"mappings": {  
"test": {  
"properties": {  
"array": {  
"type": "nested",  
"properties": {  
"value": {  
"type": "integer"  
}  
}  
}  
}  
}  
}  
}

PUT test/test/1  
{  
"array": [  
{  
"value": 927  
},  
{  
"value": 425  
},  
{  
"value": 455  
},  
{  
"value": 120  
}  
]  
}

GET test/\_search  
{  
"aggs": {  
"array": {  
"nested": {  
"path": "array"  
},  
"aggs": {  
"less\_than\_500": {  
"filter": {  
"range": {  
"array.value": {  
"to": 500  
}  
}  
}  
}  
}  
}  
}  
}

On Tue, Oct 7, 2014 at 10:41 AM, Rémi Nonnon [remi.nonnon@gmail.com](mailto:remi.nonnon@gmail.com) wrote:

> Hi all,
> 
> I have some troubles when I try to use Range Filter aggregation on an  
> array.
> 
> Example of 1 document :
> 
> {  
> "\_index": "test",  
> "\_type": "values",  
> "\_id": "1",  
> "\_version": 1,  
> "found": true,  
> "\_source": {  
> "array": [927,425,455,120]  
> }  
> }
> 
> For all my "values" document, I'd like to count, on "array" field, how  
> many numbers are less than 200 and how many are greater than 500.
> 
> I tried this aggregation :
> 
> GET /test/values/\_search
> 
> {  
> "aggs" : {  
> "less" : {  
> "filter":{"range":{"array":{ "lt" : 200}}}  
> },  
> "greater" : {  
> "filter":{"range":{"array":{ "gt" : 500}}}  
> }  
> }  
> }
> 
> But the 1st filter count the number of documents which have an array  
> containing a value \<200 and the 2nd how many have a value \>500. What I'd  
> like is to count, for all documents, how many values (not how many  
> document) are \<200 and how many are \>500.
> 
> If I make a sum / min / max aggregation, it will be on each value in  
> arrays but not with a filter. Do you have an idea how to do that thing?
> 
> I did it with 2 script filters, it works, but the computing time is too  
> bad :
> 
> {  
> "aggs" : {  
> "less" : {  
> "sum":{  
> "script":" def sum = 0; doc['array'].values.each(){if(it \< 200) sum++}; return sum;"}  
> },  
> "greater" : {  
> "sum":{  
> "script":"def sum = 0; doc['array'].values.each(){if(it \> 500) sum++}; return sum;"}  
> }  
> }
> 
> Any Idea? Thanks!
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5PSruuDE9%3Dm%3DHDOA2o0ZavQuCBmUvr\_%3DdVaYxVtawkUA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5PSruuDE9%3Dm%3DHDOA2o0ZavQuCBmUvr_%3DdVaYxVtawkUA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Remi\_Nonnon](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@Remi\_Nonnon](https://discuss.elastic.co/u/Remi_Nonnon)\
**Post date:** [October 8, 2014, 2:30pm UTC](https://discuss.elastic.co/t/range-filter-aggregation-on-array/20105/3 "2014-10-08T14:30:39Z")

</div>

Hi,

Thanks for your answer. I think it will be the solution.

Le mardi 7 octobre 2014 11:53:18 UTC+2, Adrien Grand a écrit :

> Hi,
> 
> Aggregations can only count documents. So if you want to count values, you  
> need to model your data in such a way that each value is going to be a  
> document, for instance by using nested documents. Here is an example how  
> you could do that:
> 
> DELETE test
> 
> PUT test  
> {  
> "mappings": {  
> "test": {  
> "properties": {  
> "array": {  
> "type": "nested",  
> "properties": {  
> "value": {  
> "type": "integer"  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> PUT test/test/1  
> {  
> "array": [  
> {  
> "value": 927  
> },  
> {  
> "value": 425  
> },  
> {  
> "value": 455  
> },  
> {  
> "value": 120  
> }  
> ]  
> }
> 
> GET test/\_search  
> {  
> "aggs": {  
> "array": {  
> "nested": {  
> "path": "array"  
> },  
> "aggs": {  
> "less\_than\_500": {  
> "filter": {  
> "range": {  
> "array.value": {  
> "to": 500  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> On Tue, Oct 7, 2014 at 10:41 AM, Rémi Nonnon \<[remi....@gmail.com](mailto:remi....@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Hi all,
> > 
> > I have some troubles when I try to use Range Filter aggregation on an  
> > array.
> > 
> > Example of 1 document :
> > 
> > {  
> > "\_index": "test",  
> > "\_type": "values",  
> > "\_id": "1",  
> > "\_version": 1,  
> > "found": true,  
> > "\_source": {  
> > "array": [927,425,455,120]  
> > }  
> > }
> > 
> > For all my "values" document, I'd like to count, on "array" field, how  
> > many numbers are less than 200 and how many are greater than 500.
> > 
> > I tried this aggregation :
> > 
> > GET /test/values/\_search
> > 
> > {  
> > "aggs" : {  
> > "less" : {  
> > "filter":{"range":{"array":{ "lt" : 200}}}  
> > },  
> > "greater" : {  
> > "filter":{"range":{"array":{ "gt" : 500}}}  
> > }  
> > }  
> > }
> > 
> > But the 1st filter count the number of documents which have an array  
> > containing a value \<200 and the 2nd how many have a value \>500. What I'd  
> > like is to count, for all documents, how many values (not how many  
> > document) are \<200 and how many are \>500.
> > 
> > If I make a sum / min / max aggregation, it will be on each value in  
> > arrays but not with a filter. Do you have an idea how to do that thing?
> > 
> > I did it with 2 script filters, it works, but the computing time is too  
> > bad :
> > 
> > {  
> > "aggs" : {  
> > "less" : {  
> > "sum":{  
> > "script":" def sum = 0; doc['array'].values.each(){if(it \< 200) sum++}; return sum;"}  
> > },  
> > "greater" : {  
> > "sum":{  
> > "script":"def sum = 0; doc['array'].values.each(){if(it \> 500) sum++}; return sum;"}  
> > }  
> > }
> > 
> > Any Idea? Thanks!
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/76084de4-472f-4084-9a27-9f158d018043%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eea06154-57d6-4e49-a680-18fe77857be6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eea06154-57d6-4e49-a680-18fe77857be6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:57am UTC](https://discuss.elastic.co/t/range-filter-aggregation-on-array/20105/4 "2017-07-06T00:57:37Z")

</div>


