如何分桶排序
mapping
{
"index_product" : {
"mappings" : {
"_doc" : {
"properties" : {
"product" : {
"type" : "nested",
"properties" : {
"id" : {
"type" : "integer"
},
"name" : {
"type" : "text"
},
"router" : {
"type" : "keyword"
}
}
}
}
}
}
}
}
数据
POST /index_product/_doc/1
{
"product": [
{
"id": 1,
"name": "Product A",
"router": "z"
},
{
"id": 2,
"name": "Product B",
"router": "c"
},
{
"id": 3,
"name": "Product C",
"router": "e"
}
]
}
需求
我有一个类似上述索引数据结构,目的是想对过ID分桶,然后按router排序,我的思路是如下写的,但是执行不成功,希望高人指点下
GET /index_product/_search
{
"size": 0,
"aggs": {
"nested_products": {
"nested": {
"path": "product"
},
"aggs": {
"product_buckets": {
"terms": {
"field": "product.id",
"size": 10,
"order": {
"router_sort": "asc" // 按router字段的升序排序
}
},
"aggs": {
"router_sort": {
"terms": {
"field": "product.router.keyword", // 使用.keyword进行精确匹配,不分析字符串
"size": 10,
"order": {
"_key": "asc" // 按router字段的升序排序
}
}
}
}
}
}
}
}
}
问题
{
"error": {
"root_cause": [
{
"type": "aggregation_execution_exception",
"reason": "Invalid aggregation order path [router_sort]. Buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "index_product",
"node": "CbXGx8QhRUu6ownsGF9gAA",
"reason": {
"type": "aggregation_execution_exception",
"reason": "Invalid aggregation order path [router_sort]. Buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
}
}
]
},
"status": 500
}
期望
按ID分桶后,再按route字段按字母升序或降序