From d5ad6d27a3f04b4baab23cdd53d94432ecd0455b Mon Sep 17 00:00:00 2001 From: "Maximilian P." Date: Thu, 14 Dec 2023 10:07:19 +0100 Subject: [PATCH 1/2] fix: geosearch command when count limit is set --- pkg/commands/geo_search.ts | 2 +- pkg/commands/geo_search_store.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/commands/geo_search.ts b/pkg/commands/geo_search.ts index 91cc04fb..ae237b24 100644 --- a/pkg/commands/geo_search.ts +++ b/pkg/commands/geo_search.ts @@ -84,7 +84,7 @@ export class GeoSearchCommand< command.push(order); if (opts?.count) { - command.push(opts.count.limit, ...(opts.count.any ? ["ANY"] : [])); + command.push("COUNT", opts.count.limit, ...(opts.count.any ? ["ANY"] : [])); } const transform = (result: string[] | string[][]) => { diff --git a/pkg/commands/geo_search_store.ts b/pkg/commands/geo_search_store.ts index 68f8000f..6884c21a 100644 --- a/pkg/commands/geo_search_store.ts +++ b/pkg/commands/geo_search_store.ts @@ -60,7 +60,7 @@ export class GeoSearchStoreCommand< command.push(order); if (opts?.count) { - command.push(opts.count.limit, ...(opts.count.any ? ["ANY"] : [])); + command.push("COUNT", opts.count.limit, ...(opts.count.any ? ["ANY"] : [])); } super([...command, ...(opts?.storeDist ? ["STOREDIST"] : [])], commandOptions); From a782fa664e44b5899efcdd8ebdfa541a512e4c32 Mon Sep 17 00:00:00 2001 From: "Maximilian P." Date: Thu, 14 Dec 2023 11:10:09 +0100 Subject: [PATCH 2/2] test: update for COUNT option on GEOSEARCH --- pkg/commands/geo_search.test.ts | 19 +++++++++++++++ pkg/commands/geo_search_store.test.ts | 34 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/commands/geo_search.test.ts b/pkg/commands/geo_search.test.ts index a8921958..437ddaaf 100644 --- a/pkg/commands/geo_search.test.ts +++ b/pkg/commands/geo_search.test.ts @@ -175,4 +175,23 @@ describe("GEOSEARCH tests", () => { }, ]); }); + + test("should return one member, with count set", async () => { + const key = newKey(); + await new GeoAddCommand([ + key, + { longitude: 13.361389, latitude: 38.115556, member: "Palermo" }, + { longitude: 15.087269, latitude: 37.502669, member: "Catania" }, + ]).exec(client); + + const res = await new GeoSearchCommand([ + key, + { type: "FROMLONLAT", coordinate: { lon: 15, lat: 37 } }, + { type: "BYRADIUS", radius: 200, radiusType: "KM" }, + "ASC", + { count: { limit: 1 } }, + ]).exec(client); + + expect(res).toEqual([{ member: "Catania" }]); + }); }); diff --git a/pkg/commands/geo_search_store.test.ts b/pkg/commands/geo_search_store.test.ts index 58e255ac..f388043a 100644 --- a/pkg/commands/geo_search_store.test.ts +++ b/pkg/commands/geo_search_store.test.ts @@ -116,4 +116,38 @@ describe("GEOSSEARCHSTORE tests", () => { ]); expect(res).toEqual(zrangeRes.length / 2); }); + + test("should return limited amount of members and store them in sorted set", async () => { + const key = newKey(); + const destination = newKey(); + + await new GeoAddCommand([ + key, + { longitude: -73.9857, latitude: 40.7488, member: "Empire State Building" }, + { longitude: -74.0445, latitude: 40.6892, member: "Statue of Liberty" }, + { longitude: -73.9632, latitude: 40.7789, member: "Central Park" }, + { longitude: -73.873, latitude: 40.7769, member: "LaGuardia Airport" }, + { longitude: -74.177, latitude: 40.6413, member: "JFK Airport" }, + { longitude: -73.9772, latitude: 40.7527, member: "Grand Central Terminal" }, + ]).exec(client); + + const res = await new GeoSearchStoreCommand([ + destination, + key, + { type: "FROMMEMBER", member: "Empire State Building" }, + { type: "BYRADIUS", radius: 5, radiusType: "KM" }, + "ASC", + { count: { limit: 2 } }, + ]).exec(client); + const zrangeRes = await new ZRangeCommand([destination, 0, -1, { withScores: true }]).exec( + client, + ); + expect(zrangeRes).toEqual([ + "Empire State Building", + 1791875672666387, + "Grand Central Terminal", + 1791875708058440, + ]); + expect(res).toEqual(zrangeRes.length / 2); + }); });