# Terms aggregation not working for a specific field

**URL:** <https://discuss.elastic.co/t/terms-aggregation-not-working-for-a-specific-field/89222>\
**Category:** Elasticsearch\
**Created:** [June 13, 2017, 3:15pm UTC](https://discuss.elastic.co/t/terms-aggregation-not-working-for-a-specific-field/89222 "2017-06-13T15:15:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maheshvyakaranam](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maheshvyakaranam](https://discuss.elastic.co/u/maheshvyakaranam)\
**Post date:** [June 13, 2017, 3:15pm UTC](https://discuss.elastic.co/t/terms-aggregation-not-working-for-a-specific-field/89222/1 "2017-06-13T15:15:06Z")

</div>

Hello Team,

I am doing a terms aggregation on documents that yields me field name and count of it. The query works fine for many fields but not for specific fields.

The query is below and it fetches me the appliance types and their counts in buckets for a given time range. This query does NOT work when i use a different field (say sale.items.item.item\_desc). Could this be a problem with index or something? Can someone please help me get through?

GET market\_v1/appliances/\_search  
{  
"size": 0,  
"aggs": {  
"appliance\_types": {  
"terms": {  
"field": "sale.invoice.appliance\_type"  
}  
}  
},  
"query": {  
"bool": {  
"filter": {  
"range": {  
"sale.summary.create\_date": {  
"gte": "2017-05-01T00:00:00-04:00",  
"lte": "2017-05-03T23:59:59-04:00",  
"format": "date\_time\_no\_millis"  
}  
}  
}  
}  
}  
}

Thank you in advance!  
-Mahesh

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 14, 2017, 7:52am UTC](https://discuss.elastic.co/t/terms-aggregation-not-working-for-a-specific-field/89222/2 "2017-06-14T07:52:31Z")

</div>

Hey,

I suppose that you are trying to use `text` fields (in your mapping) for aggregations which does not work (it should also be mentioned in the response of your request, when you take a look). You can only run aggregations on keyword fields, as a certain data structure is required on disk.

--Alex

---

<div class="post-metadata">

**Author:** ![maheshvyakaranam](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maheshvyakaranam](https://discuss.elastic.co/u/maheshvyakaranam)\
**Post date:** [June 15, 2017, 7:09pm UTC](https://discuss.elastic.co/t/terms-aggregation-not-working-for-a-specific-field/89222/3 "2017-06-15T19:09:55Z")

</div>

the issue doesn't seem to be with the fields. Looks like i had to use a nested path to aggregate the values in the path.  
Below works for me.

GET market\_v1/appliances/\_search  
{  
"size": 0,  
"aggs": {  
"agg\_path": {  
"nested": {  
"path": "sale.items.item"  
},  
"aggs": {  
"item\_type": {  
"terms": {  
"field": "sale.items.item.item\_desc"  
}  
}  
}  
}  
},  
"query": {  
"bool": {  
"filter": {  
"range": {  
"sale.summary.create\_date": {  
"gte": "2017-05-01T00:00:00-04:00",  
"lte": "2017-05-03T23:59:59-04:00",  
"format": "date\_time\_no\_millis"  
}  
}  
}  
}  
}  
}

---

<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 13, 2017, 7:10pm UTC](https://discuss.elastic.co/t/terms-aggregation-not-working-for-a-specific-field/89222/4 "2017-07-13T19:10:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
