How can I add a new key called 'agency_name' in my output bucket.
I am running an aggregation code as shown below
{
"aggs": {
"name": {
"terms": {
"field": "agency_code"
}
}
}
}
I will be getting the out put as
"aggregations": {
"name": {
"doc_count_error_upper_bound": 130,
"sum_other_doc_count": 39921,
"buckets": [
{
"key": "1000",
"doc_count": 105163
},
{
"key": "2100",
"doc_count": 43006
}
]
}
}
While displaying I need to show the agency name, code and doc_count
How can I modify the aggregation query so that I could get the below format. I am new to ElasticSearch, not sure how to fix this
"aggregations": {
"name": {
"doc_count_error_upper_bound": 130,
"sum_other_doc_count": 39921,
"buckets": [
{
"key": "1000",
"doc_count": 105163,
"agency_name": 'Agent 1'
},
{
"key": "2100",
"doc_count": 43006,
"agency_name": 'Agent 2'
}
]
}
}
Sample Data in ElasticSearch (fields are analysed)
{
"_index": "feeds",
"_type": "news",
"_id": "22005",
"_version": 1,
"_score": 1,
"_source": {
"id": 22005,
"name": "Test News",
"agency_name": "Agent 1",
"agency_code": "1000",
}
}
You can use the top hits aggregation like in the link below. The format will be slightly different since creating the extra aggregation will embed the agency name under another 'hits' key.
Adding additional fields to ElasticSearch terms aggregation
{
"aggs": {
"name": {
"terms": {
"field": "agency_code"
},
"aggs": {
"agency_names" : {
"top_hits": {
size: 1,
_source: {
include: ['agency_name']
}
}
}
}
}
}
}