使用 Elasticsearch 聚合时获取描述
Getting description when aggregating with Elasticsearch
当我们在 elastic 上使用聚合功能时,我们得到了我们聚合返回的字段的值,但我们也想获得该字段的描述。我们必须使用 sector.id
,因为我们 api 的其他部分稍后会用到它。
例如:我们的数据如下所示:
[{
"id":"123"
"sectors":[{
"id":"sector-1",
"name":"Automotive"
}]
},
{
"id":"123"
"sectors":[{
"id":"sector-2",
"name":"Biology"
}]
}]
当我们汇总 sectors.id
时,我们的响应如下:
"aggregations": {
"sector": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "sector-2",
"doc_count": 19672
},
{
"key": "sector-1",
"doc_count": 11699
}]
}
}
有什么办法可以得到sectors.name
以及结果中的key吗?
似乎 sectors
应该是一个嵌套字段。现在假设每个扇区 ID 的扇区名称是唯一的。
您可以使用sub-aggregations找出相关键
GET _search
{
"size": 0,
"aggs": {
"sectors": {
"nested": {
"path": "sectors"
},
"aggs": {
"sector_id": {
"terms": {
"field": "sectors.id"
},
"aggs": {
"sector_name": {
"terms": {
"field": "sectors.name"
}
}
}
}
}
}
}
}
当我们在 elastic 上使用聚合功能时,我们得到了我们聚合返回的字段的值,但我们也想获得该字段的描述。我们必须使用 sector.id
,因为我们 api 的其他部分稍后会用到它。
例如:我们的数据如下所示:
[{
"id":"123"
"sectors":[{
"id":"sector-1",
"name":"Automotive"
}]
},
{
"id":"123"
"sectors":[{
"id":"sector-2",
"name":"Biology"
}]
}]
当我们汇总 sectors.id
时,我们的响应如下:
"aggregations": {
"sector": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "sector-2",
"doc_count": 19672
},
{
"key": "sector-1",
"doc_count": 11699
}]
}
}
有什么办法可以得到sectors.name
以及结果中的key吗?
似乎 sectors
应该是一个嵌套字段。现在假设每个扇区 ID 的扇区名称是唯一的。
您可以使用sub-aggregations找出相关键
GET _search
{
"size": 0,
"aggs": {
"sectors": {
"nested": {
"path": "sectors"
},
"aggs": {
"sector_id": {
"terms": {
"field": "sectors.id"
},
"aggs": {
"sector_name": {
"terms": {
"field": "sectors.name"
}
}
}
}
}
}
}
}