Skip to content

Commit b3be5fc

Browse files
committed
DOC-4197: add TCEs to the geospatial query page
1 parent 06e91d0 commit b3be5fc

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

doctests/query-geo.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// EXAMPLE: query_geo
2+
// HIDE_START
3+
import assert from 'assert';
4+
import fs from 'fs';
5+
import { createClient } from 'redis';
6+
import { SchemaFieldTypes } from '@redis/search';
7+
8+
const client = createClient();
9+
10+
await client.connect();
11+
12+
// create index
13+
await client.ft.create('idx:bicycle', {
14+
'$.store_location': {
15+
type: SchemaFieldTypes.GEO,
16+
AS: 'store_location'
17+
},
18+
'$.pickup_zone': {
19+
type: SchemaFieldTypes.GEOSHAPE,
20+
AS: 'pickup_zone'
21+
}
22+
}, {
23+
ON: 'JSON',
24+
PREFIX: 'bicycle:'
25+
})
26+
27+
// load data
28+
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
29+
30+
await Promise.all(
31+
bicycles.map((bicycle, bid) => {
32+
return client.json.set(`bicycle:${bid}`, '$', bicycle);
33+
})
34+
);
35+
// HIDE_END
36+
37+
// STEP_START geo1
38+
let res = await client.ft.search('idx:bicycle', '@store_location:[-0.1778 51.5524 20 mi]');
39+
console.log(res.total); // >>> 1
40+
console.log(res); // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]}
41+
// REMOVE_START
42+
assert.strictEqual(res.total, 1);
43+
// REMOVE_END
44+
// STEP_END
45+
46+
// STEP_START geo2
47+
const params_dict_geo2 = { bike: 'POINT(-0.1278 51.5074)' };
48+
const q_geo2 = '@pickup_zone:[CONTAINS $bike]';
49+
res = await client.ft.search('idx:bicycle', q_geo2, { PARAMS: params_dict_geo2, DIALECT: 3 });
50+
console.log(res.total); // >>> 1
51+
console.log(res); // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]}
52+
// REMOVE_START
53+
assert.strictEqual(res.total, 1);
54+
// REMOVE_END
55+
// STEP_END
56+
57+
// STEP_START geo3
58+
const params_dict_geo3 = { europe: 'POLYGON((-25 35, 40 35, 40 70, -25 70, -25 35))' };
59+
const q_geo3 = '@pickup_zone:[WITHIN $europe]';
60+
res = await client.ft.search('idx:bicycle', q_geo3, { PARAMS: params_dict_geo3, DIALECT: 3 });
61+
console.log(res.total); // >>> 5
62+
console.log(res); // >>>
63+
// {
64+
// total: 5,
65+
// documents: [
66+
// { id: 'bicycle:5', value: [Object: null prototype] },
67+
// { id: 'bicycle:6', value: [Object: null prototype] },
68+
// { id: 'bicycle:7', value: [Object: null prototype] },
69+
// { id: 'bicycle:8', value: [Object: null prototype] },
70+
// { id: 'bicycle:9', value: [Object: null prototype] }
71+
// ]
72+
// }
73+
// REMOVE_START
74+
assert.strictEqual(res.total, 5);
75+
// REMOVE_END
76+
// STEP_END
77+
78+
// REMOVE_START
79+
// destroy index and data
80+
await client.ft.dropIndex('idx:bicycle', { DD: true });
81+
await client.disconnect();
82+
// REMOVE_END

0 commit comments

Comments
 (0)