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