共计 579 个字符,预计需要花费 2 分钟才能阅读完成。
我想要统计的是 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); | |
} |
正文完