GEOHASH

Introduction and Use Case(s)

The GEOHASH command in Redis is used to retrieve the Geohash strings representing the position of specified members of a geospatial index (a sorted set). Geohashes are short, alphanumeric strings that encode geographic locations into a compact format. This command is typically used in applications needing efficient storage and retrieval of geographic data, such as location-based services, mapping applications, and real-time tracking systems.

Syntax

  1. GEOHASH key member [member ...]

Parameter Explanations

  • key: The name of the sorted set that acts as a geospatial index.
  • member: One or more members within the geospatial index whose Geohash strings you want to retrieve.

Return Values

The GEOHASH command returns an array where each element is the Geohash string of the corresponding specified member. If a member does not exist, a nil value is returned for that member.

Example output:

  1. 1) "s0d7n2zw"
  2. 2) "s0d7n2zx"
  3. 3) (nil)

Code Examples

  1. dragonfly> GEOADD mygeoset 13.361389 38.115556 "Palermo"
  2. (integer) 1
  3. dragonfly> GEOADD mygeoset 15.087269 37.502669 "Catania"
  4. (integer) 1
  5. dragonfly> GEOHASH mygeoset "Palermo" "Catania" "NonExistent"
  6. 1) "sqc8b49rny0"
  7. 2) "sqdtr74hyu0"
  8. 3) (nil)

Best Practices

  • Ensure that your geospatial index contains unique member names to avoid ambiguity.
  • Regularly update the geospatial index to reflect any changes in geographic data accurately.

Common Mistakes

  • Attempting to retrieve a Geohash for a member that does not exist in the specified key will return nil. Always check if the member exists before using the result.
  • Using non-geospatial keys with GEOHASH will lead to errors. Make sure the key is a valid geospatial index created with GEOADD.

Can I use GEOHASH with non-geospatial data?

No, the GEOHASH command is specifically designed for use with geospatial indexes. Using it with non-geospatial data will result in errors.

What happens if I request a Geohash for a member not present in the set?

If a requested member does not exist in the specified geospatial index, the command will return nil for that member.