// aggregations/bucket/terms-aggregation.asciidoc:309

[source, php]
----
$params = [
    'body' => [
        'aggs' => [
            'countries' => [
                'terms' => [
                    'field' => 'artist.country',
                    'order' => [
                        'rock>playback_stats.avg' => 'desc',
                    ],
                ],
                'aggs' => [
                    'rock' => [
                        'filter' => [
                            'term' => [
                                'genre' => 'rock',
                            ],
                        ],
                        'aggs' => [
                            'playback_stats' => [
                                'stats' => [
                                    'field' => 'play_count',
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];
$response = $client->search($params);
----
