es實體,用 @GeoPointField 註解,值爲:中間逗號隔開,如 29.477000,119.278536(經度, 維度)elasticsearch
@Document(indexName = "v_intelligent_store") @Data public class IntelligentStore implements Serializable { private String id; /** 體驗店名稱 */ private String name; /** 聯繫人姓名 */ private String contactsName; /** 聯繫手機號 */ private String phoneNumber; /** 聯繫電話 */ private String telePhone; /** 場景分類 */ private String category; /** 開店時間 */ private LocalDate foundTime; /** 營業時間 */ private String openHours; /** 主營產品 */ private String mainProduct; /** 店鋪面積 */ private Double shopSize; /** xx省xx市xx區 */ private String area; /** 店鋪面積 */ private String detailAddress; /** 經維度,中間逗號隔開 */ @GeoPointField private String location; ... }
接口:ide
/** * 搜索附近的體驗店 * * @param lat 經度 * @param lon 維度 * @param distance 距離 * @param pageable * @return */ Page<IntelligentStoreDTO> findAllByLocaltion( double lat, double lon, double distance, Pageable pageable);
實現:ui
@Override public Page<IntelligentStoreDTO> findAllByLocaltion( double lat, double lon, double distance, Pageable pageable) { GeoDistanceQueryBuilder builder = QueryBuilders.geoDistanceQuery("location")//查詢字段 .point(lat, lon)//設置經緯度 .distance(distance, DistanceUnit.METERS)//設置距離和單位(米) .geoDistance(GeoDistance.ARC); GeoDistanceSortBuilder sortBuilder = SortBuilders.geoDistanceSort("location") .point(lat, lon) .unit(DistanceUnit.METERS) .order(SortOrder.ASC);//排序方式 //構造查詢條件 NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder() .withFilter(builder) .withSort(sortBuilder) .withPageable(pageable); Page<IntelligentStoreDTO> page = elasticsearchTemplate.queryForPage( nativeSearchQueryBuilder.build(), IntelligentStoreDTO.class); return page; }
查詢10千米範圍數據,返回:spa
{ "code": "0", "msg": "成功", "result": [ { "id": "5af2c615e15b402c54c390b9", "name": "智哪兒體驗店1", "area": "浙江省杭州市餘杭區", "detailAddress": "夢想小鎮", "location": "30.292227,120.004870", "distance": "13", //距離目標位置多少米 "createTime": "2018-05-09 17:57:41", "updateTime": "2018-05-09 17:57:41" }, { "id": "5af2c62ce15b402c54c390bc", "name": "智哪兒體驗店3", "area": "浙江省杭州市餘杭區", "detailAddress": "倉前工業園", "location": "30.282905,119.987661", "distance": "1962",//距離目標位置多少米 "createTime": "2018-05-09 17:58:04", "updateTime": "2018-05-09 17:58:04" }, { "id": "5af2c61fe15b402c54c390ba", "name": "智哪兒體驗店1", "area": "浙江省杭州市餘杭區", "detailAddress": "中國移動杭州研發中心", "location": "30.316513,120.021278", "distance": "3111",//距離目標位置多少米 "createTime": "2018-05-09 17:57:51", "updateTime": "2018-05-09 17:57:51" }, { "id": "5af2c626e15b402c54c390bb", "name": "智哪兒體驗店2", "area": "浙江省杭州市餘杭區", "detailAddress": "杭州師範大學", "location": "30.241073,119.949798", "distance": "7774",//距離目標位置多少米 "createTime": "2018-05-09 17:57:58", "updateTime": "2018-05-09 17:57:58" }, { "id": "5b0fbe41d601800007db438c", "name": "社會主義學院智能鎖店", "contactsName": "健康的風景", "area": "浙江省杭州市餘杭區", "detailAddress": "浙江省社會主義學院", "location": "30.291823,120.138587", "distance": "12818",//距離目標位置多少米 "createTime": "2018-05-31 17:20:01", "updateTime": "2018-05-31 17:21:06" } ] }