公司最近来了一个新项目,做小程序招聘。其中有一个需求是实现附近岗位推荐。由于用户量不大,决定采用redis来实现。之前没有接触过。现在用来记录一下。(redis必须使用3.2及以上版本)
- 先说一下大概流程。将职位id和经纬度存入redis中。每当添加职位时就增加一条信息。当用户点击附近时,通过用户的经纬度来查询它对应的职位id,这样就可以关联起来查询出职位信息返回用户给予展示。
- 项目采用的spring cloud alibaba全家桶,就不写它的maven依赖,只编写redis相关
引入redis依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> <version>2.3.0.release</version> </dependency>
bo
package cn.zxw.vo_bo; import io.swagger.annotations.apimodel; import io.swagger.annotations.apimodelproperty; import lombok.data; /** * @author: zhangxiongwei * @date: 2021-10-26 16:11 * @description: 位置信息 */ @data @apimodel("位置信息") public class locationbo { @apimodelproperty("经度") private double longitude; @apimodelproperty("纬度") private double latitude; @apimodelproperty("半径") private double radius; @apimodelproperty("条数") private long limit; }
redis配置类
package cn.zxw.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.redis.core.boundgeooperations; import org.springframework.data.redis.core.redistemplate; /** * @author: zhangxiongwei * @date: 2021-10-26 16:38 * @description: redi配置 */ @configuration public class redisconfig { /** * the constant geo_stage. */ public static final string geo_stage = "cities"; /** * geo ops bound geo operations. * * @param redistemplate the redis template * @return the bound geo operations */ @bean public boundgeooperations<string, string> citiesgeoops(redistemplate<string, string> redistemplate) { // 清理缓存 redistemplate.delete(geo_stage); return redistemplate.boundgeoops(geo_stage); } }
测试控制器
package cn.zxw.controller; import cn.zxw.result.commonresult; import cn.zxw.vo_bo.locationbo; import io.swagger.annotations.api; import io.swagger.annotations.apioperation; import io.swagger.annotations.apiparam; import lombok.allargsconstructor; import lombok.extern.slf4j.slf4j; import org.springframework.data.geo.*; import org.springframework.data.redis.connection.redisgeocommands; import org.springframework.data.redis.core.boundgeooperations; import org.springframework.data.redis.core.redistemplate; import org.springframework.web.bind.annotation.*; import java.util.hashmap; import java.util.map; /** * @author: zhangxiongwei * @date: 2021-10-26 15:41 * @description: 附近推荐 */ @slf4j @restcontroller @api(tags = "redis", description = "redis控制") @requestmapping("/geo") @allargsconstructor public class redisgeocontroller { private static final string geo_stage = "cities"; private final redistemplate<string, string> redistemplate; private final boundgeooperations<string, string> citiesgeoops; /** * 初始化数据可以将职位id和经纬度存入redis, * 添加职业时增加位置数据 * 当用户点击附近是,传入经纬度。返回id获得职位信息推送给用户 */ @getmapping("/init") @apioperation("初始化") public void init() { // 清理缓存 redistemplate.delete(geo_stage); map<string, point> points = new hashmap<>(); points.put("shijiazhuang", new point(114.48, 38.03)); points.put("xingtang", new point(114.54, 38.42)); points.put("guangcheng", new point(114.84, 38.03)); points.put("gaoyi", new point(114.58, 37.62)); points.put("zhaoxian", new point(114.78, 37.76)); points.put("jinxing", new point(114.13, 38.03)); points.put("luquan", new point(114.03, 38.08)); points.put("xinle", new point(114.67, 38.33)); points.put("zhengding", new point(114.56, 38.13)); // 添加地理信息 redistemplate.boundgeoops(geo_stage).add(points); } @postmapping("/city") @apioperation("获得城市") public commonresult<georesults<redisgeocommands.geolocation<string>>> dis(@requestbody locationbo locationbo) { //设置当前位置 point point = new point(locationbo.getlongitude(), locationbo.getlatitude()); //设置半径范围 metric metric = redisgeocommands.distanceunit.meters; distance distance = new distance(locationbo.getradius(), metric); circle circle = new circle(point, distance); //设置参数 包括距离、坐标、条数 redisgeocommands.georadiuscommandargs args = redisgeocommands .georadiuscommandargs .newgeoradiusargs() .includedistance() .includecoordinates() .sortascending() .limit(locationbo.getlimit()); georesults<redisgeocommands.geolocation<string>> radius = citiesgeoops.radius(circle, args); return commonresult.success(radius); } }
测试数据
### 使用的是httpclient post http://localhost:6001/geo/city content-type: application/json { "longitude": 114.56, "latitude": 38.13, "radius": 100000, "limit": 10 }
返回结果
{
"code": 200,
"message": "操作成功",
"data": {
"averagedistance": {
"value": 31642.19217777778,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.004961039905191403
},
"content": [
{
"content": {
"name": "zhengding",
"point": {
"x": 114.55999821424484,
"y": 38.12999923666221
}
},
"distance": {
"value": 0.1778,
"metric": "meters",
"unit": "m",
"normalizedvalue": 2.787647866453794e-8
}
},
{
"content": {
"name": "shijiazhuang",
"point": {
"x": 114.55999821424484,
"y": 38.02999941748397
}
},
"distance": {
"value": 13144.3531,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.0020608452123245394
}
},
{
"content": {
"name": "xinle",
"point": {
"x": 114.55999821424484,
"y": 38.329998875018696
}
},
"distance": {
"value": 24232.5609,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.0037993164618445796
}
},
{
"content": {
"name": "guangcheng",
"point": {
"x": 114.55999821424484,
"y": 38.02999941748397
}
},
"distance": {
"value": 26919.7324,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.004220626242427844
}
},
{
"content": {
"name": "xingtang",
"point": {
"x": 114.55999821424484,
"y": 38.419999219223335
}
},
"distance": {
"value": 32302.7819,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.005064610857371048
}
},
{
"content": {
"name": "jinxing",
"point": {
"x": 114.55999821424484,
"y": 38.02999941748397
}
},
"distance": {
"value": 39255.7243,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.006154732063610425
}
},
{
"content": {
"name": "zhaoxian",
"point": {
"x": 114.55999821424484,
"y": 37.760000919591185
}
},
"distance": {
"value": 45453.0791,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.007126388018946599
}
},
{
"content": {
"name": "luquan",
"point": {
"x": 114.55999821424484,
"y": 38.07999932707309
}
},
"distance": {
"value": 46718.8049,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.00732483559070619
}
},
{
"content": {
"name": "gaoyi",
"point": {
"x": 114.55999821424484,
"y": 37.62000066579741
}
},
"distance": {
"value": 56752.5152,
"metric": "meters",
"unit": "m",
"normalizedvalue": 0.00889797682301274
}
}
]
}
}response code: 200; time: 92ms; content length: 1844 bytes
上传的只是练习项目,同理只需要将城市名称换成职业id即可
到此这篇关于redis geo实现附近搜索功能的文章就介绍到这了,更多相关redis geo附近搜索内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论