Skip to content

Commit 3be9f6b

Browse files
committed
adding geo search tile in solutions page
1 parent 43a25a5 commit 3be9f6b

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

Diff for: docs/howtos/solutions/geo/getting-started-geo/index-geo-search.mdx

+50-42
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ This section outlines the implementation of the `getStoreProductsByGeoFilter` AP
266266

267267
4. **Result Processing**: The function filters out duplicate products across different stores, ensuring unique product listings in the final output. It then formats the store locations into a more readable structure and compiles the final list of products to return.
268268

269-
```js
269+
```ts
270270
import * as StoreInventoryRepo from '../../../common/models/store-inventory-repo';
271271

272272
interface IInventoryBodyFilter {
@@ -276,7 +276,7 @@ interface IInventoryBodyFilter {
276276
userLocation?: {
277277
latitude?: number;
278278
longitude?: number;
279-
},
279+
};
280280
}
281281

282282
const searchStoreInventoryByGeoFilter = async (
@@ -286,13 +286,14 @@ const searchStoreInventoryByGeoFilter = async (
286286
const redisClient = getNodeRedisClient();
287287
const repository = StoreInventoryRepo.getRepository();
288288
let storeProducts: IStoreInventory[] = [];
289-
const trimmedStoreProducts: IStoreInventory[] = [] // similar item of other stores are removed
289+
const trimmedStoreProducts: IStoreInventory[] = []; // similar item of other stores are removed
290290
const uniqueProductIds = {};
291291

292-
if (repository
293-
&& _inventoryFilter?.userLocation?.latitude
294-
&& _inventoryFilter?.userLocation?.longitude) {
295-
292+
if (
293+
repository &&
294+
_inventoryFilter?.userLocation?.latitude &&
295+
_inventoryFilter?.userLocation?.longitude
296+
) {
296297
const lat = _inventoryFilter.userLocation.latitude;
297298
const long = _inventoryFilter.userLocation.longitude;
298299
const radiusInMiles = _inventoryFilter.searchRadiusInMiles || 500;
@@ -306,17 +307,13 @@ const searchStoreInventoryByGeoFilter = async (
306307
.gt(0)
307308
.and('storeLocation')
308309
.inRadius((circle) => {
309-
return circle
310-
.latitude(lat)
311-
.longitude(long)
312-
.radius(radiusInMiles)
313-
.miles
310+
return circle.latitude(lat).longitude(long).radius(radiusInMiles).miles;
314311
});
315312

316313
if (_inventoryFilter.productDisplayName) {
317314
queryBuilder = queryBuilder
318315
.and('productDisplayName')
319-
.matches(_inventoryFilter.productDisplayName)
316+
.matches(_inventoryFilter.productDisplayName);
320317
}
321318

322319
console.log(queryBuilder.query);
@@ -325,26 +322,38 @@ const searchStoreInventoryByGeoFilter = async (
325322
FT.SEARCH "storeInventory:storeInventoryId:index" "( ( ( (@statusCode:[1 1]) (@stockQty:[(0 +inf]) ) (@storeLocation:[-73.968285 40.785091 50 mi]) ) (@productDisplayName:'puma') )"
326323
*/
327324

328-
// (3) --- Executing the Query
325+
// (3) --- Executing the Query
329326
const indexName = `storeInventory:storeInventoryId:index`;
330327
const aggregator = await redisClient.ft.aggregate(
331328
indexName,
332329
queryBuilder.query,
333330
{
334-
LOAD: ["@storeId", "@storeName", "@storeLocation", "@productId", "@productDisplayName", "@stockQty"],
335-
STEPS: [{
336-
type: AggregateSteps.APPLY,
337-
expression: `geodistance(@storeLocation, ${long}, ${lat})/1609`,
338-
AS: 'distInMiles'
339-
}, {
340-
type: AggregateSteps.SORTBY,
341-
BY: ["@distInMiles", "@productId"]
342-
}, {
343-
type: AggregateSteps.LIMIT,
344-
from: 0,
345-
size: 1000,
346-
}]
347-
});
331+
LOAD: [
332+
'@storeId',
333+
'@storeName',
334+
'@storeLocation',
335+
'@productId',
336+
'@productDisplayName',
337+
'@stockQty',
338+
],
339+
STEPS: [
340+
{
341+
type: AggregateSteps.APPLY,
342+
expression: `geodistance(@storeLocation, ${long}, ${lat})/1609`,
343+
AS: 'distInMiles',
344+
},
345+
{
346+
type: AggregateSteps.SORTBY,
347+
BY: ['@distInMiles', '@productId'],
348+
},
349+
{
350+
type: AggregateSteps.LIMIT,
351+
from: 0,
352+
size: 1000,
353+
},
354+
],
355+
},
356+
);
348357

349358
/* Sample command to run on CLI
350359
FT.AGGREGATE "storeInventory:storeInventoryId:index"
@@ -360,37 +369,36 @@ const searchStoreInventoryByGeoFilter = async (
360369

361370
if (!storeProducts.length) {
362371
// throw `Product not found with in ${radiusInMiles}mi range!`;
363-
}
364-
else {
365-
366-
// (4) --- Result Processing
372+
} else {
373+
// (4) --- Result Processing
367374
storeProducts.forEach((storeProduct) => {
368-
if (storeProduct?.productId && !uniqueProductIds[storeProduct.productId]) {
375+
if (
376+
storeProduct?.productId &&
377+
!uniqueProductIds[storeProduct.productId]
378+
) {
369379
uniqueProductIds[storeProduct.productId] = true;
370380

371-
if (typeof storeProduct.storeLocation == "string") {
372-
const location = storeProduct.storeLocation.split(",");
381+
if (typeof storeProduct.storeLocation == 'string') {
382+
const location = storeProduct.storeLocation.split(',');
373383
storeProduct.storeLocation = {
374384
longitude: Number(location[0]),
375385
latitude: Number(location[1]),
376-
}
386+
};
377387
}
378388

379-
trimmedStoreProducts.push(storeProduct)
389+
trimmedStoreProducts.push(storeProduct);
380390
}
381391
});
382392
}
383-
}
384-
else {
385-
throw "Mandatory fields like userLocation latitude / longitude missing !"
393+
} else {
394+
throw 'Mandatory fields like userLocation latitude / longitude missing !';
386395
}
387396

388397
return {
389398
storeProducts: trimmedStoreProducts,
390-
productIds: Object.keys(uniqueProductIds)
399+
productIds: Object.keys(uniqueProductIds),
391400
};
392401
};
393-
394402
```
395403

396404
The implementation demonstrates a practical use case for Redis's Geo Location search capabilities, showcasing how to perform proximity searches combined with other filtering criteria (like product name) and present the results in a user-friendly format.

Diff for: docs/howtos/solutions/index-solutions.mdx

+14
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,17 @@ Learn how to easily build, test and deploy code for common microservice and cach
215215
</div>
216216

217217
</div>
218+
219+
## Geo Location Search
220+
221+
<div class="row">
222+
223+
<div class="col">
224+
<RedisCard
225+
title="Geo Location Search in Redis"
226+
description="Getting Started With Geo Location Search in Redis"
227+
page="/howtos/solutions/geo/getting-started"
228+
/>
229+
</div>
230+
231+
</div>

0 commit comments

Comments
 (0)