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()) {
            // 业务处理
        }
相关推荐
阿明副业观察20 小时前
AI视频生成设备选型指南:平板 vs 电脑,怎么选更高效?
大数据·人工智能·ai作画·aigc·电脑·音视频·ai写作
小巫山云子20 小时前
Git的用法
大数据·git·elasticsearch
yichengerp20 小时前
中小电子厂用什么ERP?电子厂ERP管理软件实施要点与易呈方案推荐
大数据·运维·人工智能·云计算·制造
我的愿望是成为富婆21 小时前
2027校招AI产品经理能力清单:数据科学与大数据技术专业如何准备
大数据·人工智能·产品经理
腾讯云大数据21 小时前
本体立骨架,语义供刻度——DataBuddy对企业AI语义架构的一次完整回答
大数据·人工智能·架构·云计算·腾讯云
武汉星际互动21 小时前
从“一件事”到“一类事”,政务服务改革的“下一站”在哪里?
大数据·人工智能·政务
Allen_LVyingbo21 小时前
生命过程的分子语言:AI 解析范式与场景应用全景研究(上)
大数据·人工智能·知识图谱·健康医疗·数据库开发·数据库架构
KKKlucifer21 小时前
国内数据分类分级平台的路线分化与落地边界
大数据·人工智能·分类
todoitbo21 小时前
大数据平台如何选时序数据库:用业务 SQL 验证查询、留存和 Spark 链路
大数据·sql·时序数据库
Elasticsearch21 小时前
多租户集群中的 Kubernetes 吵闹邻居:使用 ES|QL 找出它
elasticsearch