ElasticSearch - How to display an additional field name in aggregation query -
how can add new key called 'agency_name' in output bucket.
i running aggregation code shown below
{ "aggs": { "name": { "terms": { "field": "agency_code" } } } }
i getting 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 need show agency name, code , doc_count
how can modify aggregation query below format. new elasticsearch, not sure how fix
"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 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 top hits aggregation in link below. format different since creating aggregation embed agency name under 'hits' key.
adding additional fields elasticsearch terms aggregation
{ "aggs": { "name": { "terms": { "field": "agency_code" }, "aggs": { "agency_names" : { "top_hits": { size: 1, _source: { include: ['agency_name'] } } } } } } }
Comments
Post a Comment