Yii2 ElasticSearch aggregate (group) 的例子

直接上代码:

public function actionCountry(){
    $size = 5000;
    $name = 'country_code';
    $type = 'terms';
    $options = [
      'field' => 'country_code',
      'size'  => $size,
    ];
    
    $data = WholeCountryData::find()
        //->limit(5000)
        //->offset(0)
        ->asArray()  
        ->addAgg($name, $type, $options)
        ->createCommand()
        ->search();
    $agg_data = $data['aggregations'];
    $buckets  = $agg_data['country_code']['buckets'];
    //var_dump($agg_data);exit;
    $country_code_arr = \yii\helpers\BaseArrayHelper::getColumn($buckets,'key');
    var_dump($country_code_arr);
    
  }

我想要统计的是country_code 出现的次数,通过yii2的ElasticSearch扩展,上面的例子满足我的需要。