GEO就是Geolocation的简写形式,代表地理坐标。Redis在3.2版本中加入了对GEO的支持,允许存储地理坐标信息,帮助我们根据经纬度来检索数据。常见的命令有:
命令概览:
GEOADD key [NX|XX] [CH] longitude latitude member [longitude latitude member ...]
summary: Add one or more geospatial items in the geospatial index represented using a sorted set
since: 3.2.0
GEODIST key member1 member2 [m|km|ft|mi]
summary: Returns the distance between two members of a geospatial index
since: 3.2.0
GEOHASH key member [member ...]
summary: Returns members of a geospatial index as standard geohash strings
since: 3.2.0
GEOPOS key member [member ...]
summary: Returns longitude and latitude of members of a geospatial index
since: 3.2.0
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]
summary: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point
since: 3.2.0
GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]
summary: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member
since: 3.2.0
GEOSEARCH key [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH]
summary: Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle.
since: 6.2
GEOSEARCHSTORE destination source [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH] [STOREDIST]
summary: Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle, and store the result in another key.
since: 6.2
用例:
127.0.0.1:6379> geoadd geo1 111.203200 27.680740 yangxi
(integer) 1
127.0.0.1:6379> geoadd geo1 111.324864 27.736339 xinhua
(integer) 1
127.0.0.1:6379> geodist geo1 xinhua yangxi km
"13.4824"
127.0.0.1:6379> geodist geo1 xinhua yangxi m
"13482.4195"
127.0.0.1:6379> geohash geo1 xinhua
1) "wkzjv30rck0"
127.0.0.1:6379> geohash geo1 yangxi
1) "wkzj6rpsn20"
127.0.0.1:6379> geopos geo1 xinhua
1) 1) "111.32486253976821899"
2) "27.73633963669661284"
127.0.0.1:6379> geosearch geo1 frommember yangxi byradius 20 km withdist
1) 1) "yangxi"
2) "0.0000"
2) 1) "xinhua"
2) "13.4824"
具体应用在查询附近的人啊,商店,等跟地理位置有关的。