node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

geoRadius - How to?

Open asindl opened this issue 2 years ago • 5 comments

Description

I am running a 6.0.14 redis server (in Azure) and want to use the geoRadius search that includes the coordinates (WITHCOORD). It is really hard to find in the documentation how exactly the call would look like.

Any idea anyone?

asindl avatar Dec 21 '23 16:12 asindl

client.GEORADIUS_WITHCOORDS/client.geoRadiusWithCoords should do the trick

leibale avatar Dec 25 '23 09:12 leibale

Not sure, what that means. There is no geoRadiusWithCoords, at least to what I see....

asindl avatar Jan 02 '24 14:01 asindl

I suspect, those additional attributes are not implemented in the latest version. I checked the file @redis/client/dist/lib/commands/generic-transformers.js and updated the function pushGeoRadsArgumentsas follows

function pushGeoRadiusArguments(args, key, from, radius, unit, options) {
    var geoRadiusAdditionInfo = ["WITHDIST", "WITHCOORD", "WITHHASH"];
    args.push(key);
    if (typeof from === 'string') {
        args.push(from);
    }
    else {
        args.push(from.longitude.toString(), from.latitude.toString());
    }
    args.push(radius.toString(), unit);
    geoRadiusAdditionInfo.forEach(info => {
        if (options && options[info] && options[info] == true){
            args.push(info);
        }
    });
    if (options?.SORT) {
        args.push(options.SORT);
    }
    pushGeoCountArgument(args, options?.COUNT);
    return args;
}

So now, one could call client.geoRadius ('key', {longitude: location[0], latitude: location[1]}, radius, 'km',{WITHCOORD: true}); Could that be included in the lib?

asindl avatar Jan 02 '24 14:01 asindl

@asindl my bad, it's not client.GEORADIUS_WITHCOORDS it's client.GEORADIUS_WITH:

import { GeoReplyWith } from 'redis';

await client.GEORADIUS_WITH(
  'key',
  { longitude: location[0], latitude: location[1] },
  radius,
  'km',
  [GeoReplyWith.WITH_COORDS]
);

in general, when a command argument changes the reply type, its implemented as its own command (to allow for better types).

leibale avatar Jan 02 '24 17:01 leibale

Thanks a lot, @leibale! Small correction: the attribute name is COORDINATES': GeoReplyWith["COORDINATES"] = "WITHCOORD";`

So, you example needs to be

import { GeoReplyWith } from 'redis';

await client.GEORADIUS_WITH(
  'key',
  { longitude: location[0], latitude: location[1] },
  radius,
  'km',
  [GeoReplyWith.COORDINATES]
);

Now, it works like a charm!

Since I suspected, it would work "somehow", I labeled this as "documentation". Is there any documentation about the usage of this including the response types? If not, it would be helpful to add it...

Thanks again!

asindl avatar Jan 03 '24 09:01 asindl