Redis中GEO数据结构实现附近商户搜索

devtools/2024/10/9 13:12:20/

Redis的版本必须是6.2以上

在测试类中将数据导入Redis

@Testvoid loadShopData(){//1.查询店铺信息List<Shop> list = shopService.list();//2.把店铺分组,按照typeId分组,typeId一致的放到一个集合Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));//3.分批完成写入Redisfor (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {//3.1获取类型idLong typeId = entry.getKey();String key = "shop:geo:" + typeId;//3.2获取同类型的店铺集合List<Shop> value = entry.getValue();List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(value.size());//3.3 写入redis GEOADD key 经度 纬度 memberfor (Shop shop : value) {
//                stringRedisTemplate.opsForGeo().add(key, new Point(shop.getX(),shop.getY()), shop.getId().toString());//通过批量来写locations.add(new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),new Point(shop.getX(), shop.getY())));stringRedisTemplate.opsForGeo().add(key, locations);}}}

控制层代码

    public Result queryShopByType(@RequestParam("typeId") Integer typeId,@RequestParam(value = "current", defaultValue = "1") Integer current,@RequestParam(value = "x", required = false) Double x,@RequestParam(value = "y", required = false) Double y) {return shopService.queryShopByType(typeId, current, x, y);}

服务层代码

@Overridepublic 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;//查询Redis,按照距离排序、分页。结果:shopId、distance  ,通过limit获取的是0-end,之后进行截取String key = SHOP_GEO_KEY + typeId;GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo().search(key,GeoReference.fromCoordinate(x, y),new Distance(5000),RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end));//4.解析出idif (results == null){return Result.ok(Collections.emptyList());}List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();if(list.size() <= from){//没有下一页了,结束return Result.ok(Collections.emptyList());}//4.1截取from-end的部分List<Long> ids = new ArrayList<>(list.size());Map<String, Distance> distanceMap = new HashMap<>(list.size());list.stream().skip(from).forEach(result ->{//获取店铺idString shopIdStr = result.getContent().getName();ids.add(Long.parseLong(shopIdStr));//获取距离Distance distance = result.getDistance();distanceMap.put(shopIdStr, distance);});//根据id查询shopString idStr = StrUtil.join(",", ids);List<Shop> shops = query().in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list();for (Shop shop : shops) {shop.setDistance(distanceMap.get(shop.getImages().toString()).getValue());}return Result.ok(shops);}


http://www.ppmy.cn/devtools/123393.html

相关文章

高等数学 4.2 换元积分法(二)第二类换元法

第二类换元法是&#xff1a;适当选择变量代换 x ψ ( t ) x \psi(t) xψ(t) &#xff0c;将积分 ∫ f ( x ) d x \int f(x) \mathrm{d}x ∫f(x)dx 化为积分 ∫ f [ ψ ( t ) ] ψ ′ ( t ) d t \int f[\psi(t)]\psi(t)\mathrm{d}t ∫f[ψ(t)]ψ′(t)dt .这是另一种形式的变…

pod管理及优化

一、k8s中的资源 1、资源介绍 [rootk8s-master ~]# kubectl --namespace timinglee get po No resources found in timinglee namespace. [rootk8s-master ~]# kubectl run testpod --image timinglee/nginx [rootk8s-master ~]# kubectl get pods -w NAME READY STATU…

2410git,git合并pr

git remote add dy https://github.com/dlang/dmd //添加远程,可以多添加几个,作为基础. git remote update //远程更新,全部拉进本地 git remote -v git branch -r //查看 git checkout -b pt dd/private-this //从某个分支拉到本地为pt分支 git checkout master //切换到主分…

Spring Boot ⽇志

目录 1.⽇志使⽤ 2.⽇志级别 3.⽇志配置 3.1配置⽇志级别 3.2⽇志持久化 3.3配置⽇志⽂件分割 4.更简单的⽇志输出 1.⽇志使⽤ 在使用之前我们先来了解一下为什么要使用&#xff1f; ⽇志的⽤途 1.系统监控 我们可以通过⽇志记录这个系统的运⾏状态&#xff0c;对数…

大数据新视界 --大数据大厂之 Presto 性能优化秘籍:加速大数据交互式查询

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

LabVIEW提高开发效率技巧----阻塞时钟

在LabVIEW开发中&#xff0c;阻塞时钟&#xff08;Blocking Timed Loops&#xff09;是一种常见且强大的技术&#xff0c;尤其适用于时间关键的应用。在这些应用中&#xff0c;精确控制循环的执行频率是关键任务。阻塞时钟通过等待循环的执行完成后再进入下一次迭代&#xff0c…

数字安全新时代:聚焦关键信息基础设施安全保障——The Open Group 2024生态系统架构·可持续发展年度大会盛大来袭

在全球数字化转型的浪潮中&#xff0c;关键信息基础设施&#xff08;Critical Information Infrastructure&#xff0c;简称CII&#xff09;的安全保障已成为各国政府和企业共同关注的焦点。随着技术的飞速发展和网络威胁的日益复杂&#xff0c;如何构建高效、灵活且安全的数字…

sqlserver-合理化CTFP(cost threshold for parallelism)

文章目录 About CTFPCTFP 默认值的意义合理化CTFP值1.查看高使用次数的执行计划2.调整CTFP值 About CTFP CTFP (Cost Threshold for Parallelism) 是 SQL Server 中的一项配置&#xff0c;用于控制查询执行计划何时使用并行处理。具体来说&#xff0c;它表示执行计划的“子树成…