php查找es所有的index

php查询es数据,yii2.0有对应的Query类,根据查询时间可以获取所需要的index和type,但是有时候也会有数据异常的情况下,比如说我查询7月份的数据但是没有7月份的index,yii2.0处理一起查询6月和7月份的数据,7月份索引不存在就返回404,

这样6月份的数据也查询不出来的现象,所以在根据时间获取索引的时候要和es里所有索引做对比,取其交集,这样没有的索引就忽略掉。

下面是我使用的方法,yii2.0封装服方法没找到,最后用的curl
<?php
namespace appmodels;

use Yii;
use yiielasticsearchQuery;

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;
    }
}
原文地址:https://www.cnblogs.com/angellating/p/7115661.html