1
1
// EXAMPLE: query_vector
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 , VectorAlgorithms } from '@redis/search' ;
7
7
import { pipeline } from '@xenova/transformers' ;
8
8
9
- const float32Buffer = ( arr ) => {
9
+ function float32Buffer ( arr ) {
10
10
const floatArray = new Float32Array ( arr ) ;
11
11
const float32Buffer = Buffer . from ( floatArray . buffer ) ;
12
12
return float32Buffer ;
13
- } ;
13
+ }
14
14
15
15
async function embedText ( sentence ) {
16
16
let modelName = 'Xenova/all-MiniLM-L6-v2' ;
@@ -26,28 +26,28 @@ async function embedText(sentence) {
26
26
return embedding ;
27
27
}
28
28
29
- let query = " Bike for small kids" ;
30
- let vector_query = float32Buffer ( await embedText ( " That is a very happy person" ) ) ;
29
+ let query = ' Bike for small kids' ;
30
+ let vector_query = float32Buffer ( await embedText ( ' That is a very happy person' ) ) ;
31
31
32
32
const client = createClient ( ) ;
33
- await client . connect ( ) ;
33
+ await client . connect ( ) . catch ( console . error ) ;
34
34
35
35
// create index
36
36
await client . ft . create ( 'idx:bicycle' , {
37
37
'$.description' : {
38
38
type : SchemaFieldTypes . TEXT ,
39
- sortable : false
39
+ AS : 'description'
40
40
} ,
41
41
'$.description_embeddings' : {
42
42
type : SchemaFieldTypes . VECTOR ,
43
43
TYPE : 'FLOAT32' ,
44
44
ALGORITHM : VectorAlgorithms . FLAT ,
45
45
DIM : 384 ,
46
46
DISTANCE_METRIC : 'COSINE' ,
47
- AS : 'vector' ,
47
+ AS : 'vector'
48
48
}
49
49
} , {
50
- ON : 'JSON' ,
50
+ ON : 'JSON' ,
51
51
PREFIX : 'bicycle:'
52
52
} ) ;
53
53
@@ -62,15 +62,15 @@ await Promise.all(
62
62
// HIDE_END
63
63
64
64
// STEP_START vector1
65
- let res = await client . ft . search ( 'idx:bicycle' ,
65
+ const res1 = await client . ft . search ( 'idx:bicycle' ,
66
66
'*=>[KNN 3 @vector $query_vector AS score]' , {
67
67
PARAMS : { query_vector : vector_query } ,
68
68
RETURN : [ 'description' ] ,
69
69
DIALECT : 2
70
70
}
71
71
) ;
72
- console . log ( res . total ) ; // >>> 3
73
- console . log ( res ) ; // >>>
72
+ console . log ( res1 . total ) ; // >>> 3
73
+ console . log ( res1 ) ; // >>>
74
74
//{
75
75
// total: 3,
76
76
// documents: [
@@ -80,27 +80,27 @@ console.log(res); // >>>
80
80
// ]
81
81
//}
82
82
// REMOVE_START
83
- assert . strictEqual ( res . total , 3 ) ;
83
+ assert . strictEqual ( res1 . total , 3 ) ;
84
84
// REMOVE_END
85
85
// STEP_END
86
86
87
87
// STEP_START vector2
88
- res = await client . ft . search ( 'idx:bicycle' ,
88
+ const res2 = await client . ft . search ( 'idx:bicycle' ,
89
89
'@vector:[VECTOR_RANGE 0.9 $query_vector]=>{$YIELD_DISTANCE_AS: vector_dist}' , {
90
90
PARAMS : { query_vector : vector_query } ,
91
91
SORTBY : 'vector_dist' ,
92
92
RETURN : [ 'vector_dist' , 'description' ] ,
93
93
DIALECT : 2
94
94
}
95
95
) ;
96
- console . log ( res . total ) ; // >>> 1
97
- console . log ( res ) ; // >>>
96
+ console . log ( res2 . total ) ; // >>> 1
97
+ console . log ( res2 ) ; // >>>
98
98
//{
99
99
// total: 1,
100
100
// documents: [ { id: 'bicycle:0', value: [Object: null prototype] } ]
101
101
//}
102
102
// REMOVE_START
103
- assert . strictEqual ( res . total , 1 ) ;
103
+ assert . strictEqual ( res2 . total , 1 ) ;
104
104
// REMOVE_END
105
105
// STEP_END
106
106
0 commit comments