当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/401680.html

相关文章:

  • Kotlin Misk Web框架
  • 【设计模式之美】【建造型】工厂模式:通过面向接口编程思路,串起业务流程
  • AI算法19-偏最小二乘法回归算法Partial Least Squares Regression | PLS
  • live555关于RTSP协议交互流程
  • Centos7 安装私有 Gitlab
  • 浅谈数学模型在UGC/AIGC游戏数值配置调参中的应用(AI智能体)
  • 第T5周:使用TensorFlow实现运动鞋品牌识别
  • 网络编程学习之tcp
  • 前端XMLHttpRequest、Fetch API、Axios实现文件上传、下载方法及后端Spring文件服务器处理方法
  • STM32智能交通监测系统教程
  • 【利用Selenium+autoIt实现文件上传】
  • python join
  • cython加速python代码
  • React@16.x(60)Redux@4.x(9)- 实现 applyMiddleware
  • level 6 day1 Linux网络编程之网络基础
  • PostgreSQL UPDATE 命令
  • 什么? CSS 将支持 if() 函数了?
  • function calling实现调用理杏仁api获取数据
  • Excel中用VBA实现Outlook发送当前工作簿
  • 从 ArcMap 迁移到 ArcGIS Pro
  • WSL2 的安装与运行 Linux 系统
  • 业务终端动态分配IP-DHCP技术、DHCP中继技术
  • 新一代大语言模型 GPT-5 对工作与生活的影响及应对策略
  • AI基于大模型语言存在的网络安全风险
  • 探索Perl语言:入门学习与实战指南
  • dp or 数学问题
  • kibana连接elasticsearch(版本8.11.3)
  • 基于python的图像去水印
  • Linux下Supervisor的安装与配置
  • 使用Pandas读取Excel文件将特定列转成str格式方法汇总