Redis.Commands.Geo (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis geospatial operations.

Provides pure functions that build command lists for storing, querying, and measuring geographic coordinates using Redis sorted sets. Supports adding members with longitude/latitude (GEOADD), searching by radius or bounding box (GEOSEARCH), computing distances (GEODIST), and retrieving positions (GEOPOS). Each function returns a plain list of strings suitable for passing to Redis.command/2 or Redis.pipeline/2.

These functions contain no connection or networking logic -- they only construct the Redis protocol command as a list.

Examples

Add geographic coordinates for several locations:

iex> Redis.Commands.Geo.geoadd("restaurants", [{-122.4194, 37.7749, "San Francisco"}, {-73.9857, 40.7484, "New York"}])
["GEOADD", "restaurants", "-122.4194", "37.7749", "San Francisco", "-73.9857", "40.7484", "New York"]

Search for members within a radius of a given point:

iex> Redis.Commands.Geo.geosearch("restaurants", fromlonlat: {-122.4194, 37.7749}, byradius: {50, "km"}, asc: true)
["GEOSEARCH", "restaurants", "FROMLONLAT", "-122.4194", "37.7749", "BYRADIUS", "50", "km", "ASC"]

Compute the distance between two members:

iex> Redis.Commands.Geo.geodist("restaurants", "San Francisco", "New York", "mi")
["GEODIST", "restaurants", "San Francisco", "New York", "mi"]

Summary

Functions

Builds a GEOADD command to add members with longitude/latitude to a geo set.

Builds a GEODIST command to compute the distance between two members.

Builds a GEOPOS command to retrieve the longitude/latitude of one or more members.

Deprecated: use geosearch/2 instead.

Builds a GEOSEARCH command to query members within a geographic area.

Functions

geoadd(key, members, opts \\ [])

@spec geoadd(String.t(), [{float(), float(), String.t()}], keyword()) :: [String.t()]

Builds a GEOADD command to add members with longitude/latitude to a geo set.

Each member is a {longitude, latitude, name} tuple. Supports :nx (only add new members), :xx (only update existing), and :ch (return count of changed elements).

Example

iex> Redis.Commands.Geo.geoadd("places", [{13.361389, 38.115556, "Palermo"}], ch: true)
["GEOADD", "places", "CH", "13.361389", "38.115556", "Palermo"]

geodist(key, member1, member2, unit \\ nil)

@spec geodist(String.t(), String.t(), String.t(), String.t() | nil) :: [String.t()]

Builds a GEODIST command to compute the distance between two members.

The optional unit argument can be "m" (meters, default), "km", "mi", or "ft".

Examples

iex> Redis.Commands.Geo.geodist("places", "Palermo", "Catania")
["GEODIST", "places", "Palermo", "Catania"]

iex> Redis.Commands.Geo.geodist("places", "Palermo", "Catania", "km")
["GEODIST", "places", "Palermo", "Catania", "km"]

geohash(key, members)

@spec geohash(String.t(), [String.t()]) :: [String.t()]

geopos(key, members)

@spec geopos(String.t(), [String.t()]) :: [String.t()]

Builds a GEOPOS command to retrieve the longitude/latitude of one or more members.

Example

iex> Redis.Commands.Geo.geopos("places", ["Palermo", "Catania"])
["GEOPOS", "places", "Palermo", "Catania"]

georadius(key, longitude, latitude, radius, unit, opts \\ [])

@spec georadius(
  String.t(),
  float(),
  float(),
  float() | integer(),
  String.t(),
  keyword()
) :: [
  String.t()
]

Deprecated: use geosearch/2 instead.

georadiusbymember(key, member, radius, unit, opts \\ [])

@spec georadiusbymember(
  String.t(),
  String.t(),
  float() | integer(),
  String.t(),
  keyword()
) :: [
  String.t()
]

Deprecated: use geosearch/2 instead.

geosearch(key, opts \\ [])

@spec geosearch(
  String.t(),
  keyword()
) :: [String.t()]

Builds a GEOSEARCH command to query members within a geographic area.

Requires an origin (:fromlonlat or :frommember) and a shape (:byradius or :bybox). Supports ordering (:asc/:desc), result limiting (:count), and optional coordinate/distance/hash output (:withcoord, :withdist, :withhash).

Example

iex> Redis.Commands.Geo.geosearch("places", frommember: "Palermo", byradius: {200, "km"}, count: 5, withcoord: true)
["GEOSEARCH", "places", "FROMMEMBER", "Palermo", "BYRADIUS", "200", "km", "COUNT", "5", "WITHCOORD"]

geosearchstore(destination, source, opts \\ [])

@spec geosearchstore(String.t(), String.t(), keyword()) :: [String.t()]