ES中提供了一个数据类型 geo_point,这个类型就是用来存储经纬度的。
创建一个带geo_point类型的索引,并添加测试数据
# 创建一个索引,指定一个name,locaiton
PUT /map
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"map": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "geo_point"
}
}
}
}
}
# 添加测试数据
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的地图检索方式
文章来源:https://www.toymoban.com/news/detail-517108.html
文章来源地址https://www.toymoban.com/news/detail-517108.html
基于RESTful实现地图检索
# 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": { # 左上角的坐标点
"lon": 116.326943,
"lat": 39.95499
},
"bottom_right": { # 右下角的坐标点
"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实现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. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 输出结果
for (SearchHit hit : resp.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
到了这里,关于ES 地图经纬度搜索的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!