Terms aggregation not working for a specific field

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

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

1 Like

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"
}
}
}
}
}
}

1 Like

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