动态

详情 返回 返回

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);
}
user avatar me_life 头像 rui_sen 头像
点赞 2 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.