通过以前的工做,目前已经完成了数据地图的数据格式化和录入记录,目前咱们的数据地图项目已经进行到最后阶段,因此如今须要一个接口,进行格式化数据并输出,其中须要用到Elasticsearch的全文检索,检索出数据后,使用php接口格式化数据输出php
1、全文检索html
例如,一个小时内,在中国范围内,各个经纬度坐标的,有操做行为的,用户个数node
由此需求,能够获得相应的Elasticsearch的搜索语句,以下:json
{
"size": 0,
"aggs": {
"filter_agg": {
"filter": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 90,
"lon": -34.453125
},
"bottom_right": {
"lat": -90,
"lon": 34.453125
}
}
}
},
"aggs": {
"2": {
"geohash_grid": {
"field": "location",
"precision": 2
},
"aggs": {
"3": {
"geo_centroid": {
"field": "location"
}
}
}
}
}
}
},
"stored_fields": [
"*"
],
"docvalue_fields": [
"@timestamp"
],
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": 1542692193461,
"lte": 1542695793461,
"format": "epoch_millis"
}
}
}
]
}
}
}
复制代码
结果数据格式以下:api
{
"took": 428,
"timed_out": false,
"_shards": {
"total": 131,
"successful": 126,
"skipped": 121,
"failed": 5,
"failures": [
{
"shard": 0,
"index": "elastalert_status_status",
"node": "w10b9zEBRpuUEQsWvNqEig",
"reason": {
"type": "query_shard_exception",
"reason": "failed to find geo_point field [location]",
"index_uuid": "Dm4dpUtTTHitYN-TZFC-1g",
"index": "elastalert_status_status"
}
}
]
},
"hits": {
"total": 360942,
"max_score": 0,
"hits": []
},
"aggregations": {
"filter_agg": {
"2": {
"buckets": [
{
"3": {
"location": {
"lat": 48.58949514372008,
"lon": 7.584022147181843
},
"count": 252
},
"key": "u0",
"doc_count": 252
},
{
"3": {
"location": {
"lat": 54.420127907268785,
"lon": -3.120888938036495
},
"count": 181
},
"key": "gc",
"doc_count": 181
},
{
"3": {
"location": {
"lat": 42.32862451614172,
"lon": 3.7518564593602917
},
"count": 67
},
"key": "sp",
"doc_count": 67
},
{
"3": {
"location": {
"lat": 45.40799999143928,
"lon": 11.88589995726943
},
"count": 21
},
"key": "u2",
"doc_count": 21
},
{
"3": {
"location": {
"lat": 46.65579996071756,
"lon": 32.61779992841184
},
"count": 1
},
"key": "u8",
"doc_count": 1
}
]
},
"doc_count": 522
}
}
}
复制代码
自此,咱们首先明白了,在Elasticsearch,如何书写search语句查询咱们想要的东西。 接下来,咱们须要书写相应的php接口,来格式化输出数据bash
2、接口书写数据结构
代码以下,有注释:elasticsearch
<?php
/**
* Created by PhpStorm.
* User: ekisong
* Date: 2018/11/13
* Time: 15:55
*/
require 'vendor/autoload.php';
ini_set('display_errors','on');
error_reporting(E_ALL);
use Elasticsearch\ClientBuilder;
//建立Elasticsearch 的搜索对象client
$client = ClientBuilder::create()->setHosts(["localhost:9200"])->build();
//须要被筛选的字段名,默认值为location
$fieldName = isset($_GET['field']) ? $_GET['field'] : 'location';
//地理围栏左上角纬度,默认值90
$topLeftLat = isset($_GET['top_left_lat']) ? $_GET['top_left_lat'] : 90;
//地理围栏左上角经度,默认值-180
$topLeftLon = isset($_GET['top_left_lon']) ? $_GET['top_left_lon'] : -180;
//地理围栏右下角纬度,默认值-90
$bottomRightLat = isset($_GET['bottom_right_lat']) ? $_GET['bottom_right_lat'] : -90;
//地理围栏右下角经度,默认值180
$bottomRightLon = isset($_GET['bottom_right_lon']) ? $_GET['bottom_right_lon'] : 180;
//时间范围结束时间,默认当前时间
$endTime = isset($_GET['end_time']) ? $_GET['end_time'] : time()*1000;
//时间范围其实时间,默认当前时间前15分钟
$startTime = isset($_GET['start_time']) ? $_GET['start_time'] : $endTime - 15*60*1000;
//建立查询结构体
$body = [
'size' => 0,
'query' => [
'bool' => [
'must' => [
[
'range' => [
'@timestamp' => [
'gte' => $startTime,
'lte' => $endTime,
'format' => 'epoch_millis'
]
]
]
]
]
],
'aggs' => [
'filter_agg' => [
'filter' => [
'geo_bounding_box' => [
'location' => [
'top_left' => [
'lat' => $topLeftLat,
'lon' => $topLeftLon
],
'bottom_right' => [
'lat' => $bottomRightLat,
'lon' => $bottomRightLon
]
]
]
],
'aggs' => [
'2' => [
'geohash_grid' => [
'field' => $fieldName,
'precision' => 1
],
'aggs' => [
'3' => [
'geo_centroid' => [
'field' => $fieldName
]
]
]
]
]
]
],
'stored_fields' => [
'*'
],
'docvalue_fields' => [
'@timestamp'
]
];
//搜索参数
$params = [
'index' => 'logstash-*',
'body' => $body
];
//Elasticsearch搜索结果原始数据
$response = $client->search($params);
$resultTmp = $response['aggregations']['filter_agg']['2']['buckets'];
$data = array();
//格式化数据
foreach ($resultTmp as $doc)
{
$lat = $doc['3'][$fieldName]['lat'];
$lon = $doc['3'][$fieldName]['lon'];
$count = $doc['doc_count'];
$tmp = [
'count' => $count,
'geometry' => [
'type' => 'Point',
'coordinates' => [$lon,$lat]
]
];
$data[] = $tmp;
}
$result = array('data'=>$data,'error_msg'=>'','flag'=>1);
if (empty($data))
{
$result['error_msg'] = 'no data';
$result['flag'] = 0;
}
//最终输出
echo json_encode($result);
exit();
复制代码
因为H5页面插件限制,因此须要特定的数据格式。因此最终输出结果以下:ide
[{
"count": 6,
"geometry": {
"type": "Point",
"coordinates": ["116.395645", "39.929986"]
}
}, {
"count": 6,
"geometry": {
"type": "Point",
"coordinates": ["121.487899", "31.249162"]
}
}, {
"count": 5,
"geometry": {
"type": "Point",
"coordinates": ["117.210813", "39.14393"]
}
}, {
"count": 4,
"geometry": {
"type": "Point",
"coordinates": ["106.530635", "29.544606"]
}
}]
复制代码
至此,咱们数据地图项目在数据方面的工做暂且告一段落。ui
参考文档:
www.elastic.co/guide/en/elasticsearch/reference/current/search.html