Module: Valkey::Commands::GeoCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/geo_commands.rb

Overview

This module contains commands on geospatial operations.

Instance Method Summary collapse

Instance Method Details

#geoadd(key, *members) ⇒ Integer

Add one or more geospatial items (longitude, latitude, name) to a key.

Examples:

valkey.geoadd("locations", 13.361389, 38.115556, "Palermo", 15.087269, 37.502669, "Catania")
  # => Integer (number of elements added)

Parameters:

  • key (String)

    the name of the key

  • members (Array<String, Float>)

    one or more longitude, latitude, and name triplets

Returns:

  • (Integer)

    the number of elements added



19
20
21
# File 'lib/valkey/commands/geo_commands.rb', line 19

def geoadd(key, *members)
  send_command(RequestType::GEO_ADD, [key, *members])
end

#geodist(key, member1, member2, unit = 'm') ⇒ String?

Returns the distance between two members of a geospatial index

Parameters:

  • key (String)
  • members (Array<String>)
  • unit ('m', 'km', 'mi', 'ft') (defaults to: 'm')

Returns:

  • (String, nil)

    returns distance in specified unit if both members present, nil otherwise.



51
52
53
# File 'lib/valkey/commands/geo_commands.rb', line 51

def geodist(key, member1, member2, unit = 'm')
  send_command(RequestType::GEO_DIST, [key, member1, member2, unit])
end

#geohash(key, *member) ⇒ Array<String, nil>

Returns geohash string representing position for specified members of the specified key.

Parameters:

  • key (String)
  • member (String, Array<String>)

    one member or array of members

Returns:

  • (Array<String, nil>)

    returns array containg geohash string if member is present, nil otherwise



41
42
43
# File 'lib/valkey/commands/geo_commands.rb', line 41

def geohash(key, *member)
  send_command(RequestType::GEO_HASH, [key, *member])
end

#geopos(key, *members) ⇒ Array<Array<Float, Float>, nil>

Retrieve the positions (longitude, latitude) of one or more elements.

Examples:

valkey.geopos("locations", "Palermo", "Catania")
  # => [[13.361389, 38.115556], [15.087269, 37.502669]]

Parameters:

  • key (String)

    the name of the key

  • members (Array<String>)

    one or more member names to get positions for

Returns:

  • (Array<Array<Float, Float>, nil>)

    list of positions or nil for missing members



32
33
34
# File 'lib/valkey/commands/geo_commands.rb', line 32

def geopos(key, *members)
  send_command(RequestType::GEO_POS, [key, *members])
end

#geosearch(*args) ⇒ Array

Perform raw GEOSEARCH command with direct arguments like Redis

Examples:

valkey.geosearch("places", "FROMMEMBER", "berlin", "BYRADIUS", 1000, "km", "WITHDIST")

Parameters:

  • args (Array<String>)

    full argument list for GEOSEARCH

Returns:

  • (Array)

    raw result from server



62
63
64
# File 'lib/valkey/commands/geo_commands.rb', line 62

def geosearch(*args)
  send_command(RequestType::GEO_SEARCH, args)
end

#geosearchstore(destination, source, *args) ⇒ Integer

Store the result of a GEOSEARCH query into a new sorted set key.

Examples:

valkey.geosearchstore(
  "nearby:berlin",       # destination key
  "Places",               # source key
  "FROMMEMBER", "Berlin",
  "BYRADIUS", 200, "km",
  "ASC", "COUNT", 10
)
# => 2 (number of items stored)

Parameters:

  • destination (String)

    the name of the key where results will be stored

  • source (String)

    the name of the source geo key to search from

  • args (Array<String, Integer>)

    full argument list like GEOSEARCH (e.g., FROMMEMBER, BYRADIUS, COUNT, etc.)

Returns:

  • (Integer)

    the number of items stored in the destination key



82
83
84
# File 'lib/valkey/commands/geo_commands.rb', line 82

def geosearchstore(destination, source, *args)
  send_command(RequestType::GEO_SEARCH_STORE, [destination, source, *args])
end