Sort Data Created via Aggregation

I needed to create an aggregation based on the customer id, but inside the document I have an object with the customer details, like name and email.

Through a code I found, I learned to display in this aggregation the name of the customer that is generating the sum of the data, however, I can't sort these records.

I've tried using the instructions in the documentation, but it still returns the names out of order.

How can I order them?

{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "datecreation": {
                            "gte": "2021-02-10T02:00:00+0000",
                            "lte": "2022-03-11T03:00:00+0000"
                        }
                    }
                },
                {
                    "terms": {
                        "customer_type": [
                            "E",
                            "I",
                            "S"
                        ]
                    }
                }
            ]
        }
    },
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "datagroup": {
            "terms": {
                "field": "customer_id"
            },
            "aggregations": {
                "dedup_docs": {
                    "top_hits": {
                        "size": 1,
                        "_source": [
                            "customer.name",
                            "customer.email"
                        ]
                    }
                }
            }
        }
    }
}

Result:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 8,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "datagroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 2313743,
                    "doc_count": 4,
                    "dedup_docs": {
                        "hits": {
                            "total": 4,
                            "max_score": 2.0,
                            "hits": [
                                {
                                    "_index": "purchases",
                                    "_type": "purchase",
                                    "_id": "123",
                                    "_score": 2.0,
                                    "_source": {
                                        "customer": {
                                            "name": "Louis",
                                            "email": "email@example.com"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": 2313735,
                    "doc_count": 3,
                    "dedup_docs": {
                        "hits": {
                            "total": 3,
                            "max_score": 2.0,
                            "hits": [
                                {
                                    "_index": "purchases",
                                    "_type": "purchase",
                                    "_id": "456",
                                    "_score": 2.0,
                                    "_source": {
                                        "customer": {
                                            "name": "John!",
                                            "email": "email@example.com"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": 2313753,
                    "doc_count": 1,
                    "dedup_docs": {
                        "hits": {
                            "total": 1,
                            "max_score": 2.0,
                            "hits": [
                                {
                                    "_index": "purchases",
                                    "_type": "purchase",
                                    "_id": "789",
                                    "_score": 2.0,
                                    "_source": {
                                        "customer": {
                                            "name": "Albert",
                                            "email": ""
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.