php使用kibana实例1-----查询数据

来源:互联网 发布:手机购买火车票软件 编辑:程序博客网 时间:2024/06/02 06:33

1.在kibana里面查询出想要的数据,从request里面拿出es代码。如下图


2.在php控制器某个方法里,可以把拿到的es代码拿过去用。

3.下载php中elasticsearch拓展。可以用composer下载。我这里的下载地址:http://download.csdn.net/download/ougexingfuba/10133911

4.把vendor文件放在你的php项目\ThinkPHP\Library\Vendor文件夹下。

5.在php中使用。代码如下(查询代码):

private function getbikes_all($start,$end){$lpath =  THINK_PATH.'Library/Vendor/vendor/autoload.php';require $lpath;$hosts = ['http://xxxxx:8081',         // IP + Port,您的ip加端口号];$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();//从kinaba拿过来的es代码粘贴过来,放在$json里面$json = '{  "size": 0,  "aggs": {    "2": {      "date_histogram": {        "field": "timestamp",        "interval": "1d",        "time_zone": "Asia/Shanghai",        "min_doc_count": 1      },      "aggs": {        "3": {          "terms": {            "field": "company",            "size": 10,            "order": {              "_term": "desc"            }          }        }      }    }  },  "version": true,  "query": {    "bool": {      "must": [        {          "match_all": {}        },        {          "match_phrase": {            "_type": {              "query": "dbs_realtime_first"            }          }        },        {          "range": {            "timestamp": {              "gte": "'.$start.'",              "lte": "'.$end.'",              "format": "epoch_millis"            }          }        }      ],      "must_not": [        {          "match_phrase": {            "company": {              "query": "其他"            }          }        }      ]    }  },  "_source": {    "excludes": []  },  "highlight": {    "pre_tags": [      "@kibana-highlighted-field@"    ],    "post_tags": [      "@/kibana-highlighted-field@"    ],    "fields": {      "*": {        "highlight_query": {          "bool": {            "must": [              {                "match_all": {}              },              {                "match_phrase": {                  "_type": {                    "query": "dbs_realtime_first"                  }                }              },              {                "range": {                  "timestamp": {                    "gte": "'.$start.'",                    "lte": "'.$end.'",                    "format": "epoch_millis"                  }                }              }            ],            "must_not": [              {                "match_phrase": {                  "company": {                    "query": "其他"                  }                }              }            ]          }        }      }    },    "fragment_size": 2147483647  }}';$params = ['index' => 'bike_index_v6','type' => 'dbs_realtime_first','body' => $json];$results = $client->search($params);//$ts = $results['hits']['hits'][0]['_source']['ts'];//var_dump($results);//var_dump($ts);return $results;}

6.直接调用这个方法就可以获取到从kibana查询到的数据。

原创粉丝点击