spring data jpa 存储es地理位置

Document类下增加字段

    /**
     * 地理 范围 信息
     */
    @GeoShapeField
    private GeoJsonPolygon geoJsonPolygon;

    /**
     * 地理位置 点 信息
     */
    @GeoPointField
    private GeoJsonPoint geoJsonPoint;

赋值

写入点的经纬信息

// 写入点的经纬信息
GeoJsonPoint geoJsonPoint = GeoJsonPoint.of(Double.parseDouble(build.getParamsMap().get("lat").toString()), Double.parseDouble(build.getParamsMap().get("lon").toString()));

写入多边形的经纬信息

// 写入多边形的经纬信息,geojson的顺序为左上,右上,右下,左下,最后左上封口作为一个四边形
GeoPoint topLeft = new GeoPoint(Double.parseDouble(build.getParamsMap().get("topLeftLatitude").toString()), Double.parseDouble(build.getParamsMap().get("topLeftLongitude").toString()));
                GeoPoint topRight = new GeoPoint(Double.parseDouble(build.getParamsMap().get("topRightLatitude").toString()), Double.parseDouble(build.getParamsMap().get("topRightLongitude").toString()));
                GeoPoint bottomRight = new GeoPoint(Double.parseDouble(build.getParamsMap().get("bottomRightLatitude").toString()), Double.parseDouble(build.getParamsMap().get("bottomRightLongitude").toString()));
                GeoPoint bottomLeft = new GeoPoint(Double.parseDouble(build.getParamsMap().get("bottomLeftLatitude").toString()), Double.parseDouble(build.getParamsMap().get("bottomLeftLongitude").toString()));

                List<GeoPoint> getPointers = new ArrayList<>();
                getPointers.add(topLeft);
                getPointers.add(topRight);
                getPointers.add(bottomRight);
                getPointers.add(bottomLeft);
                getPointers.add(topLeft);
                GeoJsonPolygon geoJsonPolygon = GeoJsonPolygon.ofGeoPoints(getPointers);

对保存完的数据进行坐标查询

查询参数

{
    "topLeftLongitude":"113",
    "topLeftLatitude":"35",
    "bottomRightLongitude":"114",
    "bottomRightLatitude":"34"
}
	BoolQueryBuilder nameMatch = QueryBuilders.boolQuery();
	CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder();
            coordinatesBuilder.coordinate(searchDto.getTopLeftLongitude(), searchDto.getTopLeftLatitude());
            coordinatesBuilder.coordinate(searchDto.getBottomRightLongitude(), searchDto.getTopLeftLatitude());
            coordinatesBuilder.coordinate(searchDto.getBottomRightLongitude(), searchDto.getBottomRightLatitude());
            coordinatesBuilder.coordinate(searchDto.getTopLeftLongitude(), searchDto.getBottomRightLatitude());
            coordinatesBuilder.coordinate(searchDto.getTopLeftLongitude(), searchDto.getTopLeftLatitude());

	PolygonBuilder polygonBuilder = new PolygonBuilder(coordinatesBuilder);

	nameMatch.must(QueryBuilders.geoShapeQuery("geoJsonPolygon", polygonBuilder.buildGeometry()));
            
	NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(nameMatch).withPageable(pageRequest).build();

	SearchHits<DiskFileEsDataDomain> customers = elasticsearchRestTemplate.search(nativeSearchQuery, DiskFileEsDataDomain.class);

对结果进行set即可使用jpa的repository保存
所有支持的类型参考官方网站
https://www.elastic.co/guide/en/elasticsearch/reference/7.16/geo-shape.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值