# Cannot access parent document field in nested document aggration

**URL:** https://discuss.elastic.co/t/cannot-access-parent-document-field-in-nested-document-aggration/18295
**Category:** Elasticsearch
**Created:** [June 24, 2014, 2:58pm UTC](https://discuss.elastic.co/t/cannot-access-parent-document-field-in-nested-document-aggration/18295 "2014-06-24T14:58:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Viacheslav\_Chimishuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viacheslav_chimishuk/32/1450_2.png) [@Viacheslav\_Chimishuk](https://discuss.elastic.co/u/Viacheslav_Chimishuk)
#### Post date: [June 24, 2014, 2:58pm UTC](https://discuss.elastic.co/t/cannot-access-parent-document-field-in-nested-document-aggration/18295/1 "2014-06-24T14:58:45Z")

</div>

Hi, there.

I have a problem with nested documents aggregation. The problem is next.  
Imagine that we have index full of next simple documents

{name: "aaa",  
count: 3,  
companyEmotions: [{  
emotion: 1,  
company: "foo"  
}, {  
emotion: 2,  
company: "bar"  
}]  
}

{name: "bbb",  
count: 10,  
companyEmotions: [{  
emotion: 2,  
company: "foo"  
}, {  
emotion: 2,  
company: "bar"  
}]  
}

and we want to receive sum aggregation of "count" field grouped by emotion  
for some company. Speaking by our example with two documents we want to get  
answer for the question: "what is the sum of "count" for documents where  
foo company emotion is 1, is 2.  
Result buckets should be something like that:

```
      "buckets" : [ {
        "key" : 1, // This is our emotion
        "doc_count" : 1,
        "someAggrName" : {
          "value" : 3.0 // sum of all counts.
        }
      }, {
        "key" : 2,
        "doc_count" : 1,
        "emotionAggr4" : {
          "value" : 10.0
        }
      } ]

```

And here is the mapping which I'm using.

{  
"emotion" : {  
"properties" : {  
"name" : {"type" : "string", "index" : "not\_analyzed" },  
"count" : {"type" : "integer" },  
"companyEmotions" : {  
"type" : "nested",  
"\_parent": {  
"type": "emotion"  
},  
"properties": {  
"company" : {"type" : "string", "index" :  
"not\_analyzed" },  
"emotion" : { "type" : "integer" }  
}  
}  
}  
}  
}

I have experimented a lot with next aggregation request, and can't find the  
way how to refer to the "count" field. I have tried \_parent.count,  
\_doc["\_parent.count"].value, etc. Most of the time, if some error is not  
happen, sum is 0. Ho to correctly refer to the parent "count" field in  
script?  
Or maybe problem in the mapping or something?

curl -XPOST "[http://localhost:9200/emotions/emotion/\_search?pretty=true](http://localhost:9200/emotions/emotion/_search?pretty=true)" -d  
'{  
"aggregations": {  
"emotionAggr": {  
"nested": {  
"path": "companyEmotions"  
},  
"aggregations": {  
"emotionAggr2": {  
"filter": {  
"term": {"companyEmotions.company": "foo"}  
},  
"aggregations": {  
"emotionAggr3": {  
"terms": {  
"field": "companyEmotions.emotion"  
},  
"aggregations": {  
"emotionAggr4": {  
"sum": {"script":  
"doc["count"].value"} // How to reffer \_parent.count here?  
}  
}  
}  
}  
}  
}  
}  
}  
}'

Thanks for helping.

P. S.  
If you want to experiment here is the list of commands which you can  
execute to create an index full of documents.

> <https://gist.github.com/vchimishuk/2c324e2be71c23b7f4c7>

--  
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/2da1f10a-235b-4866-8162-e1bf9846e6c9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2da1f10a-235b-4866-8162-e1bf9846e6c9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Viacheslav\_Chimishuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viacheslav_chimishuk/32/1450_2.png) [@Viacheslav\_Chimishuk](https://discuss.elastic.co/u/Viacheslav_Chimishuk)
#### Post date: [June 26, 2014, 9:44am UTC](https://discuss.elastic.co/t/cannot-access-parent-document-field-in-nested-document-aggration/18295/2 "2014-06-26T09:44:34Z")

</div>

Maybe someone will find it useful.

Finally I've found solution, -- reverse\_nested  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html)  
aggregation (new in 1.2.0) is what I was needed. Here is my example solved.

curl -XPOST "[http://localhost:9200/emotions/emotion/\_search?pretty=true](http://localhost:9200/emotions/emotion/_search?pretty=true)" -d  
'{  
"aggregations": {  
"emotionAggr": {  
"nested": {  
"path": "companyEmotions"  
},  
"aggregations": {  
"emotionAggr2": {  
"filter": {  
"term": {"companyEmotions.company": "foo"}  
},  
"aggregations": {  
"emotionAggr3": {  
"terms": {  
"field": "companyEmotions.emotion"  
},  
"aggregations": {  
"emotionAggr4": {  
"reverse\_nested": {  
},  
"aggregations": {  
"emotionAggr5": {  
"sum": {"script":  
"doc["count"].value + 1"}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}'

Thanks, Viacheslav.

--  
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/48dc4748-e72f-4781-9c83-fb9afd356957%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/48dc4748-e72f-4781-9c83-fb9afd356957%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, 1:19am UTC](https://discuss.elastic.co/t/cannot-access-parent-document-field-in-nested-document-aggration/18295/3 "2017-07-06T01:19:34Z")

</div>


