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

目录

一、字段设计

二、数据录入

三、查询语句

四、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<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()) {
            // 业务处理
        }

相关推荐

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

    2024-07-13 23:40:01       18 阅读
  2. mac安装elasticsearch8.x

    2024-07-13 23:40:01       57 阅读
  3. Elasticsearch 8 支持别名查询

    2024-07-13 23:40:01       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 23:40:01       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 23:40:01       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 23:40:01       42 阅读
  4. Python语言-面向对象

    2024-07-13 23:40:01       53 阅读

热门阅读

  1. SpringBoot后端代码基本逻辑

    2024-07-13 23:40:01       15 阅读
  2. 响应式编程-数据劫持

    2024-07-13 23:40:01       14 阅读
  3. Vue-生命周期勾子函数

    2024-07-13 23:40:01       14 阅读
  4. 计算机如何学习

    2024-07-13 23:40:01       14 阅读
  5. 要修改已经推送到远程仓库的提交信息

    2024-07-13 23:40:01       13 阅读
  6. linux 设置nginx开机自启

    2024-07-13 23:40:01       19 阅读
  7. c++贪心算法

    2024-07-13 23:40:01       16 阅读
  8. ArcGIS Pro SDK (八)地理数据库 4 查询

    2024-07-13 23:40:01       14 阅读
  9. 文本语言的上升沿写法

    2024-07-13 23:40:01       13 阅读
  10. Aop实现后端数据重复提交

    2024-07-13 23:40:01       16 阅读
  11. Android C++系列:Linux进程间关系

    2024-07-13 23:40:01       19 阅读
  12. thinkphp5多层with关联查询错误问题

    2024-07-13 23:40:01       23 阅读
  13. Understanding EtherCAT Device Serial Number Checking

    2024-07-13 23:40:01       15 阅读
  14. 1.1 Android启动概览

    2024-07-13 23:40:01       18 阅读
  15. HttpUtils工具类

    2024-07-13 23:40:01       15 阅读