您的位置:首页 > 编程语言 > PHP开发

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

2017-11-27 09:53 1006 查看
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查询到的数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch