附近店铺
GEO数据结构
基本使用
# 添加三个坐标到geo中
GEOADD g1 116.378248 39.865275 bjn 116.42803 39.903738 bjz 116.322287 39.893729 bjx
# 返回两点之间的距离(默认是米)
GEODIST g1 bjn bjx
# 返回两点之间的距离(以km返回)
GEODIST g1 bjn bjx km
# 搜索以某个点附近10km内的点带上距离
GEORADIUS g1 116.397904 39.909005 10 km withdist
搜索附近店铺
导入数据
@Test
void method1() {
// 查数据库将所有店铺信息查出来
List<Shop> shops = iShopService.list();
// 通过类型分组
Map<Long, List<Shop>> map = shops.stream().collect(Collectors.groupingBy(Shop::getTypeId));
// 遍历所有map
for(Map.Entry<Long,List<Shop>> shop : map.entrySet()) {
拿到每个类型下的所有店铺集合
List<Shop> shopList = shop.getValue();
// 根据店铺类型来创建key
Long typeId = shop.getKey();
String key = "shop:geo:" + typeId;
// 创建redis中的数据结构
List<RedisGeoCommands.GeoLocation<String>> geoList = new ArrayList<>(shopList.size());
// 遍历所有店铺
for (Shop s : shopList) {
// 将店铺信息的id作为member放入redis集合
geoList.add(new RedisGeoCommands.GeoLocation<>(s.getId().toString(),new Point(s.getX(),s.getY())));
}
// 插入操作
stringRedisTemplate.opsForGeo().add(key,geoList);
}
}
实现附近的店铺
@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
if (x == null || y == null) {
Page<Shop> page = query()
.eq("type_id", typeId).page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));
return Result.ok(page.getRecords());
}
int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;
int end = current * SystemConstants.DEFAULT_PAGE_SIZE;
String key = "shop:geo:" + typeId;
GeoResults<RedisGeoCommands.GeoLocation<String>> radius = stringRedisTemplate.opsForGeo().radius(key, new Circle(new Point(x, y), new Distance(5000)), RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending().limit(end).includeDistance());
if (radius == null) {
return Result.ok();
}
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> content = radius.getContent();
if (content.size() <= from) {
return Result.ok();
}
List<Shop> shops = content.stream().skip(from).map(r -> {
Shop shop = this.getById(Integer.parseInt(r.getContent().getName()));
shop.setDistance(r.getDistance().getValue());
return shop;
}
).collect(Collectors.toList());
return Result.ok(shops);
}
).collect(Collectors.toList());
return Result.ok(shops);
}