ES中提供了一個數(shù)據(jù)類型 geo_point,這個類型就是用來存儲經(jīng)緯度的。
創(chuàng)建一個帶geo_point類型的索引,并添加測試數(shù)據(jù)
# 創(chuàng)建一個索引,指定一個name,locaiton
PUT /map
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"map": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "geo_point"
}
}
}
}
}
# 添加測試數(shù)據(jù)
PUT /map/map/1
{
"name": "天安門",
"location": {
"lon": 116.403981,
"lat": 39.914492
}
}
PUT /map/map/2
{
"name": "海淀公園",
"location": {
"lon": 116.302509,
"lat": 39.991152
}
}
PUT /map/map/3
{
"name": "北京動物園",
"location": {
"lon": 116.343184,
"lat": 39.947468
}
}
ES的地圖檢索方式
文章來源:http://www.zghlxwxcb.cn/news/detail-517108.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-517108.html
基于RESTful實現(xiàn)地圖檢索
# geo_distance
POST /map/map/_search
{
"query": {
"geo_distance": {
"location": { # 確定一個點
"lon": 116.433733,
"lat": 39.908404
},
"distance": 3000, # 確定半徑
"distance_type": "arc" # 指定形狀為圓形
}
}
}
# geo_bounding_box
POST /map/map/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": { # 左上角的坐標(biāo)點
"lon": 116.326943,
"lat": 39.95499
},
"bottom_right": { # 右下角的坐標(biāo)點
"lon": 116.433446,
"lat": 39.908737
}
}
}
}
}
# geo_polygon
POST /map/map/_search
{
"query": {
"geo_polygon": {
"location": {
"points": [ # 指定多個點確定一個多邊形
{
"lon": 116.298916,
"lat": 39.99878
},
{
"lon": 116.29561,
"lat": 39.972576
},
{
"lon": 116.327661,
"lat": 39.984739
}
]
}
}
}
}
// 基于Java實現(xiàn)geo_polygon查詢
@Test
public void geoPolygon() throws IOException {
//1. SearchRequest
SearchRequest request = new SearchRequest(index);
request.types(type);
//2. 指定檢索方式
SearchSourceBuilder builder = new SearchSourceBuilder();
List<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint(39.99878,116.298916));
points.add(new GeoPoint(39.972576,116.29561));
points.add(new GeoPoint(39.984739,116.327661));
builder.query(QueryBuilders.geoPolygonQuery("location",points));
request.source(builder);
//3. 執(zhí)行查詢
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 輸出結(jié)果
for (SearchHit hit : resp.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
到了這里,關(guān)于ES 地圖經(jīng)緯度搜索的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!