其他分享
首页 > 其他分享> > Yii2 ElasticSearch aggregate (group)

Yii2 ElasticSearch aggregate (group)

作者:互联网

我想要统计的是 country_code 出现的次数,通过 yii2 的 ElasticSearch 扩展,上面的例子满足我的需要。业务场景:在 fecify 商城中,使用 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);
}

 

标签:code,group,agg,country,buckets,aggregate,Yii2,data,size
来源: https://www.cnblogs.com/fecify/p/16615867.html