// This file is autogenerated, DO NOT EDIT
// aggregations/bucket/terms-aggregation.asciidoc:365

[source, python]
----
resp = client.search(
    aggs={
        "countries": {
            "terms": {
                "field": "artist.country",
                "order": [
                    {
                        "rock>playback_stats.avg": "desc"
                    },
                    {
                        "_count": "desc"
                    }
                ]
            },
            "aggs": {
                "rock": {
                    "filter": {
                        "term": {
                            "genre": "rock"
                        }
                    },
                    "aggs": {
                        "playback_stats": {
                            "stats": {
                                "field": "play_count"
                            }
                        }
                    }
                }
            }
        }
    },
)
print(resp)
----
