1
1
// EXAMPLE: query_geo
2
2
// HIDE_START
3
- import assert from 'assert' ;
4
- import fs from 'fs' ;
3
+ import assert from 'node: assert' ;
4
+ import fs from 'node: fs' ;
5
5
import { createClient } from 'redis' ;
6
6
import { SchemaFieldTypes } from '@redis/search' ;
7
7
8
8
const client = createClient ( ) ;
9
9
10
- await client . connect ( ) ;
10
+ await client . connect ( ) . catch ( console . error ) ;
11
11
12
12
// create index
13
13
await client . ft . create ( 'idx:bicycle' , {
14
14
'$.store_location' : {
15
15
type : SchemaFieldTypes . GEO ,
16
16
AS : 'store_location'
17
17
} ,
18
- '$.pickup_zone' : {
18
+ '$.pickup_zone' : {
19
19
type : SchemaFieldTypes . GEOSHAPE ,
20
20
AS : 'pickup_zone'
21
21
}
22
22
} , {
23
- ON : 'JSON' ,
23
+ ON : 'JSON' ,
24
24
PREFIX : 'bicycle:'
25
25
} )
26
26
@@ -35,31 +35,31 @@ await Promise.all(
35
35
// HIDE_END
36
36
37
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] } ]}
38
+ const res1 = await client . ft . search ( 'idx:bicycle' , '@store_location:[-0.1778 51.5524 20 mi]' ) ;
39
+ console . log ( res1 . total ) ; // >>> 1
40
+ console . log ( res1 ) ; // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]}
41
41
// REMOVE_START
42
- assert . strictEqual ( res . total , 1 ) ;
42
+ assert . strictEqual ( res1 . total , 1 ) ;
43
43
// REMOVE_END
44
44
// STEP_END
45
45
46
46
// STEP_START geo2
47
47
const params_dict_geo2 = { bike : 'POINT(-0.1278 51.5074)' } ;
48
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] } ]}
49
+ const res2 = await client . ft . search ( 'idx:bicycle' , q_geo2 , { PARAMS : params_dict_geo2 , DIALECT : 3 } ) ;
50
+ console . log ( res2 . total ) ; // >>> 1
51
+ console . log ( res2 ) ; // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]}
52
52
// REMOVE_START
53
- assert . strictEqual ( res . total , 1 ) ;
53
+ assert . strictEqual ( res2 . total , 1 ) ;
54
54
// REMOVE_END
55
55
// STEP_END
56
56
57
57
// STEP_START geo3
58
58
const params_dict_geo3 = { europe : 'POLYGON((-25 35, 40 35, 40 70, -25 70, -25 35))' } ;
59
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 ) ; // >>>
60
+ const res3 = await client . ft . search ( 'idx:bicycle' , q_geo3 , { PARAMS : params_dict_geo3 , DIALECT : 3 } ) ;
61
+ console . log ( res3 . total ) ; // >>> 5
62
+ console . log ( res3 ) ; // >>>
63
63
// {
64
64
// total: 5,
65
65
// documents: [
@@ -71,7 +71,7 @@ console.log(res); // >>>
71
71
// ]
72
72
// }
73
73
// REMOVE_START
74
- assert . strictEqual ( res . total , 5 ) ;
74
+ assert . strictEqual ( res3 . total , 5 ) ;
75
75
// REMOVE_END
76
76
// STEP_END
77
77
0 commit comments