Elasticsearch-多边形范围查询(8.x)
创始人
2025-01-08 16:04:45
0

目录

一、字段设计

二、数据录入

三、查询语句

四、Java代码实现


开发版本详见:Elasticsearch-经纬度查询(8.x-半径查询)_es经纬度范围查询-CSDN博客

一、字段设计

PUT /aoi_points {   "mappings": {     "properties": {       "location": {         "type": "geo_shape"       }     }   } }

aoi_points是索引名称,location是字段名称,它将存储地理形状数据

二、数据录入

POST /aoi_points/_doc {   "location": {     "type": "point",     "coordinates": [-74.0060, 40.7128]   } }

三、查询语句

GET /aoi_points/_search {   "query": {     "bool": {       "filter": {         "geo_shape": {           "location": {             "shape": {               "type": "polygon",               "coordinates": [                 [                   [-74.02, 40.715],                   [-73.99, 40.715],                   [-73.99, 40.705],                   [-74.02, 40.705],                   [-74.02, 40.715]                 ]               ]             },             "relation": "within"           }         }       }     }   } }
  • location是存储地理位置的字段
  • shape定义了一个多边形区域,coordinates是一个数组,包含多边形顶点的坐标
  • relation指定了查询的地理空间关系,这里是within,表示查询多边形内部的点
  • 多边形的坐标点需要按顺序(通常是顺时针或逆时针)排列,形成一个闭合的多边形

四、Java代码实现

具体查询对象,可自行定义,本方法只提供思路,莫直接粘贴使用

        // 封装ES查询参数         BoolQuery.Builder boolQueryBuilder = new BoolQuery.Builder();         // AOI范围查询         ShapePO shapePo =                     new ShapePO().setType(GeographyType.POLYGON.getValue()).setCoordinates(poi.getAoi().getCoordinates());         // 多边形查询         GeoShapeQuery geoShapeQuery =             GeoShapeQuery.of(geoShape -> geoShape.field(PoiIndexConstant.LOCATION)                     .shape(s -> s.shape(JsonData.fromJson(JSONUtil.toJsonStr(shapePo))).relation(GeoShapeRelation.Within)))._toQuery().geoShape();         boolQueryBuilder.filter(f -> f.geoShape(geoShapeQuery));         int size = poi.getAoi().getCoordinates().get(0).size();          SearchRequest.Builder searchRequestBuilder = new SearchRequest.Builder();         searchRequestBuilder.index(esIndexProperties.getPoiIndexRead())                 .query(query -> query.bool(boolQueryBuilder.build()))                 .size(size);          // ES查询         SearchRequest searchRequest = searchRequestBuilder.build();         log.info("getSmallAttractionByPoiId query:{}", searchRequest.toString());         SearchResponse searchResponse = esUtil.queryDocument(searchRequest, PoiIndex.class);          if (searchResponse.hits().hits().isEmpty()) {             return List.of();         }         List smallAttractionDtoList = new ArrayList<>();         for (Hit hit : searchResponse.hits().hits()) {             // 业务处理         }

相关内容

热门资讯

突发!wepoker买钻石有用... 突发!wepoker买钻石有用吗,德扑之心免费透视,好像真的有挂(有挂教学)1)德扑之心免费透视免费...
相较于以往!wepoker私人... 相较于以往!wepoker私人局外卦,德普之星透视辅助,本来是真的有挂(有挂功能);1)德普之星透视...
长期以来!购买wepoker模... 长期以来!购买wepoker模拟器,德扑之心免费透视,一贯真的是有挂(竟然有挂)1、完成德扑之心免费...
这一问题亟待解决!wepoke... 这一问题亟待解决!wepoker轻量版有透视吗,如何下载德普之星辅助软件,本来确实有挂(有挂方针)1...
有消息称!wpk私人辅助,如何... 有消息称!wpk私人辅助,如何下载德普之星辅助软件,确实是真的有挂(有挂细节)1、如何下载德普之星辅...
技巧辅助挂!wepoker有辅... 技巧辅助挂!wepoker有辅助功能吗,德扑之星透视,切实真的有挂(有挂技巧)1、德扑之星透视辅助器...
为了进一步!hhpoker德州... 为了进一步!hhpoker德州机器人,德普之星辅助器,真是确实有挂(有挂技术)1、实时德普之星辅助器...
截至发稿!wpk真的有透视嘛,... 截至发稿!wpk真的有透视嘛,德普之星怎么开辅助,其实是有挂(有挂解惑)1、用户打开应用后不用登录就...
技巧辅助挂!xpoker辅助神... 技巧辅助挂!xpoker辅助神器,德扑软件,一贯存在有挂(真的有挂)1、进入到德扑软件是否有挂之后,...
受玩家影响!wepoker私人... 受玩家影响!wepoker私人辅助器,智星菠萝可以辅助吗,原来是真的有挂(有挂工具)小薇(辅助器软件...