即使是不成熟的尝试,也胜于胎死腹中的策略。

Elasticsearch nested 聚合后的排序问题

Elasticsearch | 作者 lixg0817 | 发布于2022年04月06日 | 阅读数:1296

我有一个mapping 详情如下:
{
    "mapping": {
        "comment": {
            "properties": {
                "total": {
                    "type": "double"
                },
                "qiyemingcheng2Eg1": {
                    "type": "nested",
                    "properties": {
                        "id": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}
数据如下
[
{"_id":"56","_source":{"total":"240.0","qiyemingcheng2Eg1":[{"id":"16775432"}]}},
{"_id":"57","_source":{"total":"260.0","qiyemingcheng2Eg1":[{"id":"16775440"}]}},
{"_id":"58","_source":{"total":"238.8","qiyemingcheng2Eg1":[{"id":"16775443"}]}},
{"_id":"59","_source":{"total":"228.0","qiyemingcheng2Eg1":[{"id":"16775447"}]}},
{"_id":"60","_source":{"total":"222.0","qiyemingcheng2Eg1":[{"id":"16775452"}]}},
{"_id":"61","_source":{"total":"222.0","qiyemingcheng2Eg1":[{"id":"16775458"}]}},
{"_id":"62","_source":{"total":"216.0","qiyemingcheng2Eg1":[{"id":"16775462"}]}},
{"_id":"63","_source":{"total":"214.26","qiyemingcheng2Eg1":[{"id":"16775464"}]}}
]
聚合query 如下:
{
    "size": 0,
    "query": {
        "constant_score": {
            "filter": {
                "bool": {
                    "adjust_pure_negative": true,
                    "boost": 1
                }
            },
            "boost": 1
        }
    },
    "aggregations": {
        "qiyemingcheng2Eg1": {
            "nested": {
                "path": "qiyemingcheng2Eg1"
            },
            "aggregations": {
                "qiyemingcheng2Eg1.id": {
                    "terms": {
                        "field": "qiyemingcheng2Eg1.id",
                        "size": 10,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "qiyemingcheng2Eg1": "desc"
                            }
                        ]
                    },
                    "aggregations": {
                        "qiyemingcheng2Eg1": {
                            "reverse_nested": {},
                            "aggregations": {
                                "sum": {
                                    "sum": {
                                        "field": "total"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
结果如下:

{
    "took": 30,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 354,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "qiyemingcheng2Eg1": {
            "doc_count": 354,
            "qiyemingcheng2Eg1.id": {
                "doc_count_error_upper_bound": -1,
                "sum_other_doc_count": 344,
                "buckets": [
                    {
                        "key": 16775432,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 240
                            }
                        }
                    },
                    {
                        "key": 16775433,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 130
                            }
                        }
                    },
                    {
                        "key": 16775440,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 260
                            }
                        }
                    },
                    {
                        "key": 16775441,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 130
                            }
                        }
                    },
                    {
                        "key": 16775443,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 238.8
                            }
                        }
                    },
                    {
                        "key": 16775444,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 130
                            }
                        }
                    },
                    {
                        "key": 16775447,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 228
                            }
                        }
                    },
                    {
                        "key": 16775448,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 130
                            }
                        }
                    },
                    {
                        "key": 16775452,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 222
                            }
                        }
                    },
                    {
                        "key": 16775455,
                        "doc_count": 1,
                        "qiyemingcheng2Eg1": {
                            "doc_count": 1,
                            "sum": {
                                "value": 130
                            }
                        }
                    }
                ]
            }
        }
    }
}
目前看是按照nested的id 进行排序,或者按照count数进行排序,我想按照内层的sum 进行排序,请问应该如何操作查询语句
已邀请:

duanxiaobiao - 90 后IT男

赞同来自:

在你的聚合语句的基础上改动一处地方,order 中的key 改成 qiyemingchenAggs>sum 可以试一下。
 


 

 

要回复问题请先登录注册