# Nested Aggregations

**URL:** https://discuss.elastic.co/t/nested-aggregations/17337
**Category:** Elasticsearch
**Created:** [May 4, 2014, 4:54pm UTC](https://discuss.elastic.co/t/nested-aggregations/17337 "2014-05-04T16:54:10Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Niv\_Penso](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Niv\_Penso](https://discuss.elastic.co/u/Niv_Penso)
#### Post date: [May 4, 2014, 4:54pm UTC](https://discuss.elastic.co/t/nested-aggregations/17337/1 "2014-05-04T16:54:10Z")

</div>

Hey guys,

I have this mappings:

{  
"ckdocuments": {  
"mappings": {  
"msv": {  
"properties": {  
"MSV": {  
"type": "nested",  
"properties": {  
"country": {  
"type": "string",  
"index": "not\_analyzed"  
},  
"date": {  
"type": "date",  
"format": "YYYY-MM-dd HH:mm:ss"  
},  
"hits": {  
"type": "nested",  
"properties": {  
"click\_type": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
}  
}  
},  
"c": {  
"type": "string"  
},  
"doc\_creation\_time": {  
"type": "date",  
"format": "YYYY-MM-dd HH:mm:ss"  
},  
"views": {  
"properties": {  
"country": {  
"type": "string"  
},  
"date": {  
"type": "date",  
"format": "YYYY-MM-dd HH:mm:ss"  
},  
"hits": {  
"properties": {  
"click\_type": {  
"type": "string"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

with this documents:

GET ckdocuments/msv/1

"\_source": {  
"MSV": [  
{  
"country": "US",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
}  
],  
"views": [  
{  
"country": "US",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
},  
{  
"country": "IL",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
}  
]  
}

GET ckdocuments/msv/2

"\_source": {  
"doc\_creation\_time": "2013-01-01 00:00:00",  
"MSV": [  
{  
"country": "IL",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "pixel"  
}  
]  
},  
{  
"country": "US",  
"date": "2013-01-02 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
}  
],  
"views": [  
{  
"country": "US",  
"date": "2013-01-01 00:00:00",  
"hits": []  
},  
{  
"country": "US",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "pixel"  
},  
{  
"click\_type": "pixel"  
}  
]  
},  
{  
"country": "US",  
"date": "2013-01-02 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
}  
]  
}

GET ckdocuments/msv/3

"\_source": {  
"MSV": [  
{  
"country": "IL",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
}  
],  
"views": [  
{  
"country": "US",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
},  
{  
"country": "IL",  
"date": "2013-01-01 00:00:00",  
"hits": [  
{  
"click\_type": "click"  
}  
]  
}  
]  
}

I am trying to make an aggregation query by "MSV.country" with filtering  
that the results will be only the MSVs that have "MSV.hits.click\_type =  
click" AND "MSV.date" is in the range 2013-01-01 2013-01-02 // format  
yyyy-mm-dd

this is the query i am trying to run:

GET ckdocuments/msv/\_search  
{  
"aggregations": {  
"MSV1":{  
"filter":{  
"nested": {  
"path": "MSV",  
"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "MSV.hits",  
"query": {  
"term": {  
"MSV.hits.click\_type": "click"  
}  
}  
}  
},  
{  
"range": {  
"MSV.date": {  
"from":"2013-01-01 00:00:00",  
"to": "2013-01-01 00:00:00"  
}  
}  
}]  
}  
}  
}  
},  
"aggregations":{  
"country":{  
"terms":{  
"field": "MSV.country"  
}  
}  
}  
}  
},  
"size":0  
}

and this is the results:

{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 3,  
"max\_score": 0,  
"hits": []  
},  
"aggregations": {  
"MSV1": {  
"doc\_count": 2,  
"country": {  
"buckets": []  
}  
}  
}  
}

I dont understand why i don't see 1 IL results and 1 US results (as marked  
in green).

Thnx for you attention 😃

Niv

--  
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/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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: [May 4, 2014, 5:20pm UTC](https://discuss.elastic.co/t/nested-aggregations/17337/2 "2014-05-04T17:20:17Z")

</div>

Hi Niv,

MSV is a nested field. Nested fields are effectively stored in separate  
documents, you need to use the nested[1] aggregation in order to make this  
aggregation work. It would look something like (not tested):

"aggregations":{  
"MSV": {  
"nested": {  
"path": "MSV"  
},  
"aggregations": {  
"country":{  
"terms":{  
"field": "MSV.country"  
}  
}  
}  
}  
}

On Sun, May 4, 2014 at 6:54 PM, Niv Penso [nivp@toonimo.com](mailto:nivp@toonimo.com) wrote:

> Hey guys,
> 
> I have this mappings:
> 
> {  
> "ckdocuments": {  
> "mappings": {  
> "msv": {  
> "properties": {  
> "MSV": {  
> "type": "nested",  
> "properties": {  
> "country": {  
> "type": "string",  
> "index": "not\_analyzed"  
> },  
> "date": {  
> "type": "date",  
> "format": "YYYY-MM-dd HH:mm:ss"  
> },  
> "hits": {  
> "type": "nested",  
> "properties": {  
> "click\_type": {  
> "type": "string",  
> "index": "not\_analyzed"  
> }  
> }  
> }  
> }  
> },  
> "c": {  
> "type": "string"  
> },  
> "doc\_creation\_time": {  
> "type": "date",  
> "format": "YYYY-MM-dd HH:mm:ss"  
> },  
> "views": {  
> "properties": {  
> "country": {  
> "type": "string"  
> },  
> "date": {  
> "type": "date",  
> "format": "YYYY-MM-dd HH:mm:ss"  
> },  
> "hits": {  
> "properties": {  
> "click\_type": {  
> "type": "string"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> with this documents:
> 
> GET ckdocuments/msv/1
> 
> "\_source": {  
> "MSV": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ],  
> "views": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> },  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ]  
> }
> 
> GET ckdocuments/msv/2
> 
> "\_source": {  
> "doc\_creation\_time": "2013-01-01 00:00:00",  
> "MSV": [  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "pixel"  
> }  
> ]  
> },  
> {  
> "country": "US",  
> "date": "2013-01-02 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ],  
> "views": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits":   
> },  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "pixel"  
> },  
> {  
> "click\_type": "pixel"  
> }  
> ]  
> },  
> {  
> "country": "US",  
> "date": "2013-01-02 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ]  
> }
> 
> GET ckdocuments/msv/3
> 
> "\_source": {  
> "MSV": [  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ],  
> "views": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> },  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ]  
> }
> 
> I am trying to make an aggregation query by "MSV.country" with filtering  
> that the results will be only the MSVs that have "MSV.hits.click\_type =  
> click" AND "MSV.date" is in the range 2013-01-01 2013-01-02 // format  
> yyyy-mm-dd
> 
> this is the query i am trying to run:
> 
> GET ckdocuments/msv/\_search  
> {  
> "aggregations": {  
> "MSV1":{  
> "filter":{  
> "nested": {  
> "path": "MSV",  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "nested": {  
> "path": "MSV.hits",  
> "query": {  
> "term": {  
> "MSV.hits.click\_type": "click"  
> }  
> }  
> }  
> },  
> {  
> "range": {  
> "MSV.date": {  
> "from":"2013-01-01 00:00:00",  
> "to": "2013-01-01 00:00:00"  
> }  
> }  
> }]  
> }  
> }  
> }  
> },  
> "aggregations":{  
> "country":{  
> "terms":{  
> "field": "MSV.country"  
> }  
> }  
> }  
> }  
> },  
> "size":0  
> }
> 
> and this is the results:
> 
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 3,  
> "max\_score": 0,  
> "hits":   
> },  
> "aggregations": {  
> "MSV1": {  
> "doc\_count": 2,  
> "country": {  
> "buckets":   
> }  
> }  
> }  
> }
> 
> I dont understand why i don't see 1 IL results and 1 US results (as marked  
> in green).
> 
> Thnx for you attention 😃
> 
> Niv
> 
> --  
> 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/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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/CAL6Z4j5z9M87MO7td8TvTwD02Qa9z-POQOUy\_Yw%2BioQ1C3PuoQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5z9M87MO7td8TvTwD02Qa9z-POQOUy_Yw%2BioQ1C3PuoQ%40mail.gmail.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: [May 4, 2014, 5:20pm UTC](https://discuss.elastic.co/t/nested-aggregations/17337/3 "2014-05-04T17:20:39Z")

</div>

Oops, I missed to include the link:

[1]

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Sun, May 4, 2014 at 7:20 PM, Adrien Grand \<[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)

> wrote:

> Hi Niv,
> 
> MSV is a nested field. Nested fields are effectively stored in separate  
> documents, you need to use the nested[1] aggregation in order to make this  
> aggregation work. It would look something like (not tested):
> 
> "aggregations":{  
> "MSV": {  
> "nested": {  
> "path": "MSV"  
> },  
> "aggregations": {  
> "country":{  
> "terms":{  
> "field": "MSV.country"  
> }  
> }  
> }  
> }  
> }
> 
> On Sun, May 4, 2014 at 6:54 PM, Niv Penso [nivp@toonimo.com](mailto:nivp@toonimo.com) wrote:
> 
> > Hey guys,
> > 
> > I have this mappings:
> > 
> > {  
> > "ckdocuments": {  
> > "mappings": {  
> > "msv": {  
> > "properties": {  
> > "MSV": {  
> > "type": "nested",  
> > "properties": {  
> > "country": {  
> > "type": "string",  
> > "index": "not\_analyzed"  
> > },  
> > "date": {  
> > "type": "date",  
> > "format": "YYYY-MM-dd HH:mm:ss"  
> > },  
> > "hits": {  
> > "type": "nested",  
> > "properties": {  
> > "click\_type": {  
> > "type": "string",  
> > "index": "not\_analyzed"  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "c": {  
> > "type": "string"  
> > },  
> > "doc\_creation\_time": {  
> > "type": "date",  
> > "format": "YYYY-MM-dd HH:mm:ss"  
> > },  
> > "views": {  
> > "properties": {  
> > "country": {  
> > "type": "string"  
> > },  
> > "date": {  
> > "type": "date",  
> > "format": "YYYY-MM-dd HH:mm:ss"  
> > },  
> > "hits": {  
> > "properties": {  
> > "click\_type": {  
> > "type": "string"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > with this documents:
> > 
> > GET ckdocuments/msv/1
> > 
> > "\_source": {  
> > "MSV": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ],  
> > "views": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ]  
> > }
> > 
> > GET ckdocuments/msv/2
> > 
> > "\_source": {  
> > "doc\_creation\_time": "2013-01-01 00:00:00",  
> > "MSV": [  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "pixel"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "US",  
> > "date": "2013-01-02 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ],  
> > "views": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits":   
> > },  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "pixel"  
> > },  
> > {  
> > "click\_type": "pixel"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "US",  
> > "date": "2013-01-02 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ]  
> > }
> > 
> > GET ckdocuments/msv/3
> > 
> > "\_source": {  
> > "MSV": [  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ],  
> > "views": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ]  
> > }
> > 
> > I am trying to make an aggregation query by "MSV.country" with filtering  
> > that the results will be only the MSVs that have "MSV.hits.click\_type =  
> > click" AND "MSV.date" is in the range 2013-01-01 2013-01-02 // format  
> > yyyy-mm-dd
> > 
> > this is the query i am trying to run:
> > 
> > GET ckdocuments/msv/\_search  
> > {  
> > "aggregations": {  
> > "MSV1":{  
> > "filter":{  
> > "nested": {  
> > "path": "MSV",  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "nested": {  
> > "path": "MSV.hits",  
> > "query": {  
> > "term": {  
> > "MSV.hits.click\_type": "click"  
> > }  
> > }  
> > }  
> > },  
> > {  
> > "range": {  
> > "MSV.date": {  
> > "from":"2013-01-01 00:00:00",  
> > "to": "2013-01-01 00:00:00"  
> > }  
> > }  
> > }]  
> > }  
> > }  
> > }  
> > },  
> > "aggregations":{  
> > "country":{  
> > "terms":{  
> > "field": "MSV.country"  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "size":0  
> > }
> > 
> > and this is the results:
> > 
> > {  
> > "took": 2,  
> > "timed\_out": false,  
> > "\_shards": {  
> > "total": 5,  
> > "successful": 5,  
> > "failed": 0  
> > },  
> > "hits": {  
> > "total": 3,  
> > "max\_score": 0,  
> > "hits":   
> > },  
> > "aggregations": {  
> > "MSV1": {  
> > "doc\_count": 2,  
> > "country": {  
> > "buckets":   
> > }  
> > }  
> > }  
> > }
> > 
> > I dont understand why i don't see 1 IL results and 1 US results (as  
> > marked in green).
> > 
> > Thnx for you attention 😃
> > 
> > Niv
> > 
> > --  
> > 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/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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

--  
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/CAL6Z4j4RHtYvQ005JbLbXZLRT4sUjFyGhKyXnrd7%2BLZPc150%2Bw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4RHtYvQ005JbLbXZLRT4sUjFyGhKyXnrd7%2BLZPc150%2Bw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Niv\_Penso](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Niv\_Penso](https://discuss.elastic.co/u/Niv_Penso)
#### Post date: [May 5, 2014, 8:05am UTC](https://discuss.elastic.co/t/nested-aggregations/17337/4 "2014-05-05T08:05:52Z")

</div>

Hey Adrian thank you for the quick respone.  
I don't understand how to combine this filter condition in the aggregation:

"bool": {  
"must": [  
{  
"nested": {  
"path": "MSV.hits",  
"query": {  
"term": {  
"MSV.hits.click\_type": "click"  
}  
}  
}  
},  
{  
"range": {  
"MSV.date": {  
"from":"2013-01-01 00:00:00",  
"to": "2013-01-01 00:00:00"  
}  
}  
}]

could you help me plz?

Niv

On Sunday, May 4, 2014 8:20:39 PM UTC+3, Adrien Grand wrote:

> Oops, I missed to include the link:
> 
> [1]  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)
> 
> On Sun, May 4, 2014 at 7:20 PM, Adrien Grand \<[adrien...@elasticsearch.com](mailto:adrien...@elasticsearch.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi Niv,
> > 
> > MSV is a nested field. Nested fields are effectively stored in separate  
> > documents, you need to use the nested[1] aggregation in order to make this  
> > aggregation work. It would look something like (not tested):
> > 
> > "aggregations":{  
> > "MSV": {  
> > "nested": {  
> > "path": "MSV"  
> > },  
> > "aggregations": {  
> > "country":{  
> > "terms":{  
> > "field": "MSV.country"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > On Sun, May 4, 2014 at 6:54 PM, Niv Penso \<[ni...@toonimo.com](mailto:ni...@toonimo.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > Hey guys,
> > > 
> > > I have this mappings:
> > > 
> > > {  
> > > "ckdocuments": {  
> > > "mappings": {  
> > > "msv": {  
> > > "properties": {  
> > > "MSV": {  
> > > "type": "nested",  
> > > "properties": {  
> > > "country": {  
> > > "type": "string",  
> > > "index": "not\_analyzed"  
> > > },  
> > > "date": {  
> > > "type": "date",  
> > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > },  
> > > "hits": {  
> > > "type": "nested",  
> > > "properties": {  
> > > "click\_type": {  
> > > "type": "string",  
> > > "index": "not\_analyzed"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "c": {  
> > > "type": "string"  
> > > },  
> > > "doc\_creation\_time": {  
> > > "type": "date",  
> > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > },  
> > > "views": {  
> > > "properties": {  
> > > "country": {  
> > > "type": "string"  
> > > },  
> > > "date": {  
> > > "type": "date",  
> > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > },  
> > > "hits": {  
> > > "properties": {  
> > > "click\_type": {  
> > > "type": "string"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > with this documents:
> > > 
> > > GET ckdocuments/msv/1
> > > 
> > > "\_source": {  
> > > "MSV": [  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > }  
> > > ],  
> > > "views": [  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > },  
> > > {  
> > > "country": "IL",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > }  
> > > ]  
> > > }
> > > 
> > > GET ckdocuments/msv/2
> > > 
> > > "\_source": {  
> > > "doc\_creation\_time": "2013-01-01 00:00:00",  
> > > "MSV": [  
> > > {  
> > > "country": "IL",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "pixel"  
> > > }  
> > > ]  
> > > },  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-02 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > }  
> > > ],  
> > > "views": [  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits":   
> > > },  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "pixel"  
> > > },  
> > > {  
> > > "click\_type": "pixel"  
> > > }  
> > > ]  
> > > },  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-02 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > }  
> > > ]  
> > > }
> > > 
> > > GET ckdocuments/msv/3
> > > 
> > > "\_source": {  
> > > "MSV": [  
> > > {  
> > > "country": "IL",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > }  
> > > ],  
> > > "views": [  
> > > {  
> > > "country": "US",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > },  
> > > {  
> > > "country": "IL",  
> > > "date": "2013-01-01 00:00:00",  
> > > "hits": [  
> > > {  
> > > "click\_type": "click"  
> > > }  
> > > ]  
> > > }  
> > > ]  
> > > }
> > > 
> > > I am trying to make an aggregation query by "MSV.country" with filtering  
> > > that the results will be only the MSVs that have "MSV.hits.click\_type =  
> > > click" AND "MSV.date" is in the range 2013-01-01 2013-01-02 // format  
> > > yyyy-mm-dd
> > > 
> > > this is the query i am trying to run:
> > > 
> > > GET ckdocuments/msv/\_search  
> > > {  
> > > "aggregations": {  
> > > "MSV1":{  
> > > "filter":{  
> > > "nested": {  
> > > "path": "MSV",  
> > > "query": {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "nested": {  
> > > "path": "MSV.hits",  
> > > "query": {  
> > > "term": {  
> > > "MSV.hits.click\_type":  
> > > "click"  
> > > }  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "range": {  
> > > "MSV.date": {  
> > > "from":"2013-01-01 00:00:00",  
> > > "to": "2013-01-01 00:00:00"  
> > > }  
> > > }  
> > > }]  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "aggregations":{  
> > > "country":{  
> > > "terms":{  
> > > "field": "MSV.country"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "size":0  
> > > }
> > > 
> > > and this is the results:
> > > 
> > > {  
> > > "took": 2,  
> > > "timed\_out": false,  
> > > "\_shards": {  
> > > "total": 5,  
> > > "successful": 5,  
> > > "failed": 0  
> > > },  
> > > "hits": {  
> > > "total": 3,  
> > > "max\_score": 0,  
> > > "hits":   
> > > },  
> > > "aggregations": {  
> > > "MSV1": {  
> > > "doc\_count": 2,  
> > > "country": {  
> > > "buckets":   
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > I dont understand why i don't see 1 IL results and 1 US results (as  
> > > marked in green).
> > > 
> > > Thnx for you attention 😃
> > > 
> > > Niv
> > > 
> > > --  
> > > 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/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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
> 
> --  
> 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/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%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: [May 5, 2014, 8:14am UTC](https://discuss.elastic.co/t/nested-aggregations/17337/5 "2014-05-05T08:14:32Z")

</div>

What would you like to do with this filter? Only compute aggregations on  
documents that match this filter?

On Mon, May 5, 2014 at 10:05 AM, Niv Penso [nivp@toonimo.com](mailto:nivp@toonimo.com) wrote:

> Hey Adrian thank you for the quick respone.  
> I don't understand how to combine this filter condition in the aggregation:
> 
> "bool": {  
> "must": [  
> {  
> "nested": {  
> "path": "MSV.hits",  
> "query": {  
> "term": {  
> "MSV.hits.click\_type": "click"  
> }  
> }  
> }  
> },  
> {  
> "range": {  
> "MSV.date": {  
> "from":"2013-01-01 00:00:00",  
> "to": "2013-01-01 00:00:00"  
> }  
> }  
> }]
> 
> could you help me plz?
> 
> Niv
> 
> On Sunday, May 4, 2014 8:20:39 PM UTC+3, Adrien Grand wrote:
> 
> > Oops, I missed to include the link:
> > 
> > [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/)  
> > reference/current/search-aggregations-bucket-nested-aggregation.html
> > 
> > On Sun, May 4, 2014 at 7:20 PM, Adrien Grand \<[adrien...@elasticsearch.com](mailto:adrien...@elasticsearch.com)
> > 
> > > wrote:
> > 
> > > Hi Niv,
> > > 
> > > MSV is a nested field. Nested fields are effectively stored in separate  
> > > documents, you need to use the nested[1] aggregation in order to make this  
> > > aggregation work. It would look something like (not tested):
> > > 
> > > "aggregations":{  
> > > "MSV": {  
> > > "nested": {  
> > > "path": "MSV"  
> > > },  
> > > "aggregations": {  
> > > "country":{  
> > > "terms":{  
> > > "field": "MSV.country"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > On Sun, May 4, 2014 at 6:54 PM, Niv Penso [ni...@toonimo.com](mailto:ni...@toonimo.com) wrote:
> > > 
> > > > Hey guys,
> > > > 
> > > > I have this mappings:
> > > > 
> > > > {  
> > > > "ckdocuments": {  
> > > > "mappings": {  
> > > > "msv": {  
> > > > "properties": {  
> > > > "MSV": {  
> > > > "type": "nested",  
> > > > "properties": {  
> > > > "country": {  
> > > > "type": "string",  
> > > > "index": "not\_analyzed"  
> > > > },  
> > > > "date": {  
> > > > "type": "date",  
> > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > },  
> > > > "hits": {  
> > > > "type": "nested",  
> > > > "properties": {  
> > > > "click\_type": {  
> > > > "type": "string",  
> > > > "index": "not\_analyzed"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > "c": {  
> > > > "type": "string"  
> > > > },  
> > > > "doc\_creation\_time": {  
> > > > "type": "date",  
> > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > },  
> > > > "views": {  
> > > > "properties": {  
> > > > "country": {  
> > > > "type": "string"  
> > > > },  
> > > > "date": {  
> > > > "type": "date",  
> > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > },  
> > > > "hits": {  
> > > > "properties": {  
> > > > "click\_type": {  
> > > > "type": "string"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > with this documents:
> > > > 
> > > > GET ckdocuments/msv/1
> > > > 
> > > > "\_source": {  
> > > > "MSV": [  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > }  
> > > > ],  
> > > > "views": [  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > },  
> > > > {  
> > > > "country": "IL",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > }  
> > > > ]  
> > > > }
> > > > 
> > > > GET ckdocuments/msv/2
> > > > 
> > > > "\_source": {  
> > > > "doc\_creation\_time": "2013-01-01 00:00:00",  
> > > > "MSV": [  
> > > > {  
> > > > "country": "IL",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "pixel"  
> > > > }  
> > > > ]  
> > > > },  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-02 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > }  
> > > > ],  
> > > > "views": [  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits":   
> > > > },  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "pixel"  
> > > > },  
> > > > {  
> > > > "click\_type": "pixel"  
> > > > }  
> > > > ]  
> > > > },  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-02 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > }  
> > > > ]  
> > > > }
> > > > 
> > > > GET ckdocuments/msv/3
> > > > 
> > > > "\_source": {  
> > > > "MSV": [  
> > > > {  
> > > > "country": "IL",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > }  
> > > > ],  
> > > > "views": [  
> > > > {  
> > > > "country": "US",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > },  
> > > > {  
> > > > "country": "IL",  
> > > > "date": "2013-01-01 00:00:00",  
> > > > "hits": [  
> > > > {  
> > > > "click\_type": "click"  
> > > > }  
> > > > ]  
> > > > }  
> > > > ]  
> > > > }
> > > > 
> > > > I am trying to make an aggregation query by "MSV.country" with  
> > > > filtering that the results will be only the MSVs that have  
> > > > "MSV.hits.click\_type = click" AND "MSV.date" is in the range 2013-01-01  
> > > > 2013-01-02 // format yyyy-mm-dd
> > > > 
> > > > this is the query i am trying to run:
> > > > 
> > > > GET ckdocuments/msv/\_search  
> > > > {  
> > > > "aggregations": {  
> > > > "MSV1":{  
> > > > "filter":{  
> > > > "nested": {  
> > > > "path": "MSV",  
> > > > "query": {  
> > > > "bool": {  
> > > > "must": [  
> > > > {  
> > > > "nested": {  
> > > > "path": "MSV.hits",  
> > > > "query": {  
> > > > "term": {  
> > > > "MSV.hits.click\_type":  
> > > > "click"  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > {  
> > > > "range": {  
> > > > "MSV.date": {  
> > > > "from":"2013-01-01 00:00:00",  
> > > > "to": "2013-01-01 00:00:00"  
> > > > }  
> > > > }  
> > > > }]  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > "aggregations":{  
> > > > "country":{  
> > > > "terms":{  
> > > > "field": "MSV.country"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > "size":0  
> > > > }
> > > > 
> > > > and this is the results:
> > > > 
> > > > {  
> > > > "took": 2,  
> > > > "timed\_out": false,  
> > > > "\_shards": {  
> > > > "total": 5,  
> > > > "successful": 5,  
> > > > "failed": 0  
> > > > },  
> > > > "hits": {  
> > > > "total": 3,  
> > > > "max\_score": 0,  
> > > > "hits":   
> > > > },  
> > > > "aggregations": {  
> > > > "MSV1": {  
> > > > "doc\_count": 2,  
> > > > "country": {  
> > > > "buckets":   
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > I dont understand why i don't see 1 IL results and 1 US results (as  
> > > > marked in green).
> > > > 
> > > > Thnx for you attention 😃
> > > > 
> > > > Niv
> > > > 
> > > > --  
> > > > 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).
> > > > 
> > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%  
> > > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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
> > 
> > --  
> > 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/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%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/CAL6Z4j7RQ4X%3DxvopEMe%2BAoFpnAnH8ViS9u9dZgtRQeLtDM8L5Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7RQ4X%3DxvopEMe%2BAoFpnAnH8ViS9u9dZgtRQeLtDM8L5Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Niv\_Penso](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Niv\_Penso](https://discuss.elastic.co/u/Niv_Penso)
#### Post date: [May 5, 2014, 8:54am UTC](https://discuss.elastic.co/t/nested-aggregations/17337/6 "2014-05-05T08:54:51Z")

</div>

I am trying to count the number of documents that "answers" the filter  
seperatly for each country.

On Monday, May 5, 2014 11:14:32 AM UTC+3, Adrien Grand wrote:

> What would you like to do with this filter? Only compute aggregations on  
> documents that match this filter?
> 
> On Mon, May 5, 2014 at 10:05 AM, Niv Penso \<[ni...@toonimo.com](mailto:ni...@toonimo.com)\<javascript:\>
> 
> > wrote:
> 
> > Hey Adrian thank you for the quick respone.  
> > I don't understand how to combine this filter condition in the  
> > aggregation:
> > 
> > "bool": {  
> > "must": [  
> > {  
> > "nested": {  
> > "path": "MSV.hits",  
> > "query": {  
> > "term": {  
> > "MSV.hits.click\_type":  
> > "click"  
> > }  
> > }  
> > }  
> > },  
> > {  
> > "range": {  
> > "MSV.date": {  
> > "from":"2013-01-01 00:00:00",  
> > "to": "2013-01-01 00:00:00"  
> > }  
> > }  
> > }]
> > 
> > could you help me plz?
> > 
> > Niv
> > 
> > On Sunday, May 4, 2014 8:20:39 PM UTC+3, Adrien Grand wrote:
> > 
> > > Oops, I missed to include the link:
> > > 
> > > [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/)  
> > > reference/current/search-aggregations-bucket-nested-aggregation.html
> > > 
> > > On Sun, May 4, 2014 at 7:20 PM, Adrien Grand \<adrien...@elasticsearch.  
> > > com\> wrote:
> > > 
> > > > Hi Niv,
> > > > 
> > > > MSV is a nested field. Nested fields are effectively stored in separate  
> > > > documents, you need to use the nested[1] aggregation in order to make this  
> > > > aggregation work. It would look something like (not tested):
> > > > 
> > > > "aggregations":{  
> > > > "MSV": {  
> > > > "nested": {  
> > > > "path": "MSV"  
> > > > },  
> > > > "aggregations": {  
> > > > "country":{  
> > > > "terms":{  
> > > > "field": "MSV.country"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > On Sun, May 4, 2014 at 6:54 PM, Niv Penso [ni...@toonimo.com](mailto:ni...@toonimo.com) wrote:
> > > > 
> > > > > Hey guys,
> > > > > 
> > > > > I have this mappings:
> > > > > 
> > > > > {  
> > > > > "ckdocuments": {  
> > > > > "mappings": {  
> > > > > "msv": {  
> > > > > "properties": {  
> > > > > "MSV": {  
> > > > > "type": "nested",  
> > > > > "properties": {  
> > > > > "country": {  
> > > > > "type": "string",  
> > > > > "index": "not\_analyzed"  
> > > > > },  
> > > > > "date": {  
> > > > > "type": "date",  
> > > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > > },  
> > > > > "hits": {  
> > > > > "type": "nested",  
> > > > > "properties": {  
> > > > > "click\_type": {  
> > > > > "type": "string",  
> > > > > "index": "not\_analyzed"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "c": {  
> > > > > "type": "string"  
> > > > > },  
> > > > > "doc\_creation\_time": {  
> > > > > "type": "date",  
> > > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > > },  
> > > > > "views": {  
> > > > > "properties": {  
> > > > > "country": {  
> > > > > "type": "string"  
> > > > > },  
> > > > > "date": {  
> > > > > "type": "date",  
> > > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > > },  
> > > > > "hits": {  
> > > > > "properties": {  
> > > > > "click\_type": {  
> > > > > "type": "string"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > with this documents:
> > > > > 
> > > > > GET ckdocuments/msv/1
> > > > > 
> > > > > "\_source": {  
> > > > > "MSV": [  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ],  
> > > > > "views": [  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > },  
> > > > > {  
> > > > > "country": "IL",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ]  
> > > > > }
> > > > > 
> > > > > GET ckdocuments/msv/2
> > > > > 
> > > > > "\_source": {  
> > > > > "doc\_creation\_time": "2013-01-01 00:00:00",  
> > > > > "MSV": [  
> > > > > {  
> > > > > "country": "IL",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "pixel"  
> > > > > }  
> > > > > ]  
> > > > > },  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-02 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ],  
> > > > > "views": [  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits":   
> > > > > },  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "pixel"  
> > > > > },  
> > > > > {  
> > > > > "click\_type": "pixel"  
> > > > > }  
> > > > > ]  
> > > > > },  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-02 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ]  
> > > > > }
> > > > > 
> > > > > GET ckdocuments/msv/3
> > > > > 
> > > > > "\_source": {  
> > > > > "MSV": [  
> > > > > {  
> > > > > "country": "IL",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ],  
> > > > > "views": [  
> > > > > {  
> > > > > "country": "US",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > },  
> > > > > {  
> > > > > "country": "IL",  
> > > > > "date": "2013-01-01 00:00:00",  
> > > > > "hits": [  
> > > > > {  
> > > > > "click\_type": "click"  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ]  
> > > > > }
> > > > > 
> > > > > I am trying to make an aggregation query by "MSV.country" with  
> > > > > filtering that the results will be only the MSVs that have  
> > > > > "MSV.hits.click\_type = click" AND "MSV.date" is in the range 2013-01-01  
> > > > > 2013-01-02 // format yyyy-mm-dd
> > > > > 
> > > > > this is the query i am trying to run:
> > > > > 
> > > > > GET ckdocuments/msv/\_search  
> > > > > {  
> > > > > "aggregations": {  
> > > > > "MSV1":{  
> > > > > "filter":{  
> > > > > "nested": {  
> > > > > "path": "MSV",  
> > > > > "query": {  
> > > > > "bool": {  
> > > > > "must": [  
> > > > > {  
> > > > > "nested": {  
> > > > > "path": "MSV.hits",  
> > > > > "query": {  
> > > > > "term": {  
> > > > > "MSV.hits.click\_type":  
> > > > > "click"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "range": {  
> > > > > "MSV.date": {  
> > > > > "from":"2013-01-01 00:00:00",  
> > > > > "to": "2013-01-01 00:00:00"  
> > > > > }  
> > > > > }  
> > > > > }]  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "aggregations":{  
> > > > > "country":{  
> > > > > "terms":{  
> > > > > "field": "MSV.country"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "size":0  
> > > > > }
> > > > > 
> > > > > and this is the results:
> > > > > 
> > > > > {  
> > > > > "took": 2,  
> > > > > "timed\_out": false,  
> > > > > "\_shards": {  
> > > > > "total": 5,  
> > > > > "successful": 5,  
> > > > > "failed": 0  
> > > > > },  
> > > > > "hits": {  
> > > > > "total": 3,  
> > > > > "max\_score": 0,  
> > > > > "hits":   
> > > > > },  
> > > > > "aggregations": {  
> > > > > "MSV1": {  
> > > > > "doc\_count": 2,  
> > > > > "country": {  
> > > > > "buckets":   
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > I dont understand why i don't see 1 IL results and 1 US results (as  
> > > > > marked in green).
> > > > > 
> > > > > Thnx for you attention 😃
> > > > > 
> > > > > Niv
> > > > > 
> > > > > --  
> > > > > 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).
> > > > > 
> > > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > > msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%  
> > > > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%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/fc339bf8-095a-4a9c-a41c-978663d16384%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fc339bf8-095a-4a9c-a41c-978663d16384%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: [May 5, 2014, 10:01am UTC](https://discuss.elastic.co/t/nested-aggregations/17337/7 "2014-05-05T10:01:48Z")

</div>

Unfortunately, this can't be done for the moment because you would need the  
upcoming `reverse_nested` aggregation (  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html#search-aggregations-bucket-reverse-nested-aggregation)[https://github.com/elasticsearch/elasticsearch/issues/5485](https://github.com/elasticsearch/elasticsearch/issues/5485))  
that will be part of Elasticsearch 1.2.0, which will be released soon  
hopefully. Once it is out, you could run the following request

"aggs":{  
"MSV": {  
"nested": {  
"path": "MSV"  
},  
"aggs": {  
"country":{  
"terms":{  
"field": "MSV.country"  
},  
"aggs": {  
"root": {  
"reverse\_nested": {},  
"aggs": {  
"filter\_matches": {  
"filter": {  
  
}  
}  
}  
}  
}  
}  
}  
}  
}

On Mon, May 5, 2014 at 10:54 AM, Niv Penso [nivp@toonimo.com](mailto:nivp@toonimo.com) wrote:

> I am trying to count the number of documents that "answers" the filter  
> seperatly for each country.
> 
> On Monday, May 5, 2014 11:14:32 AM UTC+3, Adrien Grand wrote:
> 
> > What would you like to do with this filter? Only compute aggregations on  
> > documents that match this filter?
> > 
> > On Mon, May 5, 2014 at 10:05 AM, Niv Penso [ni...@toonimo.com](mailto:ni...@toonimo.com) wrote:
> > 
> > > Hey Adrian thank you for the quick respone.  
> > > I don't understand how to combine this filter condition in the  
> > > aggregation:
> > > 
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "nested": {  
> > > "path": "MSV.hits",  
> > > "query": {  
> > > "term": {  
> > > "MSV.hits.click\_type":  
> > > "click"  
> > > }  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "range": {  
> > > "MSV.date": {  
> > > "from":"2013-01-01 00:00:00",  
> > > "to": "2013-01-01 00:00:00"  
> > > }  
> > > }  
> > > }]
> > > 
> > > could you help me plz?
> > > 
> > > Niv
> > > 
> > > On Sunday, May 4, 2014 8:20:39 PM UTC+3, Adrien Grand wrote:
> > > 
> > > > Oops, I missed to include the link:
> > > > 
> > > > [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/refe)  
> > > > rence/current/search-aggregations-bucket-nested-aggregation.html
> > > > 
> > > > On Sun, May 4, 2014 at 7:20 PM, Adrien Grand \<adrien...@elasticsearch.  
> > > > com\> wrote:
> > > > 
> > > > > Hi Niv,
> > > > > 
> > > > > MSV is a nested field. Nested fields are effectively stored in  
> > > > > separate documents, you need to use the nested[1] aggregation in order to  
> > > > > make this aggregation work. It would look something like (not tested):
> > > > > 
> > > > > "aggregations":{  
> > > > > "MSV": {  
> > > > > "nested": {  
> > > > > "path": "MSV"  
> > > > > },  
> > > > > "aggregations": {  
> > > > > "country":{  
> > > > > "terms":{  
> > > > > "field": "MSV.country"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > On Sun, May 4, 2014 at 6:54 PM, Niv Penso [ni...@toonimo.com](mailto:ni...@toonimo.com) wrote:
> > > > > 
> > > > > > Hey guys,
> > > > > > 
> > > > > > I have this mappings:
> > > > > > 
> > > > > > {  
> > > > > > "ckdocuments": {  
> > > > > > "mappings": {  
> > > > > > "msv": {  
> > > > > > "properties": {  
> > > > > > "MSV": {  
> > > > > > "type": "nested",  
> > > > > > "properties": {  
> > > > > > "country": {  
> > > > > > "type": "string",  
> > > > > > "index": "not\_analyzed"  
> > > > > > },  
> > > > > > "date": {  
> > > > > > "type": "date",  
> > > > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > > > },  
> > > > > > "hits": {  
> > > > > > "type": "nested",  
> > > > > > "properties": {  
> > > > > > "click\_type": {  
> > > > > > "type": "string",  
> > > > > > "index": "not\_analyzed"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "c": {  
> > > > > > "type": "string"  
> > > > > > },  
> > > > > > "doc\_creation\_time": {  
> > > > > > "type": "date",  
> > > > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > > > },  
> > > > > > "views": {  
> > > > > > "properties": {  
> > > > > > "country": {  
> > > > > > "type": "string"  
> > > > > > },  
> > > > > > "date": {  
> > > > > > "type": "date",  
> > > > > > "format": "YYYY-MM-dd HH:mm:ss"  
> > > > > > },  
> > > > > > "hits": {  
> > > > > > "properties": {  
> > > > > > "click\_type": {  
> > > > > > "type": "string"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > with this documents:
> > > > > > 
> > > > > > GET ckdocuments/msv/1
> > > > > > 
> > > > > > "\_source": {  
> > > > > > "MSV": [  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ],  
> > > > > > "views": [  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > },  
> > > > > > {  
> > > > > > "country": "IL",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ]  
> > > > > > }
> > > > > > 
> > > > > > GET ckdocuments/msv/2
> > > > > > 
> > > > > > "\_source": {  
> > > > > > "doc\_creation\_time": "2013-01-01 00:00:00",  
> > > > > > "MSV": [  
> > > > > > {  
> > > > > > "country": "IL",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "pixel"  
> > > > > > }  
> > > > > > ]  
> > > > > > },  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-02 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ],  
> > > > > > "views": [  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits":   
> > > > > > },  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "pixel"  
> > > > > > },  
> > > > > > {  
> > > > > > "click\_type": "pixel"  
> > > > > > }  
> > > > > > ]  
> > > > > > },  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-02 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ]  
> > > > > > }
> > > > > > 
> > > > > > GET ckdocuments/msv/3
> > > > > > 
> > > > > > "\_source": {  
> > > > > > "MSV": [  
> > > > > > {  
> > > > > > "country": "IL",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ],  
> > > > > > "views": [  
> > > > > > {  
> > > > > > "country": "US",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > },  
> > > > > > {  
> > > > > > "country": "IL",  
> > > > > > "date": "2013-01-01 00:00:00",  
> > > > > > "hits": [  
> > > > > > {  
> > > > > > "click\_type": "click"  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ]  
> > > > > > }
> > > > > > 
> > > > > > I am trying to make an aggregation query by "MSV.country" with  
> > > > > > filtering that the results will be only the MSVs that have  
> > > > > > "MSV.hits.click\_type = click" AND "MSV.date" is in the range 2013-01-01  
> > > > > > 2013-01-02 // format yyyy-mm-dd
> > > > > > 
> > > > > > this is the query i am trying to run:
> > > > > > 
> > > > > > GET ckdocuments/msv/\_search  
> > > > > > {  
> > > > > > "aggregations": {  
> > > > > > "MSV1":{  
> > > > > > "filter":{  
> > > > > > "nested": {  
> > > > > > "path": "MSV",  
> > > > > > "query": {  
> > > > > > "bool": {  
> > > > > > "must": [  
> > > > > > {  
> > > > > > "nested": {  
> > > > > > "path": "MSV.hits",  
> > > > > > "query": {  
> > > > > > "term": {  
> > > > > > "MSV.hits.click\_type":  
> > > > > > "click"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "range": {  
> > > > > > "MSV.date": {  
> > > > > > "from":"2013-01-01 00:00:00",  
> > > > > > "to": "2013-01-01 00:00:00"  
> > > > > > }  
> > > > > > }  
> > > > > > }]  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "aggregations":{  
> > > > > > "country":{  
> > > > > > "terms":{  
> > > > > > "field": "MSV.country"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "size":0  
> > > > > > }
> > > > > > 
> > > > > > and this is the results:
> > > > > > 
> > > > > > {  
> > > > > > "took": 2,  
> > > > > > "timed\_out": false,  
> > > > > > "\_shards": {  
> > > > > > "total": 5,  
> > > > > > "successful": 5,  
> > > > > > "failed": 0  
> > > > > > },  
> > > > > > "hits": {  
> > > > > > "total": 3,  
> > > > > > "max\_score": 0,  
> > > > > > "hits":   
> > > > > > },  
> > > > > > "aggregations": {  
> > > > > > "MSV1": {  
> > > > > > "doc\_count": 2,  
> > > > > > "country": {  
> > > > > > "buckets":   
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > I dont understand why i don't see 1 IL results and 1 US results (as  
> > > > > > marked in green).
> > > > > > 
> > > > > > Thnx for you attention 😃
> > > > > > 
> > > > > > Niv
> > > > > > 
> > > > > > --  
> > > > > > 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).
> > > > > > 
> > > > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > > > msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40goo  
> > > > > > [glegroups.com](http://glegroups.com)[https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/41795a50-5239-4aa8-8a85-9928561ca7fd%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
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%  
> > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8d586065-54ac-4b87-990c-db5b24f00229%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/fc339bf8-095a-4a9c-a41c-978663d16384%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fc339bf8-095a-4a9c-a41c-978663d16384%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/fc339bf8-095a-4a9c-a41c-978663d16384%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/fc339bf8-095a-4a9c-a41c-978663d16384%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/CAL6Z4j5wDGjJ0U2GQK\_7YXvC%2BL7HkGK7mBSGD%2B7deNH-vKEoXw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5wDGjJ0U2GQK_7YXvC%2BL7HkGK7mBSGD%2B7deNH-vKEoXw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Niv\_Penso](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@Niv\_Penso](https://discuss.elastic.co/u/Niv_Penso)
#### Post date: [May 5, 2014, 11:13am UTC](https://discuss.elastic.co/t/nested-aggregations/17337/8 "2014-05-05T11:13:02Z")

</div>

WOW Thnx you helped me so much.

Do you know when version 1.2.0 is going to be released? (weeks, months,  
over half year?)

Niv

On Sunday, May 4, 2014 7:54:10 PM UTC+3, Niv Penso wrote:

> Hey guys,
> 
> I have this mappings:
> 
> {  
> "ckdocuments": {  
> "mappings": {  
> "msv": {  
> "properties": {  
> "MSV": {  
> "type": "nested",  
> "properties": {  
> "country": {  
> "type": "string",  
> "index": "not\_analyzed"  
> },  
> "date": {  
> "type": "date",  
> "format": "YYYY-MM-dd HH:mm:ss"  
> },  
> "hits": {  
> "type": "nested",  
> "properties": {  
> "click\_type": {  
> "type": "string",  
> "index": "not\_analyzed"  
> }  
> }  
> }  
> }  
> },  
> "c": {  
> "type": "string"  
> },  
> "doc\_creation\_time": {  
> "type": "date",  
> "format": "YYYY-MM-dd HH:mm:ss"  
> },  
> "views": {  
> "properties": {  
> "country": {  
> "type": "string"  
> },  
> "date": {  
> "type": "date",  
> "format": "YYYY-MM-dd HH:mm:ss"  
> },  
> "hits": {  
> "properties": {  
> "click\_type": {  
> "type": "string"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> with this documents:
> 
> GET ckdocuments/msv/1
> 
> "\_source": {  
> "MSV": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ],  
> "views": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> },  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ]  
> }
> 
> GET ckdocuments/msv/2
> 
> "\_source": {  
> "doc\_creation\_time": "2013-01-01 00:00:00",  
> "MSV": [  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "pixel"  
> }  
> ]  
> },  
> {  
> "country": "US",  
> "date": "2013-01-02 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ],  
> "views": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits":   
> },  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "pixel"  
> },  
> {  
> "click\_type": "pixel"  
> }  
> ]  
> },  
> {  
> "country": "US",  
> "date": "2013-01-02 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ]  
> }
> 
> GET ckdocuments/msv/3
> 
> "\_source": {  
> "MSV": [  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ],  
> "views": [  
> {  
> "country": "US",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> },  
> {  
> "country": "IL",  
> "date": "2013-01-01 00:00:00",  
> "hits": [  
> {  
> "click\_type": "click"  
> }  
> ]  
> }  
> ]  
> }
> 
> I am trying to make an aggregation query by "MSV.country" with filtering  
> that the results will be only the MSVs that have "MSV.hits.click\_type =  
> click" AND "MSV.date" is in the range 2013-01-01 2013-01-02 // format  
> yyyy-mm-dd
> 
> this is the query i am trying to run:
> 
> GET ckdocuments/msv/\_search  
> {  
> "aggregations": {  
> "MSV1":{  
> "filter":{  
> "nested": {  
> "path": "MSV",  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "nested": {  
> "path": "MSV.hits",  
> "query": {  
> "term": {  
> "MSV.hits.click\_type": "click"  
> }  
> }  
> }  
> },  
> {  
> "range": {  
> "MSV.date": {  
> "from":"2013-01-01 00:00:00",  
> "to": "2013-01-01 00:00:00"  
> }  
> }  
> }]  
> }  
> }  
> }  
> },  
> "aggregations":{  
> "country":{  
> "terms":{  
> "field": "MSV.country"  
> }  
> }  
> }  
> }  
> },  
> "size":0  
> }
> 
> and this is the results:
> 
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 3,  
> "max\_score": 0,  
> "hits":   
> },  
> "aggregations": {  
> "MSV1": {  
> "doc\_count": 2,  
> "country": {  
> "buckets":   
> }  
> }  
> }  
> }
> 
> I dont understand why i don't see 1 IL results and 1 US results (as marked  
> in green).
> 
> Thnx for you attention 😃
> 
> Niv

--  
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/937a5b0b-d79e-4c5c-bd95-5faa4a193e84%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/937a5b0b-d79e-4c5c-bd95-5faa4a193e84%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: [May 5, 2014, 12:55pm UTC](https://discuss.elastic.co/t/nested-aggregations/17337/9 "2014-05-05T12:55:29Z")

</div>

It's a matter of weeks now.

On Mon, May 5, 2014 at 1:13 PM, Niv Penso [nivp@toonimo.com](mailto:nivp@toonimo.com) wrote:

> WOW Thnx you helped me so much.
> 
> Do you know when version 1.2.0 is going to be released? (weeks, months,  
> over half year?)
> 
> Niv
> 
> On Sunday, May 4, 2014 7:54:10 PM UTC+3, Niv Penso wrote:
> 
> > Hey guys,
> > 
> > I have this mappings:
> > 
> > {  
> > "ckdocuments": {  
> > "mappings": {  
> > "msv": {  
> > "properties": {  
> > "MSV": {  
> > "type": "nested",  
> > "properties": {  
> > "country": {  
> > "type": "string",  
> > "index": "not\_analyzed"  
> > },  
> > "date": {  
> > "type": "date",  
> > "format": "YYYY-MM-dd HH:mm:ss"  
> > },  
> > "hits": {  
> > "type": "nested",  
> > "properties": {  
> > "click\_type": {  
> > "type": "string",  
> > "index": "not\_analyzed"  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "c": {  
> > "type": "string"  
> > },  
> > "doc\_creation\_time": {  
> > "type": "date",  
> > "format": "YYYY-MM-dd HH:mm:ss"  
> > },  
> > "views": {  
> > "properties": {  
> > "country": {  
> > "type": "string"  
> > },  
> > "date": {  
> > "type": "date",  
> > "format": "YYYY-MM-dd HH:mm:ss"  
> > },  
> > "hits": {  
> > "properties": {  
> > "click\_type": {  
> > "type": "string"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > with this documents:
> > 
> > GET ckdocuments/msv/1
> > 
> > "\_source": {  
> > "MSV": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ],  
> > "views": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ]  
> > }
> > 
> > GET ckdocuments/msv/2
> > 
> > "\_source": {  
> > "doc\_creation\_time": "2013-01-01 00:00:00",  
> > "MSV": [  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "pixel"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "US",  
> > "date": "2013-01-02 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ],  
> > "views": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits":   
> > },  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "pixel"  
> > },  
> > {  
> > "click\_type": "pixel"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "US",  
> > "date": "2013-01-02 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ]  
> > }
> > 
> > GET ckdocuments/msv/3
> > 
> > "\_source": {  
> > "MSV": [  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ],  
> > "views": [  
> > {  
> > "country": "US",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > },  
> > {  
> > "country": "IL",  
> > "date": "2013-01-01 00:00:00",  
> > "hits": [  
> > {  
> > "click\_type": "click"  
> > }  
> > ]  
> > }  
> > ]  
> > }
> > 
> > I am trying to make an aggregation query by "MSV.country" with filtering  
> > that the results will be only the MSVs that have "MSV.hits.click\_type =  
> > click" AND "MSV.date" is in the range 2013-01-01 2013-01-02 // format  
> > yyyy-mm-dd
> > 
> > this is the query i am trying to run:
> > 
> > GET ckdocuments/msv/\_search  
> > {  
> > "aggregations": {  
> > "MSV1":{  
> > "filter":{  
> > "nested": {  
> > "path": "MSV",  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "nested": {  
> > "path": "MSV.hits",  
> > "query": {  
> > "term": {  
> > "MSV.hits.click\_type": "click"  
> > }  
> > }  
> > }  
> > },  
> > {  
> > "range": {  
> > "MSV.date": {  
> > "from":"2013-01-01 00:00:00",  
> > "to": "2013-01-01 00:00:00"  
> > }  
> > }  
> > }]  
> > }  
> > }  
> > }  
> > },  
> > "aggregations":{  
> > "country":{  
> > "terms":{  
> > "field": "MSV.country"  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "size":0  
> > }
> > 
> > and this is the results:
> > 
> > {  
> > "took": 2,  
> > "timed\_out": false,  
> > "\_shards": {  
> > "total": 5,  
> > "successful": 5,  
> > "failed": 0  
> > },  
> > "hits": {  
> > "total": 3,  
> > "max\_score": 0,  
> > "hits":   
> > },  
> > "aggregations": {  
> > "MSV1": {  
> > "doc\_count": 2,  
> > "country": {  
> > "buckets":   
> > }  
> > }  
> > }  
> > }
> > 
> > I dont understand why i don't see 1 IL results and 1 US results (as  
> > marked in green).
> > 
> > Thnx for you attention 😃
> > 
> > Niv
> > 
> > --  
> > 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/937a5b0b-d79e-4c5c-bd95-5faa4a193e84%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/937a5b0b-d79e-4c5c-bd95-5faa4a193e84%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/937a5b0b-d79e-4c5c-bd95-5faa4a193e84%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/937a5b0b-d79e-4c5c-bd95-5faa4a193e84%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/CAL6Z4j6f5XjeksHnrq%2BUdYT\_7z-wzA5FGp-RP8hEj-yLRHB9jw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6f5XjeksHnrq%2BUdYT_7z-wzA5FGp-RP8hEj-yLRHB9jw%40mail.gmail.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, 1:31am UTC](https://discuss.elastic.co/t/nested-aggregations/17337/10 "2017-07-06T01:31:40Z")

</div>


