# Aggregation with nested objects/

**URL:** <https://discuss.elastic.co/t/aggregation-with-nested-objects/22203>\
**Category:** Elasticsearch\
**Created:** [February 17, 2015, 7:24am UTC](https://discuss.elastic.co/t/aggregation-with-nested-objects/22203 "2015-02-17T07:24:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grigory\_Rubstein](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grigory_rubstein/32/793_2.png) [@Grigory\_Rubstein](https://discuss.elastic.co/u/Grigory_Rubstein)\
**Post date:** [February 17, 2015, 7:24am UTC](https://discuss.elastic.co/t/aggregation-with-nested-objects/22203/1 "2015-02-17T07:24:40Z")

</div>

Hello

Few word about the schema , i have one type of document (Reviews) that  
contain list of reviews (nested object) each review has folowing fields :  
_polarity_(negative or opposite ), _keyword_(main word of the review) , \*reviewer  
\*.  
my goal is to find top negative and positive keyword, and for each keyword  
find the count of its opposite(if the keyword was on top positive ,i need  
to find negative count for the keyword)

for example (based on data presented below)

top negative  
iphone - 2  
opposite count (positive) - 2  
samsung - 1  
opposite count(positive) - 0  
top positive  
iphone - 2  
opposite count (negative) - 2

Thank you in advance for you time.

_The schema :_  
curl -XPOST "[http://localhost:9200/forum\_poc](http://localhost:9200/forum_poc)" -d '  
{  
"settings": {  
"number\_of\_shards": 9,  
"number\_of\_replicas": 1  
},  
"mappings": {  
"_default_": {  
"\_all": {  
"enabled": false  
},  
"\_source": {  
"enabled": true  
},  
"dynamic": "false"  
},  
"ReviewEvent": {

```
  "_source": {
    "enabled": true
  },
  "properties": {
    "Reviews": {
      "type": "nested",
      "include_in_parent": true,
      "properties": {
        "polarity": {
          "type": "string",
          "index": "not_analyzed",
          "store": "true"
        },
        "reviewer": {
          "type": "string",
          "index": "not_analyzed",
          "store": "true"
        },
        "keyword": {
          "type": "string",
          "index": "not_analyzed",
          "store": "true"
        }
      }
    }
  }
}

```

}  
}'  
}

_The Data :_

curl -XPOST "[http://localhost:9200/forum\_poc](http://localhost:9200/forum_poc)" -d '  
{"index":{"\_index":"forum\_poc","\_type":"ReviewEvent","\_id":0}}  
{"Reviews":[{"polarity":"negative","reviewer":"jhon","keyword":"iphone"},{"polarity":"negative","reviewer":"kevin","keyword":"samsung"}]}  
{"index":{"\_index":"forum\_poc","\_type":"ReviewEvent","\_id":1}}  
{"Reviews":[{"polarity":"positive","reviewer":"Doron","keyword":"iphone"}]}  
{"index":{"\_index":"forum\_poc","\_type":"ReviewEvent","\_id":2}}  
{"Reviews":[{"polarity":"negative","reviewer":"Michel","keyword":"iphone"}]}  
{"index":{"\_index":"forum\_poc","\_type":"ReviewEvent","\_id":4}}  
{"Reviews":[{"polarity":"positive","reviewer":"Afi","keyword":"iphone"}]}  
'

_My query:_

POST forum\_poc/\_search?search\_type=count  
{  
"aggs": {  
"aggregation": {  
"nested": {  
"path": "Reviews"  
},  
"aggs": {  
"polarity": {  
"terms": {  
"field": "polarity",  
"size": 10  
},  
"aggs": {  
"keyword": {  
"terms": {  
"field": "keyword",  
"size": 10  
}  
}  
}  
}  
}  
}  
}  
}

i need the opposite count for each keyword.

{  
"took": 7,  
"timed\_out": false,  
"\_shards": {  
"total": 9,  
"successful": 9,  
"failed": 0  
},  
"hits": {  
"total": 4,  
"max\_score": 0,  
"hits": []  
},  
"aggregations": {  
"aggregation": {  
"doc\_count": 5,  
"polarity": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "negative",  
"doc\_count": 3,  
"keyword": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "iphone",  
"doc\_count": 2  
},  
{  
"key": "samsung",  
"doc\_count": 1  
}  
]  
}  
},  
{  
"key": "positive",  
"doc\_count": 2,  
"keyword": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "iphone",  
"doc\_count": 2  
}  
]  
}  
}  
]  
}  
}  
}

--  
This message may contain confidential and/or privileged information.  
If you are not the addressee or authorized to receive this on behalf of the  
addressee you must not use, copy, disclose or take action based on this  
message or any information herein.  
If you have received this message in error, please advise the sender  
immediately by reply email and delete this message. Thank you.

--  
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/8b770e85-8003-4e2d-b084-9ffb688e45d6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8b770e85-8003-4e2d-b084-9ffb688e45d6%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:32am UTC](https://discuss.elastic.co/t/aggregation-with-nested-objects/22203/2 "2017-07-06T00:32:14Z")

</div>

Closed on July 6, 2017, 12:32am UTC


