Summary
Command Name | GEOHASH |
Usage | Get geohash of geo index member(s) |
Group | geo |
ACL Category | @read @geo @slow |
Time Complexity | O(1) |
Flag | READONLY |
Arity | -2 |
NOTES
- Time complexity of the operation is O(1) for each member.
Signature
GEOHASH <key>
[
<member1> [ <member2> ... ]]
Usage
Get the geohash string for one or more geo index members. We can add some members(longitude, latitude, member_name) to a geoindex using “GEOADD” command, then we can get the geohash of those members using this GEOHASH command.
NOTES
- The returned geohash is an 11-character long string.
- This command uses the standard geohash algorithm. 2 geohash strings with similar prefixes indicate that, the points are closer. But 2 different prefixes do not ensure that, those points are far away, as those can be closer, because of the nature of the actual algorithm.
Arguments
Parameter | Description | Name | Type | Optional | Multiple |
---|---|---|---|---|---|
<key> | Name of the key that holds the geo index | key | key | ||
<member> | Name of the member(s) location saved in the geo index | member | string | True | True |
NOTES
- <member> is optional. If <member> is not provided then the command returns
(empty array)
.
Return Value
Return value | Case for the return value | Type |
---|---|---|
Array of geohash values | On success, it returns the array of the geohash values of the members | array[string] |
(empty array) | If the key does not exist and/or members are not provided | (empty array) |
error | If the applied to the wrong data type. | error |
NOTES
- Geohash values are returned in the same sequence, as the provided members.
(nil)
is returned for any non-existing member.- If the key does not exist, then it returns
(empty array)
. - If <member> is not provided, then it returns
(empty array)
. - If the command is applied to a key that is not a hash, then the following error is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Examples
Here are a few examples of the usage examples of the command-
# Redis GEOHASH command examples
# Add members to a geo index named bigboxcity
127.0.0.1:6379> geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
(integer) 5
# Check members saved in bigboxcity
127.0.0.1:6379> zrange bigboxcity 0 -1
1) "Rome"
2) "Paris"
3) "Bangkok"
4) "Hong Kong"
5) "Tokyo"
# Check geohash of a single member
127.0.0.1:6379> geohash bigboxcity Paris
1) "u09tvw0f6s0"
# Check geohash of multiple members
127.0.0.1:6379> geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
1) "sr2ykk5t6k0"
2) "wecpkt5uxu0"
3) "xn774c06kf0"
4) "u09tvw0f6s0"
5) "w4rqqbr0kv0"
# Check geohash of multiple members
# But pass one non existing member name
# We get (nil) for the non existing member
127.0.0.1:6379> geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
1) "sr2ykk5t6k0"
2) "wecpkt5uxu0"
3) "xn774c06kf0"
4) (nil)
5) "w4rqqbr0kv0"
# Check geohash of a non existing members
# (nil) is returned for the non existing members
127.0.0.1:6379> geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
1) (nil)
2) (nil)
3) (nil)
# Check the command without any member
# We get an empty array
127.0.0.1:6379> geohash bigboxcity
(empty array)
# Pass a wrong non existing key
# we get an empty array
127.0.0.1:6379> geohash wrongkey
(empty array)
# Pass wrong key and wrong members
# Returns (nil) for all those members
127.0.0.1:6379> geohash wrongkey membera memberb memberc
1) (nil)
2) (nil)
3) (nil)
# Set string value
127.0.0.1:6379> set bigboxstr "some string here"
OK
# Try to use GEOHASH with some key that is not a geindex
# We get an error, for using key of wrong type
127.0.0.1:6379> geohash bigboxstr abc
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Code Implementations
Here are the example implementations of the command in Golang, NodeJS, Java, C#, PHP, Python, Ruby-
// Redis GEOHASH command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
/**
* Add members to a geo index named bigboxcity
*
* Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 5
*/
getaddResult, err := rdb.GeoAdd(ctx, "bigboxcity",
&redis.GeoLocation{Longitude: 2.352222, Latitude: 48.856613, Name: "Paris"},
&redis.GeoLocation{Longitude: 100.501762, Latitude: 13.756331, Name: "Bangkok"},
&redis.GeoLocation{Longitude: 114.109497, Latitude: 22.396427, Name: "Hong Kong"},
&redis.GeoLocation{Longitude: 139.691711, Latitude: 35.689487, Name: "Tokyo"},
&redis.GeoLocation{Longitude: 12.496365, Latitude: 41.902782, Name: "Rome"},
).Result()
if err != nil {
fmt.Println("Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Error: " + err.Error())
}
fmt.Println("Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ", getaddResult)
/**
* Check members saved in bigboxcity
*
* Command: zrange bigboxcity 0 -1
* Result:
* 1) "Rome"
* 2) "Paris"
* 3) "Bangkok"
* 4) "Hong Kong"
* 5) "Tokyo"
*/
zrangeResult, err := rdb.ZRange(ctx, "bigboxcity", 0, -1).Result()
if err != nil {
fmt.Println("Command: zrange bigboxcity 0 -1 withscores | Error: " + err.Error())
}
fmt.Println("Command: zrange bigboxcity 0 -1 withscores | Result: ", zrangeResult)
/**
* Check geohash of a single member
*
* Command: geohash bigboxcity Paris
* Result:
* 1) "u09tvw0f6s0"
*/
geohashResult, err := rdb.GeoHash(ctx, "bigboxcity", "Paris").Result()
if err != nil {
fmt.Println("Command: geohash bigboxcity Paris | Error: " + err.Error())
}
fmt.Println("Command: geohash bigboxcity Paris | Result: ", geohashResult)
/**
* Check geohash of multiple members
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) "u09tvw0f6s0"
* 5) "w4rqqbr0kv0"
*/
geohashResult, err = rdb.GeoHash(ctx, "bigboxcity", "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok").Result()
if err != nil {
fmt.Println("Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Error: " + err.Error())
}
fmt.Println("Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ", geohashResult)
/**
* Check geohash of multiple members
* But pass one non existing member name
* We get (nil) for the non existing member
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) (nil)
* 5) "w4rqqbr0kv0"
*/
geohashResult, err = rdb.GeoHash(ctx, "bigboxcity", "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok").Result()
if err != nil {
fmt.Println("Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Error: " + err.Error())
}
fmt.Println("Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ", geohashResult)
/**
* Check geohash of a non existing members
* (nil) is returned for the non existing members
*
* Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
geohashResult, err = rdb.GeoHash(ctx, "bigboxcity", "wrongmember1", "wrongmember2", "wrongmember3").Result()
if err != nil {
fmt.Println("Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Error: " + err.Error())
}
fmt.Println("Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ", geohashResult)
/**
* Check the command without any member
* We get an empty array
*
* Command: geohash bigboxcity
* Result: (empty array)
*/
geohashResult, err = rdb.GeoHash(ctx, "bigboxcity").Result()
if err != nil {
fmt.Println("Command: geohash bigboxcity | Error: " + err.Error())
}
fmt.Println("Command: geohash bigboxcity | Result: ", geohashResult)
/**
* Pass a wrong non existing key
* we get an empty array
*
* Command: geohash wrongkey
* Result: (empty array)
*/
geohashResult, err = rdb.GeoHash(ctx, "wrongkey").Result()
if err != nil {
fmt.Println("Command: geohash wrongkey | Error: " + err.Error())
}
fmt.Println("Command: geohash wrongkey | Result: ", geohashResult)
/**
* Pass wrong key and wrong members
* Returns (nil) for all those members
*
* Command: geohash wrongkey membera memberb memberc
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
geohashResult, err = rdb.GeoHash(ctx, "wrongkey", "membera", "memberb", "memberc").Result()
if err != nil {
fmt.Println("Command: geohash wrongkey membera memberb memberc | Error: " + err.Error())
}
fmt.Println("Command: geohash wrongkey membera memberb memberc | Result: ", geohashResult)
/**
* Set string value
*
* Command: set bigboxstr "some string here"
* Result: OK
*/
setResult, err := rdb.Set(ctx, "bigboxstr", "soem string here", 0).Result()
if err != nil {
fmt.Println("Command: set bigboxstr "some string here" | Error: " + err.Error())
}
fmt.Println("Command: set bigboxstr "some string here" | Result: " + setResult)
/**
* Try to use GEOHASH with some key that is not a geindex
* We get an error, for using key of wrong type
*
* Command: geohash bigboxstr abc
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
geohashResult, err = rdb.GeoHash(ctx, "bigboxstr", "abc").Result()
if err != nil {
fmt.Println("Command: geohash bigboxstr abc | Error: " + err.Error())
}
fmt.Println("Command: geohash bigboxstr abc | Result: ", geohashResult)
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: [Rome Paris Bangkok Hong Kong Tokyo]
Command: geohash bigboxcity Paris | Result: [u09tvw0f6s0]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: [sr2ykk5t6k0 wecpkt5uxu0 xn774c06kf0 u09tvw0f6s0 w4rqqbr0kv0]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: [sr2ykk5t6k0 wecpkt5uxu0 xn774c06kf0 w4rqqbr0kv0]
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [ ]
Command: geohash bigboxcity | Result: []
Command: geohash wrongkey | Result: []
Command: geohash wrongkey membera memberb memberc | Result: [ ]
Command: set bigboxstr "some string here" | Result: OK
Command: geohash bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: geohash bigboxstr abc | Result: []
NOTES
- Use “
GeoHash
” method from redis-go module. - Signature of the method is-
GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd
// Redis GEOHASH command example in JavaScript(NodeJS)
import { createClient } from "redis";
// Create redis client
const redisClient = createClient({
url: "redis://default:@localhost:6379",
});
redisClient.on("error", (err) =>
console.log("Error while connecting to Redis", err)
);
// Connect Redis client
await redisClient.connect();
/**
* Add members to a geo index named bigboxcity
*
* Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 5
*/
const locationData = [
{
member: "Paris",
longitude: 2.352222,
latitude: 48.856613,
},
{
member: "Bangkok",
longitude: 100.501762,
latitude: 13.756331,
},
{
member: "Hong Kong",
longitude: 114.109497,
latitude: 22.396427,
},
{
member: "Tokyo",
longitude: 139.691711,
latitude: 35.689487,
},
{
member: "Rome",
longitude: 12.496365,
latitude: 41.90278,
},
];
let commandResult = await redisClient.geoAdd("bigboxcity", locationData);
console.log(
'Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ',
commandResult
);
/**
* Check the items in bigboxcity
* Command: zrange bigboxcity 0 -1
* Result:
* 1) "Rome"
* 2) "Paris"
* 3) "Bangkok"
* 4) "Hong Kong"
* 5) "Tokyo"
*/
commandResult = await redisClient.zRange("bigboxcity", 0, -1);
console.log(
"Command: zrange bigboxcity 0 -1 withscores | Result: ",
commandResult
);
/**
* Check geohash of a single member
*
* Command: geohash bigboxcity Paris
* Result:
* 1) "u09tvw0f6s0"
*/
commandResult = await redisClient.geoHash("bigboxcity", "Paris");
console.log("Command: geohash bigboxcity Paris | Result: ", commandResult);
/**
* Check geohash of multiple members
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) "u09tvw0f6s0"
* 5) "w4rqqbr0kv0"
*/
commandResult = await redisClient.geoHash("bigboxcity", [
"Rome",
"Hong Kong",
"Tokyo",
"Paris",
"Bangkok",
]);
console.log(
'Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ',
commandResult
);
/**
* Check geohash of multiple members
* But pass one non existing member name
* We get (nil) for the non existing member
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) (nil)
* 5) "w4rqqbr0kv0"
*/
commandResult = await redisClient.geoHash("bigboxcity", [
"Rome",
"Hong Kong",
"Tokyo",
"WrongMemberValueHere",
"Bangkok",
]);
console.log(
'Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ',
commandResult
);
/**
* Check geohash of a non existing members
* (nil) is returned for the non existing members
*
* Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
commandResult = await redisClient.geoHash("bigboxcity", [
"wrongmember1",
"wrongmember2",
"wrongmember3",
]);
console.log(
"Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ",
commandResult
);
/**
* Check the command without any member
* We get an empty array
*
* Command: geohash bigboxcity
* Result: (empty array)
*/
commandResult = await redisClient.geoHash("bigboxcity", []);
console.log("Command: geohash bigboxcity | Result: ", commandResult);
/**
* Pass a wrong non existing key
* we get an empty array
*
* Command: geohash wrongkey
* Result: (empty array)
*/
commandResult = await redisClient.geoHash("wrongkey", []);
console.log("Command: geohash wrongkey | Result: ", commandResult);
/**
* Pass wrong key and wrong members
* Returns (nil) for all those members
*
* Command: geohash wrongkey membera memberb memberc
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
commandResult = await redisClient.geoHash("wrongkey", [
"membera",
"memberb",
"memberc",
]);
console.log(
"Command: geohash wrongkey membera memberb memberc | Result: ",
commandResult
);
/**
* Set string value
*
* Command: set bigboxstr "some string here"
* Result: OK
*/
commandResult = await redisClient.set("bigboxstr", "soem string here");
console.log(
'Command: set bigboxstr "some string here" | Result: ' + commandResult
);
/**
* Try to use GEOHASH with some key that is not a geindex
* We get an error, for using key of wrong type
*
* Command: geohash bigboxstr abc
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.geoHash("bigboxstr", "abc");
console.log("Command: geohash bigboxstr abc | Result: ", commandResult);
} catch (err) {
console.log("Command: geohash bigboxstr abc | Error: ", err);
}
process.exit(0);
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: [ 'Rome', 'Paris', 'Bangkok', 'Hong Kong', 'Tokyo' ]
Command: geohash bigboxcity Paris | Result: [ 'u09tvw0f6s0' ]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: [
'sr2ykk5t6k0',
'wecpkt5uxu0',
'xn774c06kf0',
'u09tvw0f6s0',
'w4rqqbr0kv0'
]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: [ 'sr2ykk5t6k0', 'wecpkt5uxu0', 'xn774c06kf0', null, 'w4rqqbr0kv0' ]
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [ null, null, null ]
Command: geohash bigboxcity | Result: []
Command: geohash wrongkey | Result: []
Command: geohash wrongkey membera memberb memberc | Result: [ null, null, null ]
Command: set bigboxstr "some string here" | Result: OK
Command: geohash bigboxstr abc | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
NOTES
- Use the function “
geoHash
” of node-redis. - Signature of the method-
function geoHash(key: RedisCommandArgument, member: RedisCommandArgument | Array<RedisCommandArgument>)
// Redis GEOHASH command example in Java
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GeoHash {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Add members to a geo index named bigboxcity
*
* Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 5
*/
Map<String, GeoCoordinate> locationData = new HashMap<>() {{
put("Paris", new GeoCoordinate(2.352222, 48.856613));
put("Bangkok", new GeoCoordinate(100.501762, 13.756331));
put("Hong Kong", new GeoCoordinate(114.109497, 22.396427));
put("Tokyo", new GeoCoordinate(139.691711, 35.689487));
put("Rome", new GeoCoordinate(12.496365, 41.90278));
}};
long getaddResult = jedis.geoadd("bigboxcity", locationData);
System.out.println("Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " + getaddResult);
/**
* Check members saved in bigboxcity
*
* Command: zrange bigboxcity 0 -1
* Result:
* 1) "Rome"
* 2) "Paris"
* 3) "Bangkok"
* 4) "Hong Kong"
* 5) "Tokyo"
*/
List<String> zrangeResult = jedis.zrange("bigboxcity", 0, -1);
System.out.println("Command: zrange bigboxcity 0 -1 | Result: " + zrangeResult.toString());
/**
* Check geohash of a single member
*
* Command: geohash bigboxcity Paris
* Result:
* 1) "u09tvw0f6s0"
*/
List<String> geohashResult = jedis.geohash("bigboxcity", "Paris");
System.out.println("Command: geohash bigboxcity Paris | Result: " + geohashResult.toString());
/**
* Check geohash of multiple members
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) "u09tvw0f6s0"
* 5) "w4rqqbr0kv0"
*/
geohashResult = jedis.geohash("bigboxcity", "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok");
System.out.println("Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: " + geohashResult.toString());
/**
* Check geohash of multiple members
* But pass one non existing member name
* We get (nil) for the non existing member
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) (nil)
* 5) "w4rqqbr0kv0"
*/
geohashResult = jedis.geohash("bigboxcity", "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok");
System.out.println("Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: " + geohashResult.toString());
/**
* Check geohash of a non existing members
* (nil) is returned for the non existing members
*
* Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
geohashResult = jedis.geohash("bigboxcity", "wrongmember1", "wrongmember2", "wrongmember3");
System.out.println("Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: " + geohashResult.toString());
/**
* Check the command without any member
* We get an empty array
*
* Command: geohash bigboxcity
* Result: (empty array)
*/
geohashResult = jedis.geohash("bigboxcity");
System.out.println("Command: geohash bigboxcity | Result: " + geohashResult.toString());
/**
* Pass a wrong non existing key
* we get an empty array
*
* Command: geohash wrongkey
* Result: (empty array)
*/
geohashResult = jedis.geohash("wrongkey");
System.out.println("Command: geohash wrongkey | Result: " + geohashResult.toString());
/**
* Pass wrong key and wrong members
* Returns (nil) for all those members
*
* Command: geohash wrongkey membera memberb memberc
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
geohashResult = jedis.geohash("wrongkey", "membera", "memberb", "memberc");
System.out.println("Command: geohash wrongkey membera memberb memberc | Result: " + geohashResult.toString());
/**
* Set string value
*
* Command: set bigboxstr "some string here"
* Result: OK
*/
String setResult = jedis.set("bigboxstr", "soem string here");
System.out.println("Command: set bigboxstr "some string here" | Result: " + setResult);
/**
* Try to use GEOHASH with some key that is not a geindex
* We get an error, for using key of wrong type
*
* Command: geohash bigboxstr abc
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
geohashResult = jedis.geohash("bigboxstr", "abc");
System.out.println("Command: geohash bigboxstr abc | Result: " + geohashResult.toString());
} catch (Exception e) {
System.out.println("Command: geohash bigboxstr abc | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 | Result: [Rome, Paris, Bangkok, Hong Kong, Tokyo]
Command: geohash bigboxcity Paris | Result: [u09tvw0f6s0]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: [sr2ykk5t6k0, wecpkt5uxu0, xn774c06kf0, u09tvw0f6s0, w4rqqbr0kv0]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: [sr2ykk5t6k0, wecpkt5uxu0, xn774c06kf0, null, w4rqqbr0kv0]
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [null, null, null]
Command: geohash bigboxcity | Result: []
Command: geohash wrongkey | Result: []
Command: geohash wrongkey membera memberb memberc | Result: [null, null, null]
Command: set bigboxstr "some string here" | Result: OK
Command: geohash bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use method “
geohash
” from Jedis package. - The signatures of the method are-
public List<String> geohash(final String key, String... members)
// Redis GEOHASH command examples in C#
using StackExchange.Redis;
namespace GeoHash
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Add city longitude and latitude to geoindex named bigboxcity
* Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 5
*/
GeoEntry[] locationData = new GeoEntry[]{
new GeoEntry(2.352222, 48.856613, "Paris"),
new GeoEntry(100.501762, 13.756331, "Bangkok"),
new GeoEntry(114.109497, 22.396427, "Hong Kong"),
new GeoEntry(139.691711, 35.689487, "Tokyo"),
new GeoEntry(12.496365, 41.90278, "Rome"),
};
long getaddResults = rdb.GeoAdd("bigboxcity", locationData);
Console.WriteLine("Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " + getaddResults);
/**
* Check the items in bigboxcity
* Command: zrange bigboxcity 0 -1
* Result:
* 1) "Rome"
* 2) "Paris"
* 3) "Bangkok"
* 4) "Hong Kong"
* 5) "Tokyo"
*/
RedisValue[] zrangeResult = rdb.SortedSetRangeByScore("bigboxcity", 0, -1);
Console.WriteLine("Command: zrange bigboxcity 0 -1 withscores | Result: " + String.Join(",", zrangeResult));
/**
* Check geohash of a single member
*
* Command: geohash bigboxcity Paris
* Result:
* 1) "u09tvw0f6s0"
*/
string? geohashResult = rdb.GeoHash("bigboxcity", "Paris");
Console.WriteLine("Command: geohash bigboxcity Paris | Result: " + geohashResult);
/**
* Check geohash of multiple members
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) "u09tvw0f6s0"
* 5) "w4rqqbr0kv0"
*/
string?[] geohashResults = rdb.GeoHash("bigboxcity", new RedisValue[] { "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok" });
Console.WriteLine("Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: " + String.Join(",", geohashResults));
/**
* Check geohash of multiple members
* But pass one non existing member name
* We get (nil) for the non existing member
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) (nil)
* 5) "w4rqqbr0kv0"
*/
geohashResults = rdb.GeoHash("bigboxcity", new RedisValue[] { "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok" });
Console.WriteLine("Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: " + String.Join(",", geohashResults));
/**
* Check geohash of a non existing members
* (nil) is returned for the non existing members
*
* Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
geohashResults = rdb.GeoHash("bigboxcity", new RedisValue[] { "wrongmember1", "wrongmember2", "wrongmember3" });
Console.WriteLine("Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: " + String.Join(",", geohashResults));
/**
* Pass wrong key and wrong members
* Returns (nil) for all those members
*
* Command: geohash wrongkey membera memberb memberc
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
geohashResults = rdb.GeoHash("wrongkey", new RedisValue[] { "membera", "memberb", "memberc" });
Console.WriteLine("Command: geohash wrongkey membera memberb memberc | Result: " + String.Join(",", geohashResults));
/**
* Set string value
*
* Command: set bigboxstr "some string here"
* Result: OK
*/
bool setResult = rdb.StringSet("bigboxstr", "soem string here");
Console.WriteLine("Command: set bigboxstr "some string here" | Result: " + setResult);
/**
* Try to use GEOHASH with some key that is not a geindex
* We get an error, for using key of wrong type
*
* Command: geohash bigboxstr abc
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
geohashResult = rdb.GeoHash("bigboxstr", "abc");
Console.WriteLine("Command: geohash bigboxstr abc | Result: " + geohashResult);
}
catch (Exception e)
{
Console.WriteLine("Command: geohash bigboxstr abc | Error: " + e.Message);
}
}
}
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result:
Command: geohash bigboxcity Paris | Result: u09tvw0f6s0
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: sr2ykk5t6k0,wecpkt5uxu0,xn774c06kf0,u09tvw0f6s0,w4rqqbr0kv0
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: sr2ykk5t6k0,wecpkt5uxu0,xn774c06kf0,,w4rqqbr0kv0
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ,,
Command: geohash wrongkey membera memberb memberc | Result: ,,
Command: set bigboxstr "some string here" | Result: True
Command: geohash bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use the method “
” from StackExchange.Redis.GeoHash
- Method signatures-
string?[] GeoHash(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
string? GeoHash(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
<?php
// Redis GEOHASH command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Add city longitude and latitude to geoindex named bigboxcity
* Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 5
*/
$commandResult = $redisClient->geoadd(
"bigboxcity",
2.352222,
48.856613,
"Paris",
100.501762,
13.756331,
"Bangkok",
114.109497,
22.396427,
"Hong Kong",
139.691711,
35.689487,
"Tokyo",
12.496365,
41.902782,
"Rome"
);
echo "Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " . $commandResult . "n";
/**
* Check the items in bigboxcity
* Command: zrange bigboxcity 0 -1
* Result:
* 1) "Rome"
* 2) "Paris"
* 3) "Bangkok"
* 4) "Hong Kong"
* 5) "Tokyo"
*/
$commandResult = $redisClient->zrange("bigboxcity", 0, -1);
echo "Command: zrange bigboxcity 0 -1 withscores | Result: ";
print_r($commandResult);
/**
* Check geohash of a single member
*
* Command: geohash bigboxcity Paris
* Result:
* 1) "u09tvw0f6s0"
*/
$commandResult = $redisClient->geohash("bigboxcity", ["Paris"]);
echo "Command: geohash bigboxcity Paris | Result: ";
print_r($commandResult);
/**
* Check geohash of multiple members
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) "u09tvw0f6s0"
* 5) "w4rqqbr0kv0"
*/
$commandResult = $redisClient->geohash("bigboxcity", ["Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok"]);
echo "Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ";
print_r($commandResult);
/**
* Check geohash of multiple members
* But pass one non existing member name
* We get (nil) for the non existing member
*
* Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
* Result:
* 1) "sr2ykk5t6k0"
* 2) "wecpkt5uxu0"
* 3) "xn774c06kf0"
* 4) (nil)
* 5) "w4rqqbr0kv0"
*/
$commandResult = $redisClient->geohash("bigboxcity", ["Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok"]);
echo "Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ";
print_r($commandResult);
/**
* Check geohash of a non existing members
* (nil) is returned for the non existing members
*
* Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
$commandResult = $redisClient->geohash("bigboxcity", ["wrongmember1", "wrongmember2", "wrongmember3"]);
echo "Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ";
print_r($commandResult);
/**
* Check the command without any member
* We get an empty array
*
* Command: geohash bigboxcity
* Result: (empty array)
*/
$commandResult = $redisClient->geohash("bigboxcity", []);
echo "Command: geohash bigboxcity | Result: ";
print_r($commandResult);
/**
* Pass a wrong non existing key
* we get an empty array
*
* Command: geohash wrongkey
* Result: (empty array)
*/
$commandResult = $redisClient->geohash("wrongkey", []);
echo "Command: geohash wrongkey | Result: ";
print_r($commandResult);
/**
* Pass wrong key and wrong members
* Returns (nil) for all those members
*
* Command: geohash wrongkey membera memberb memberc
* Result:
* 1) (nil)
* 2) (nil)
* 3) (nil)
*/
$commandResult = $redisClient->geohash("wrongkey", ["membera", "memberb", "memberc"]);
echo "Command: geohash wrongkey membera memberb memberc | Result: ";
print_r($commandResult);
/**
* Set string value
*
* Command: set bigboxstr "some string here"
* Result: OK
*/
$commandResult = $redisClient->set("bigboxstr", "soem string here");
echo "Command: set bigboxstr "some string here" | Result: " . $commandResult . "n";
/**
* Try to use GEOHASH with some key that is not a geindex
* We get an error, for using key of wrong type
*
* Command: geohash bigboxstr abc
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->geohash("bigboxstr", ["abc"]);
echo "Command: geohash bigboxstr abc | Result: ";
print_r($commandResult);
} catch (Exception $e) {
echo "Command: geohash bigboxstr abc | Error: " . $e->getMessage() . "n";
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: Array
(
[0] => Rome
[1] => Paris
[2] => Bangkok
[3] => Hong Kong
[4] => Tokyo
)
Command: geohash bigboxcity Paris | Result: Array
(
[0] => u09tvw0f6s0
)
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: Array
(
[0] => sr2ykk5t6k0
[1] => wecpkt5uxu0
[2] => xn774c06kf0
[3] => u09tvw0f6s0
[4] => w4rqqbr0kv0
)
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: Array
(
[0] => sr2ykk5t6k0
[1] => wecpkt5uxu0
[2] => xn774c06kf0
[3] =>
[4] => w4rqqbr0kv0
)
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: Array
(
[0] =>
[1] =>
[2] =>
)
Command: geohash bigboxcity | Result: Array
(
)
Command: geohash wrongkey | Result: Array
(
)
Command: geohash wrongkey membera memberb memberc | Result: Array
(
[0] =>
[1] =>
[2] =>
)
Command: set bigboxstr "some string here" | Result: OK
Command: geohash bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use the method “
geohash
” of predis. - Signature of the method is-
geohash(string $key, array $members): array
# Redis GEOHASH command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(
host="localhost", port=6379, username="default", password="", decode_responses=True
)
# Add city longitude and latitude to geoindex named bigboxcity
# Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
# Result: (integer) 5
commandResult = redisClient.geoadd(
"bigboxcity",
(2.352222, 48.856613, "Paris")
+ (100.501762, 13.756331, "Bangkok")
+ (114.109497, 22.396427, "Hong Kong")
+ (139.691711, 35.689487, "Tokyo")
+ (12.496365, 41.902782, "Rome"),
)
print(
'Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: {}'.format(
commandResult
)
)
# Check the items in bigboxcity
# Command: zrange bigboxcity 0 -1
# Result:
# 1) "Rome"
# 2) "Paris"
# 3) "Bangkok"
# 4) "Hong Kong"
# 5) "Tokyo"
commandResult = redisClient.zrange("bigboxcity", 0, -1)
print("Command: zrange bigboxcity 0 -1 withscores | Result: {}".format(commandResult))
# Check geohash of a single member
# Command: geohash bigboxcity Paris
# Result:
# 1) "u09tvw0f6s0"
commandResult = redisClient.geohash("bigboxcity", "Paris")
print("Command: geohash bigboxcity Paris | Result: {}".format(commandResult))
# Check geohash of multiple members
# Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
# Result:
# 1) "sr2ykk5t6k0"
# 2) "wecpkt5uxu0"
# 3) "xn774c06kf0"
# 4) "u09tvw0f6s0"
# 5) "w4rqqbr0kv0"
commandResult = redisClient.geohash("bigboxcity", "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok")
print("Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: {}".format(commandResult))
# Check geohash of multiple members
# But pass one non existing member name
# We get (nil) for the non existing member
# Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
# Result:
# 1) "sr2ykk5t6k0"
# 2) "wecpkt5uxu0"
# 3) "xn774c06kf0"
# 4) (nil)
# 5) "w4rqqbr0kv0"
commandResult = redisClient.geohash("bigboxcity", "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok")
print("Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: {}".format(commandResult))
# Check geohash of a non existing members
# (nil) is returned for the non existing members
# Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
# Result:
# 1) (nil)
# 2) (nil)
# 3) (nil)
commandResult = redisClient.geohash("bigboxcity", "wrongmember1", "wrongmember2", "wrongmember3")
print("Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: {}".format(commandResult))
# Check the command without any member
# We get an empty array
# Command: geohash bigboxcity
# Result: (empty array)
commandResult = redisClient.geohash("bigboxcity")
print("Command: geohash bigboxcity | Result: {}".format(commandResult))
# Pass a wrong non existing key
# we get an empty array
# Command: geohash wrongkey
# Result: (empty array)
commandResult = redisClient.geohash("wrongkey")
print("Command: geohash wrongkey | Result: {}".format(commandResult))
# Pass wrong key and wrong members
# Returns (nil) for all those members
# Command: geohash wrongkey membera memberb memberc
# Result:
# 1) (nil)
# 2) (nil)
# 3) (nil)
commandResult = redisClient.geohash("wrongkey", "membera", "memberb", "memberc")
print("Command: geohash wrongkey membera memberb memberc | Result: {}".format(commandResult))
# Set string value
# Command: set bigboxstr "some string here"
# Result: OK
commandResult = redisClient.set("bigboxstr", "soem string here")
print("Command: set bigboxstr "some string here" | Result: {}".format(commandResult))
# Try to use GEOHASH with some key that is not a geindex
# We get an error, for using key of wrong type
# Command: geohash bigboxstr abc
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.geohash("bigboxstr", "abc")
print("Command: geohash bigboxstr abc | Result: {}".format(commandResult))
except Exception as error:
print("Command: geohash bigboxstr abc | Error: ", error)
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: ['Rome', 'Paris', 'Bangkok', 'Hong Kong', 'Tokyo']
Command: geohash bigboxcity Paris | Result: ['u09tvw0f6s0']
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ['sr2ykk5t6k0', 'wecpkt5uxu0', 'xn774c06kf0', 'u09tvw0f6s0', 'w4rqqbr0kv0']
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ['sr2ykk5t6k0', 'wecpkt5uxu0', 'xn774c06kf0', None, 'w4rqqbr0kv0']
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [None, None, None]
Command: geohash bigboxcity | Result: []
Command: geohash wrongkey | Result: []
Command: geohash wrongkey membera memberb memberc | Result: [None, None, None]
Command: set bigboxstr "some string here" | Result: True
Command: geohash bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use method “
geohash
” from redis-py. - Signature of the method is –
def geohash(self, name: KeyT, *values: FieldT) -> ResponseT
# Redis GEOHASH command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Add city longitude and latitude to geoindex named bigboxcity
# Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
# Result: (integer) 5
commandResult = redis.geoadd(
"bigboxcity",
2.352222, 48.856613, "Paris",
100.501762, 13.756331, "Bangkok",
114.109497, 22.396427, "Hong Kong",
139.691711, 35.689487, "Tokyo",
12.496365, 41.902782, "Rome",
)
print("Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ", commandResult, "n")
# Check the items in bigboxcity
# Command: zrange bigboxcity 0 -1
# Result:
# 1) "Rome"
# 2) "Paris"
# 3) "Bangkok"
# 4) "Hong Kong"
# 5) "Tokyo"
commandResult = redis.zrange("bigboxcity", 0, -1)
print("Command: zrange bigboxcity 0 -1 withscores | Result: ", commandResult, "n")
# Check geohash of a single member
# Command: geohash bigboxcity Paris
# Result:
# 1) "u09tvw0f6s0"
commandResult = redis.geohash("bigboxcity", "Paris")
print("Command: geohash bigboxcity Paris | Result: ", commandResult, "n")
# Check geohash of multiple members
# Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
# Result:
# 1) "sr2ykk5t6k0"
# 2) "wecpkt5uxu0"
# 3) "xn774c06kf0"
# 4) "u09tvw0f6s0"
# 5) "w4rqqbr0kv0"
commandResult = redis.geohash("bigboxcity", ["Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok"])
print("Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ", commandResult, "n")
# Check geohash of multiple members
# But pass one non existing member name
# We get (nil) for the non existing member
# Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
# Result:
# 1) "sr2ykk5t6k0"
# 2) "wecpkt5uxu0"
# 3) "xn774c06kf0"
# 4) (nil)
# 5) "w4rqqbr0kv0"
commandResult = redis.geohash("bigboxcity", ["Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok"])
print("Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ", commandResult, "n")
# Check geohash of a non existing members
# (nil) is returned for the non existing members
# Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3
# Result:
# 1) (nil)
# 2) (nil)
# 3) (nil)
commandResult = redis.geohash("bigboxcity", ["wrongmember1", "wrongmember2", "wrongmember3"])
print("Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ", commandResult, "n")
# Check the command without any member
# We get an empty array
# Command: geohash bigboxcity
# Result: (empty array)
commandResult = redis.geohash("bigboxcity", [])
print("Command: geohash bigboxcity | Result: ", commandResult, "n")
# Pass a wrong non existing key
# we get an empty array
# Command: geohash wrongkey
# Result: (empty array)
commandResult = redis.geohash("wrongkey", [])
print("Command: geohash wrongkey | Result: ", commandResult, "n")
# Pass wrong key and wrong members
# Returns (nil) for all those members
# Command: geohash wrongkey membera memberb memberc
# Result:
# 1) (nil)
# 2) (nil)
# 3) (nil)
commandResult = redis.geohash("wrongkey", ["membera", "memberb", "memberc"])
print("Command: geohash wrongkey membera memberb memberc | Result: ", commandResult, "n")
# Set string value
# Command: set bigboxstr "some string here"
# Result: OK
commandResult = redis.set("bigboxstr", "soem string here")
print("Command: set bigboxstr "some string here" | Result: ", commandResult, "n")
# Try to use GEOHASH with some key that is not a geindex
# We get an error, for using key of wrong type
# Command: geohash bigboxstr abc
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.geohash("bigboxstr", "abc")
print("Command: geohash bigboxstr abc | Result: ", commandResult, "n")
rescue => e
print("Command: geohash bigboxstr abc | Error: ", e, "n")
end
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: ["Rome", "Paris", "Bangkok", "Hong Kong", "Tokyo"]
Command: geohash bigboxcity Paris | Result: ["u09tvw0f6s0"]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ["sr2ykk5t6k0", "wecpkt5uxu0", "xn774c06kf0", "u09tvw0f6s0", "w4rqqbr0kv0"]
Command: geohash bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ["sr2ykk5t6k0", "wecpkt5uxu0", "xn774c06kf0", nil, "w4rqqbr0kv0"]
Command: geohash bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [nil, nil, nil]
Command: geohash bigboxcity | Result: []
Command: geohash wrongkey | Result: []
Command: geohash wrongkey membera memberb memberc | Result: [nil, nil, nil]
Command: set bigboxstr "some string here" | Result: OK
Command: geohash bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use method “
geohash
” from the redis-rb. - Signature of the method is-
# @param [String] key
# @param [String, Array<String>]
# @return [Array<String, nil>]
def geohash(key, member)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
GEOADD | Command Details |
ZRANGE | Command Details |