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

[source, php]
----
$params = [
    'body' => [
        'aggs' => [
            'genres' => [
                'terms' => [
                    'field' => 'genre',
                    'order' => [
                        'playback_stats.max' => 'desc',
                    ],
                ],
                'aggs' => [
                    'playback_stats' => [
                        'stats' => [
                            'field' => 'play_count',
                        ],
                    ],
                ],
            ],
        ],
    ],
];
$response = $client->search($params);
----
