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()) {
            // 业务处理
        }
相关推荐
Elastic 中国社区官方博客1 天前
Elasticsearch:上下文工程 vs. 提示词工程
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
2501_933329551 天前
Infoseek舆情系统:企业级数字公关AI中台技术解析
大数据·数据挖掘
2501_933670791 天前
2026高职大数据与财务管理专业证书报考条件
大数据
weilaikeqi11111 天前
2026年房产中介怎么选房源管理系统?
大数据
Hello.Reader1 天前
Flink Standalone 本地一键起集群、Session/Application 两种模式、HA 高可用与排障清单
大数据·flink
月初,1 天前
Git 常用操作大全(超详细教程)一文教会你完全使用Git
大数据·git·elasticsearch
清 晨1 天前
TikTok Shop 跨境卖家最新合规与增长应对:从“内容冲量”升级为“商品与履约可控”
大数据·人工智能·跨境电商·tiktok·营销策略
3分钟秒懂大数据1 天前
实时数仓实战篇一:长周期去重指标建设
大数据·数据仓库·面试·性能优化·flink
李少兄1 天前
Git 忽略文件机制:.gitignore 与 .git/info/exclude
java·git·elasticsearch
蓝眸少年CY1 天前
什么是Hadoop
大数据·hadoop·分布式