Spring ElasticsearchTemplate 经纬度按距离排序

es实体,用 @GeoPointField 注解,值为:中间逗号隔开,如 29.477000,119.278536(经度, 维度)

@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;
  ...  
}

接口:

/**
   * 搜索附近的体验店
   *
   * @param lat 经度
   * @param lon 维度
   * @param distance 距离
   * @param pageable
   * @return
   */
  Page<IntelligentStoreDTO> findAllByLocaltion(
      double lat, double lon, double distance, Pageable pageable);

实现:

@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公里范围数据,返回:

{
  "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"
    }
  ]
}

 

posted @ 2018-05-31 17:55  艺言弈行  阅读(8247)  评论(8编辑  收藏  举报