// getting-started.asciidoc:691

[source, php]
----
$params = [
    'index' => 'bank',
    'body' => [
        'size' => 0,
        'aggs' => [
            'group_by_state' => [
                'terms' => [
                    'field' => 'state.keyword',
                    'order' => [
                        'average_balance' => 'desc',
                    ],
                ],
                'aggs' => [
                    'average_balance' => [
                        'avg' => [
                            'field' => 'balance',
                        ],
                    ],
                ],
            ],
        ],
    ],
];
$response = $client->search($params);
----
