# Equivalent aggregation query to terms facet

**URL:** <https://discuss.elastic.co/t/equivalent-aggregation-query-to-terms-facet/19952>\
**Category:** Elasticsearch\
**Created:** [September 24, 2014, 9:41pm UTC](https://discuss.elastic.co/t/equivalent-aggregation-query-to-terms-facet/19952 "2014-09-24T21:41:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [September 24, 2014, 9:41pm UTC](https://discuss.elastic.co/t/equivalent-aggregation-query-to-terms-facet/19952/1 "2014-09-24T21:41:17Z")

</div>

Hi all,

I'm using ES 1.3.2  
My document structure

```
            {
               "mapids" : [mapid1, mapid2,mapid3],
               "rdata" : [ {mapid1: value1}, 

```

{mapid2:value2},{mapid3:value3} ]  
}

We are trying to migrate to the new aggregation framework and I'm trying to  
rewrite terms facet which was

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"must": [  
{  
"terms": {  
"mapids": [  
0,  
77188  
],  
"execution": "and"  
}  
}  
]  
}  
}  
}  
},  
"facets": {  
"facet1": {  
"terms": {  
"field": "value"  
},  
"nested": "rdata",  
"facet\_filter": {  
"term": {  
"mapid": "77188"  
}  
}  
}  
}  
}

Which comes back with the buckets

- terms: [
  - {
    - term: 10
    - count: 257998  
}

  - ...
  - ....
  - {
    - term: 4
    - count: 28477  
}  
]

I was trying to rewrite the same query using the aggregation framework but  
it comes back Parse Failure [Found two aggregation type definitions in  
[rdata]: [filter] and [nested]]]

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"must": [  
{  
"terms": {  
"mapids": [  
0,  
77188,  
77200  
],  
"execution": "and"  
}  
}  
]  
}  
}  
}  
},  
"aggs": {  
"rdata": {  
"filter": {  
"term": {  
"mapid": "77188"  
}  
},  
"nested": {  
"path": "rdata"  
},  
"aggs": {  
"rdata\_value": {  
"terms": {  
"field": "value"  
}  
}  
}  
}  
}  
}

Any help is appreciated  
Thanks,  
Abhishek

--  
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/b7cc4b9b-7767-46b9-a8ab-ff4159b5f502%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b7cc4b9b-7767-46b9-a8ab-ff4159b5f502%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [September 27, 2014, 5:54am UTC](https://discuss.elastic.co/t/equivalent-aggregation-query-to-terms-facet/19952/2 "2014-09-27T05:54:24Z")

</div>

> I think I figured it out. Each aggs sections sub divides the data. First  
> aggs buckets all the nested docs. Second aggs restricts the bucket to a  
> filter. Third aggs run the terms facet on it.

"aggs": {  
"foo": {  
"nested": {  
"path": "rdata"  
},  
"aggs": {  
"bar": {  
"filter": {  
"term": {  
"rdata.mapid": 37  
}  
},  
"aggs": {  
"foobar": {  
"terms": {  
"field": "rdata.value",  
"size": 28654,  
"order": {  
"\_count": "desc"  
}  
}  
}  
}  
}  
}  
}  
}

--  
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/e0b49cd3-95c9-4060-851a-739073140ea4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e0b49cd3-95c9-4060-851a-739073140ea4%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:59am UTC](https://discuss.elastic.co/t/equivalent-aggregation-query-to-terms-facet/19952/3 "2017-07-06T00:59:38Z")

</div>


