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

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