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()) {
            // 业务处理
        }
相关推荐
万悉科技9 小时前
比 Profound 更适合中国企业的GEO产品
大数据·人工智能
汽车仪器仪表相关领域10 小时前
LambdaCAN:重构专业空燃比测量的数字化范式
大数据·人工智能·功能测试·安全·重构·汽车·压力测试
璞华Purvar10 小时前
地方产投集团数字化平台建设实战:从内控管理到决策赋能(璞华公开课第5期活动回顾)
大数据·人工智能
GeminiJM11 小时前
Elasticsearch minimum_should_match 参数详解
大数据·elasticsearch·jenkins
少废话h12 小时前
Redis主从与集群搭建全指南
大数据·linux·redis·mysql
TextIn智能文档云平台12 小时前
什么是多模态信息抽取,它和传统OCR有什么区别?
大数据·人工智能
雨中飘荡的记忆13 小时前
HBase实战指南
大数据·数据库·hbase
半吊子全栈工匠13 小时前
如何接手一个数据团队?
大数据·人工智能
新诺韦尔API13 小时前
如何快速接入手机携号转网查询接口?
大数据·智能手机·api
铭毅天下14 小时前
Spring Boot + Easy-ES 3.0 + Easyearch 实战:从 CRUD 到“避坑”指南
java·spring boot·后端·spring·elasticsearch