php查詢es數據,yii2.0有對應的Query類,根據查詢時間可以獲取所需要的index和type,但是有時候也會有數據異常的情況下,比如說我查詢7月份的數據但是沒有7月份的index,yii2.0處理一起查詢6月和7月份的數據,7月份索引不存在就返回404,
這樣6月份的數據也查詢不出來的現象,所以在根據時間獲取索引的時候要和es里所有索引做對比,取其交集,這樣沒有的索引就忽略掉。
下面是我使用的方法,yii2.0封裝服方法沒找到,最后用的curl
<?php namespace app\models; use Yii; use yii\elasticsearch\Query; class Index_data_query extends Query { /* * 返回所有index */ public function getAllindex(){ $rows = $this->createCommand();//使用query類連接配置的集群,獲取節點 $rows = json_decode(json_encode( $rows),true); $nodes=$rows['db']['nodes']; $output = array(); $index_arr = array(); foreach ($nodes as $k=>$v){ $url = $v['protocol']."://".$v['http_address']."/_cat/indices?format=json";//以json的格式返回所有節點下面所有的index相關信息 $ch = curl_init($url) ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數據返回 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在啟用 CURLOPT_RETURNTRANSFER 時候將獲取數據返回 $output[] = curl_exec($ch) ; } if($output){ foreach ($output as $kk=>$vv){ $info_arr = json_decode($vv,true); if($info_arr){ foreach ($info_arr as $i_k=>$i_v){ $index_arr[] = $i_v['index'];//只獲取index的名稱 } } } } $index_arr = array_unique($index_arr); return $index_arr; } }