Skip to content

Commit 714b3f9

Browse files
authored
Merge pull request #638 from wjohnsto/master
redirect to learn (client-side)
2 parents 1faafd5 + 73b934e commit 714b3f9

File tree

4 files changed

+116
-53
lines changed

4 files changed

+116
-53
lines changed

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.

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>

docusaurus.config.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22

33
module.exports = {
4+
clientModules: ['./redirect.js'],
45
title: 'The Home of Redis Developers',
56
tagline:
67
'Learn all the best practices to get up and running with Redis in no time. Get started and discover the power of Redis, whether on your local machines or in the cloud.',
@@ -181,7 +182,7 @@ module.exports = {
181182
name: 'Tugdual Grall',
182183
title: 'Former Technical Marketing Manager at Redis',
183184
image: 'profile_pic_tugdual_grall.png',
184-
}
185+
},
185186
},
186187
},
187188
themeConfig:
@@ -202,7 +203,7 @@ module.exports = {
202203
'elixir',
203204
'groovy',
204205
'sql',
205-
'typescript'
206+
'typescript',
206207
],
207208
},
208209

@@ -298,14 +299,14 @@ module.exports = {
298299
// Useful if you want to support a single color mode
299300
disableSwitch: false,
300301
},
301-
announcementBar: {
302-
id: 'redis-7-2-release', // Any value that will identify this message.
303-
content:
304-
'<div class="announcement-bar"><a href="https://redis.com/blog/introducing-redis-7-2/" target="_blank" rel="noopener"><span>Announcing Redis 7.2 and Enhanced Vector DB</span> <span style="margin-left:1rem">Learn more</span> <span style="margin-left:0.25rem">→</span></a></div>',
305-
backgroundColor: 'rgb(210, 215, 254)', // Defaults to `#fff`.
306-
textColor: 'rgb(22 31 49)', // Defaults to `#000`.
307-
isCloseable: true, // Defaults to `true`.
308-
},
302+
announcementBar: {
303+
id: 'redis-7-2-release', // Any value that will identify this message.
304+
content:
305+
'<div class="announcement-bar"><a href="https://redis.com/blog/introducing-redis-7-2/" target="_blank" rel="noopener"><span>Announcing Redis 7.2 and Enhanced Vector DB</span> <span style="margin-left:1rem">Learn more</span> <span style="margin-left:0.25rem">→</span></a></div>',
306+
backgroundColor: 'rgb(210, 215, 254)', // Defaults to `#fff`.
307+
textColor: 'rgb(22 31 49)', // Defaults to `#000`.
308+
isCloseable: true, // Defaults to `true`.
309+
},
309310
}),
310311
presets: [
311312
[
@@ -354,5 +355,5 @@ module.exports = {
354355
plugins: [
355356
'docusaurus-plugin-sass',
356357
path.resolve(__dirname, 'plugins', 'gtm'),
357-
]
358+
],
358359
};

redirect.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
2+
3+
function tryRedirect() {
4+
if (location.host.includes('learn.')) {
5+
return;
6+
}
7+
8+
let path = location.pathname;
9+
10+
if (!path.startsWith('/')) {
11+
path = `/${path}`;
12+
}
13+
14+
location.replace(`https://redis.io/learn${path}`);
15+
}
16+
17+
export default (function () {
18+
if (!ExecutionEnvironment.canUseDOM) {
19+
return null;
20+
}
21+
22+
return {
23+
onRouteUpdate({ location }) {
24+
console.log(location);
25+
26+
tryRedirect();
27+
setTimeout(tryRedirect, 50);
28+
setTimeout(tryRedirect, 150);
29+
setTimeout(tryRedirect, 250);
30+
setTimeout(tryRedirect, 350);
31+
setTimeout(tryRedirect, 450);
32+
setTimeout(tryRedirect, 550);
33+
setTimeout(tryRedirect, 650);
34+
setTimeout(tryRedirect, 750);
35+
setTimeout(tryRedirect, 850);
36+
setTimeout(tryRedirect, 950);
37+
setTimeout(tryRedirect, 1050);
38+
},
39+
};
40+
})();

0 commit comments

Comments
 (0)