Elasticsearch-多边形范围查询(8.x)

目录

一、字段设计

二、数据录入

三、查询语句

四、Java代码实现


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

一、字段设计

java 复制代码
PUT /aoi_points
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

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

二、数据录入

java 复制代码
POST /aoi_points/_doc
{
  "location": {
    "type": "point",
    "coordinates": [-74.0060, 40.7128]
  }
}

三、查询语句

java 复制代码
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代码实现

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

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<PoiIndex> searchResponse = esUtil.queryDocument(searchRequest, PoiIndex.class);

        if (searchResponse.hits().hits().isEmpty()) {
            return List.of();
        }
        List<SmallAttractionDTO> smallAttractionDtoList = new ArrayList<>();
        for (Hit<PoiIndex> hit : searchResponse.hits().hits()) {
            // 业务处理
        }
相关推荐
源码之家6 小时前
基于Python房价预测系统 数据分析 Flask框架 爬虫 随机森林回归预测模型、链家二手房 可视化大屏 大数据毕业设计(附源码)✅
大数据·爬虫·python·随机森林·数据分析·spark·flask
TDengine (老段)7 小时前
什么是 TDengine IDMP?
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Apache Flink7 小时前
Flink Forward Asia 2025 城市巡回 · 深圳站
大数据·flink
Hello.Reader7 小时前
Flink DataStream API 打包使用 MySQL CDC 连接器
大数据·mysql·flink
2021_fc7 小时前
Flink入门指南:使用Java构建第一个Flink应用
java·大数据·flink
Hello.Reader7 小时前
Streaming ELT with Flink CDC · Iceberg Sink
大数据·flink
RPA机器人就选八爪鱼7 小时前
RPA财务机器人:驱动财务数字化转型的核心引擎
大数据·运维·人工智能·机器人·rpa
2021_fc7 小时前
Flink快速入门--安装与示例运行
大数据·flink
J***Q2928 小时前
大数据存储优化策略
大数据
二进制_博客8 小时前
flume抽取kafka数据到kafka,数据无法从topicA抽取到topicB
大数据·kafka·flume