ElasticSearch-如何在聚合查询中显示其他字段名称 [英] ElasticSearch - How to display an additional field name in aggregation query
问题描述
如何在输出存储桶中添加一个名为 agency_name的新密钥。
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"
}
}
}
}
我将得到结果,
"aggregations": {
"name": {
"doc_count_error_upper_bound": 130,
"sum_other_doc_count": 39921,
"buckets": [
{
"key": "1000",
"doc_count": 105163
},
{
"key": "2100",
"doc_count": 43006
}
]
}
}
在显示时,我需要显示代理商名称,代码和doc_count
While displaying I need to show the agency name, code and doc_count
如何修改聚合查询,这样我就可以得到以下格式。我是ElasticSearch的新手,不确定如何解决此问题
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'
}
]
}
}
ElasticSearch中的样本数据(已分析字段)
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.
{
"aggs": {
"name": {
"terms": {
"field": "agency_code"
},
"aggs": {
"agency_names" : {
"top_hits": {
size: 1,
_source: {
include: ['agency_name']
}
}
}
}
}
}
}
这篇关于ElasticSearch-如何在聚合查询中显示其他字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!