Debian Codegraph @

packages/search/lib/commands/index.ts

raw
1 import * as _LIST from './_LIST';
2 import * as ALTER from './ALTER';
3 import * as AGGREGATE_WITHCURSOR from './AGGREGATE_WITHCURSOR';
4 import * as AGGREGATE from './AGGREGATE';
5 import * as ALIASADD from './ALIASADD';
6 import * as ALIASDEL from './ALIASDEL';
7 import * as ALIASUPDATE from './ALIASUPDATE';
8 import * as CONFIG_GET from './CONFIG_GET';
9 import * as CONFIG_SET from './CONFIG_SET';
10 import * as CREATE from './CREATE';
11 import * as CURSOR_DEL from './CURSOR_DEL';
12 import * as CURSOR_READ from './CURSOR_READ';
13 import * as DICTADD from './DICTADD';
14 import * as DICTDEL from './DICTDEL';
15 import * as DICTDUMP from './DICTDUMP';
16 import * as DROPINDEX from './DROPINDEX';
17 import * as EXPLAIN from './EXPLAIN';
18 import * as EXPLAINCLI from './EXPLAINCLI';
19 import * as INFO from './INFO';
20 import * as PROFILESEARCH from './PROFILE_SEARCH';
21 import * as PROFILEAGGREGATE from './PROFILE_AGGREGATE';
22 import * as SEARCH from './SEARCH';
23 import * as SEARCH_NOCONTENT from './SEARCH_NOCONTENT';
24 import * as SPELLCHECK from './SPELLCHECK';
25 import * as SUGADD from './SUGADD';
26 import * as SUGDEL from './SUGDEL';
27 import * as SUGGET_WITHPAYLOADS from './SUGGET_WITHPAYLOADS';
28 import * as SUGGET_WITHSCORES_WITHPAYLOADS from './SUGGET_WITHSCORES_WITHPAYLOADS';
29 import * as SUGGET_WITHSCORES from './SUGGET_WITHSCORES';
30 import * as SUGGET from './SUGGET';
31 import * as SUGLEN from './SUGLEN';
32 import * as SYNDUMP from './SYNDUMP';
33 import * as SYNUPDATE from './SYNUPDATE';
34 import * as TAGVALS from './TAGVALS';
35 import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
36 import { pushOptionalVerdictArgument, pushVerdictArgument } from '@redis/client/dist/lib/commands/generic-transformers';
37 import { SearchOptions } from './SEARCH';
38
39 export default {
40 _LIST,
41 _list: _LIST,
42 ALTER,
43 alter: ALTER,
44 AGGREGATE_WITHCURSOR,
45 aggregateWithCursor: AGGREGATE_WITHCURSOR,
46 AGGREGATE,
47 aggregate: AGGREGATE,
48 ALIASADD,
49 aliasAdd: ALIASADD,
50 ALIASDEL,
51 aliasDel: ALIASDEL,
52 ALIASUPDATE,
53 aliasUpdate: ALIASUPDATE,
54 CONFIG_GET,
55 configGet: CONFIG_GET,
56 CONFIG_SET,
57 configSet: CONFIG_SET,
58 CREATE,
59 create: CREATE,
60 CURSOR_DEL,
61 cursorDel: CURSOR_DEL,
62 CURSOR_READ,
63 cursorRead: CURSOR_READ,
64 DICTADD,
65 dictAdd: DICTADD,
66 DICTDEL,
67 dictDel: DICTDEL,
68 DICTDUMP,
69 dictDump: DICTDUMP,
70 DROPINDEX,
71 dropIndex: DROPINDEX,
72 EXPLAIN,
73 explain: EXPLAIN,
74 EXPLAINCLI,
75 explainCli: EXPLAINCLI,
76 INFO,
77 info: INFO,
78 PROFILESEARCH,
79 profileSearch: PROFILESEARCH,
80 PROFILEAGGREGATE,
81 profileAggregate: PROFILEAGGREGATE,
82 SEARCH,
83 search: SEARCH,
84 SEARCH_NOCONTENT,
85 searchNoContent: SEARCH_NOCONTENT,
86 SPELLCHECK,
87 spellCheck: SPELLCHECK,
88 SUGADD,
89 sugAdd: SUGADD,
90 SUGDEL,
91 sugDel: SUGDEL,
92 SUGGET_WITHPAYLOADS,
93 sugGetWithPayloads: SUGGET_WITHPAYLOADS,
94 SUGGET_WITHSCORES_WITHPAYLOADS,
95 sugGetWithScoresWithPayloads: SUGGET_WITHSCORES_WITHPAYLOADS,
96 SUGGET_WITHSCORES,
97 sugGetWithScores: SUGGET_WITHSCORES,
98 SUGGET,
99 sugGet: SUGGET,
100 SUGLEN,
101 sugLen: SUGLEN,
102 SYNDUMP,
103 synDump: SYNDUMP,
104 SYNUPDATE,
105 synUpdate: SYNUPDATE,
106 TAGVALS,
107 tagVals: TAGVALS
108 };
109
110 export enum RedisSearchLanguages {
111 ARABIC = 'Arabic',
112 BASQUE = 'Basque',
113 CATALANA = 'Catalan',
114 DANISH = 'Danish',
115 DUTCH = 'Dutch',
116 ENGLISH = 'English',
117 FINNISH = 'Finnish',
118 FRENCH = 'French',
119 GERMAN = 'German',
120 GREEK = 'Greek',
121 HUNGARIAN = 'Hungarian',
122 INDONESAIN = 'Indonesian',
123 IRISH = 'Irish',
124 ITALIAN = 'Italian',
125 LITHUANIAN = 'Lithuanian',
126 NEPALI = 'Nepali',
127 NORWEIGAN = 'Norwegian',
128 PORTUGUESE = 'Portuguese',
129 ROMANIAN = 'Romanian',
130 RUSSIAN = 'Russian',
131 SPANISH = 'Spanish',
132 SWEDISH = 'Swedish',
133 TAMIL = 'Tamil',
134 TURKISH = 'Turkish',
135 CHINESE = 'Chinese'
136 }
137
138 export type PropertyName = `${'@' | '$.'}${string}`;
139
140 export type SortByProperty = string | {
141 BY: string;
142 DIRECTION?: 'ASC' | 'DESC';
143 };
144
145 export function pushSortByProperty(args: RedisCommandArguments, sortBy: SortByProperty): void {
146 if (typeof sortBy === 'string') {
147 args.push(sortBy);
148 } else {
149 args.push(sortBy.BY);
150
151 if (sortBy.DIRECTION) {
152 args.push(sortBy.DIRECTION);
153 }
154 }
155 }
156
157 export function pushSortByArguments(args: RedisCommandArguments, name: string, sortBy: SortByProperty | Array<SortByProperty>): RedisCommandArguments {
158 const lengthBefore = args.push(
159 name,
160 '' // will be overwritten
161 );
162
163 if (Array.isArray(sortBy)) {
164 for (const field of sortBy) {
165 pushSortByProperty(args, field);
166 }
167 } else {
168 pushSortByProperty(args, sortBy);
169 }
170
171 args[lengthBefore - 1] = (args.length - lengthBefore).toString();
172
173 return args;
174 }
175
176 export function pushArgumentsWithLength(args: RedisCommandArguments, fn: (args: RedisCommandArguments) => void): RedisCommandArguments {
177 const lengthIndex = args.push('') - 1;
178 fn(args);
179 args[lengthIndex] = (args.length - lengthIndex - 1).toString();
180 return args;
181 }
182
183 export enum SchemaFieldTypes {
184 TEXT = 'TEXT',
185 NUMERIC = 'NUMERIC',
186 GEO = 'GEO',
187 TAG = 'TAG',
188 VECTOR = 'VECTOR'
189 }
190
191 type CreateSchemaField<
192 T extends SchemaFieldTypes,
193 E = Record<PropertyKey, unknown>
194 > = T | ({
195 type: T;
196 AS?: string;
197 } & E);
198
199 type CreateSchemaCommonField<
200 T extends SchemaFieldTypes,
201 E = Record<PropertyKey, unknown>
202 > = CreateSchemaField<
203 T,
204 ({
205 SORTABLE?: true | 'UNF';
206 NOINDEX?: true;
207 } & E)
208 >;
209
210 export enum SchemaTextFieldPhonetics {
211 DM_EN = 'dm:en',
212 DM_FR = 'dm:fr',
213 FM_PT = 'dm:pt',
214 DM_ES = 'dm:es'
215 }
216
217 type CreateSchemaTextField = CreateSchemaCommonField<SchemaFieldTypes.TEXT, {
218 NOSTEM?: true;
219 WEIGHT?: number;
220 PHONETIC?: SchemaTextFieldPhonetics;
221 WITHSUFFIXTRIE?: boolean;
222 }>;
223
224 type CreateSchemaNumericField = CreateSchemaCommonField<SchemaFieldTypes.NUMERIC>;
225
226 type CreateSchemaGeoField = CreateSchemaCommonField<SchemaFieldTypes.GEO>;
227
228 type CreateSchemaTagField = CreateSchemaCommonField<SchemaFieldTypes.TAG, {
229 SEPARATOR?: string;
230 CASESENSITIVE?: true;
231 WITHSUFFIXTRIE?: boolean;
232 }>;
233
234 export enum VectorAlgorithms {
235 FLAT = 'FLAT',
236 HNSW = 'HNSW'
237 }
238
239 type CreateSchemaVectorField<
240 T extends VectorAlgorithms,
241 A extends Record<string, unknown>
242 > = CreateSchemaField<SchemaFieldTypes.VECTOR, {
243 ALGORITHM: T;
244 TYPE: string;
245 DIM: number;
246 DISTANCE_METRIC: 'L2' | 'IP' | 'COSINE';
247 INITIAL_CAP?: number;
248 } & A>;
249
250 type CreateSchemaFlatVectorField = CreateSchemaVectorField<VectorAlgorithms.FLAT, {
251 BLOCK_SIZE?: number;
252 }>;
253
254 type CreateSchemaHNSWVectorField = CreateSchemaVectorField<VectorAlgorithms.HNSW, {
255 M?: number;
256 EF_CONSTRUCTION?: number;
257 EF_RUNTIME?: number;
258 }>;
259
260 export interface RediSearchSchema {
261 [field: string]:
262 CreateSchemaTextField |
263 CreateSchemaNumericField |
264 CreateSchemaGeoField |
265 CreateSchemaTagField |
266 CreateSchemaFlatVectorField |
267 CreateSchemaHNSWVectorField;
268 }
269
270 export function pushSchema(args: RedisCommandArguments, schema: RediSearchSchema) {
271 for (const [field, fieldOptions] of Object.entries(schema)) {
272 args.push(field);
273
274 if (typeof fieldOptions === 'string') {
275 args.push(fieldOptions);
276 continue;
277 }
278
279 if (fieldOptions.AS) {
280 args.push('AS', fieldOptions.AS);
281 }
282
283 args.push(fieldOptions.type);
284
285 switch (fieldOptions.type) {
286 case SchemaFieldTypes.TEXT:
287 if (fieldOptions.NOSTEM) {
288 args.push('NOSTEM');
289 }
290
291 if (fieldOptions.WEIGHT) {
292 args.push('WEIGHT', fieldOptions.WEIGHT.toString());
293 }
294
295 if (fieldOptions.PHONETIC) {
296 args.push('PHONETIC', fieldOptions.PHONETIC);
297 }
298
299 if (fieldOptions.WITHSUFFIXTRIE) {
300 args.push('WITHSUFFIXTRIE');
301 }
302
303 break;
304
305 // case SchemaFieldTypes.NUMERIC:
306 // case SchemaFieldTypes.GEO:
307 // break;
308
309 case SchemaFieldTypes.TAG:
310 if (fieldOptions.SEPARATOR) {
311 args.push('SEPARATOR', fieldOptions.SEPARATOR);
312 }
313
314 if (fieldOptions.CASESENSITIVE) {
315 args.push('CASESENSITIVE');
316 }
317
318 if (fieldOptions.WITHSUFFIXTRIE) {
319 args.push('WITHSUFFIXTRIE');
320 }
321
322 break;
323
324 case SchemaFieldTypes.VECTOR:
325 args.push(fieldOptions.ALGORITHM);
326
327 pushArgumentsWithLength(args, () => {
328 args.push(
329 'TYPE', fieldOptions.TYPE,
330 'DIM', fieldOptions.DIM.toString(),
331 'DISTANCE_METRIC', fieldOptions.DISTANCE_METRIC
332 );
333
334 if (fieldOptions.INITIAL_CAP) {
335 args.push('INITIAL_CAP', fieldOptions.INITIAL_CAP.toString());
336 }
337
338 switch (fieldOptions.ALGORITHM) {
339 case VectorAlgorithms.FLAT:
340 if (fieldOptions.BLOCK_SIZE) {
341 args.push('BLOCK_SIZE', fieldOptions.BLOCK_SIZE.toString());
342 }
343
344 break;
345
346 case VectorAlgorithms.HNSW:
347 if (fieldOptions.M) {
348 args.push('M', fieldOptions.M.toString());
349 }
350
351 if (fieldOptions.EF_CONSTRUCTION) {
352 args.push('EF_CONSTRUCTION', fieldOptions.EF_CONSTRUCTION.toString());
353 }
354
355 if (fieldOptions.EF_RUNTIME) {
356 args.push('EF_RUNTIME', fieldOptions.EF_RUNTIME.toString());
357 }
358
359 break;
360 }
361 });
362
363 continue; // vector fields do not contain SORTABLE and NOINDEX options
364 }
365
366 if (fieldOptions.SORTABLE) {
367 args.push('SORTABLE');
368
369 if (fieldOptions.SORTABLE === 'UNF') {
370 args.push('UNF');
371 }
372 }
373
374 if (fieldOptions.NOINDEX) {
375 args.push('NOINDEX');
376 }
377 }
378 }
379
380 export type Params = Record<string, RedisCommandArgument | number>;
381
382 export function pushParamsArgs(
383 args: RedisCommandArguments,
384 params?: Params
385 ): RedisCommandArguments {
386 if (params) {
387 const enrties = Object.entries(params);
388 args.push('PARAMS', (enrties.length * 2).toString());
389 for (const [key, value] of enrties) {
390 args.push(key, typeof value === 'number' ? value.toString() : value);
391 }
392 }
393
394 return args;
395 }
396
397 export function pushSearchOptions(
398 args: RedisCommandArguments,
399 options?: SearchOptions
400 ): RedisCommandArguments {
401 if (options?.VERBATIM) {
402 args.push('VERBATIM');
403 }
404
405 if (options?.NOSTOPWORDS) {
406 args.push('NOSTOPWORDS');
407 }
408
409 // if (options?.WITHSCORES) {
410 // args.push('WITHSCORES');
411 // }
412
413 // if (options?.WITHPAYLOADS) {
414 // args.push('WITHPAYLOADS');
415 // }
416
417 pushOptionalVerdictArgument(args, 'INKEYS', options?.INKEYS);
418 pushOptionalVerdictArgument(args, 'INFIELDS', options?.INFIELDS);
419 pushOptionalVerdictArgument(args, 'RETURN', options?.RETURN);
420
421 if (options?.SUMMARIZE) {
422 args.push('SUMMARIZE');
423
424 if (typeof options.SUMMARIZE === 'object') {
425 if (options.SUMMARIZE.FIELDS) {
426 args.push('FIELDS');
427 pushVerdictArgument(args, options.SUMMARIZE.FIELDS);
428 }
429
430 if (options.SUMMARIZE.FRAGS) {
431 args.push('FRAGS', options.SUMMARIZE.FRAGS.toString());
432 }
433
434 if (options.SUMMARIZE.LEN) {
435 args.push('LEN', options.SUMMARIZE.LEN.toString());
436 }
437
438 if (options.SUMMARIZE.SEPARATOR) {
439 args.push('SEPARATOR', options.SUMMARIZE.SEPARATOR);
440 }
441 }
442 }
443
444 if (options?.HIGHLIGHT) {
445 args.push('HIGHLIGHT');
446
447 if (typeof options.HIGHLIGHT === 'object') {
448 if (options.HIGHLIGHT.FIELDS) {
449 args.push('FIELDS');
450 pushVerdictArgument(args, options.HIGHLIGHT.FIELDS);
451 }
452
453 if (options.HIGHLIGHT.TAGS) {
454 args.push('TAGS', options.HIGHLIGHT.TAGS.open, options.HIGHLIGHT.TAGS.close);
455 }
456 }
457 }
458
459 if (options?.SLOP) {
460 args.push('SLOP', options.SLOP.toString());
461 }
462
463 if (options?.INORDER) {
464 args.push('INORDER');
465 }
466
467 if (options?.LANGUAGE) {
468 args.push('LANGUAGE', options.LANGUAGE);
469 }
470
471 if (options?.EXPANDER) {
472 args.push('EXPANDER', options.EXPANDER);
473 }
474
475 if (options?.SCORER) {
476 args.push('SCORER', options.SCORER);
477 }
478
479 // if (options?.EXPLAINSCORE) {
480 // args.push('EXPLAINSCORE');
481 // }
482
483 // if (options?.PAYLOAD) {
484 // args.push('PAYLOAD', options.PAYLOAD);
485 // }
486
487 if (options?.SORTBY) {
488 args.push('SORTBY');
489 pushSortByProperty(args, options.SORTBY);
490 }
491
492 // if (options?.MSORTBY) {
493 // pushSortByArguments(args, 'MSORTBY', options.MSORTBY);
494 // }
495
496 if (options?.LIMIT) {
497 args.push(
498 'LIMIT',
499 options.LIMIT.from.toString(),
500 options.LIMIT.size.toString()
501 );
502 }
503
504 if (options?.PARAMS) {
505 pushParamsArgs(args, options.PARAMS);
506 }
507
508 if (options?.DIALECT) {
509 args.push('DIALECT', options.DIALECT.toString());
510 }
511
512 if (options?.RETURN?.length === 0) {
513 args.preserve = true;
514 }
515
516 if (options?.TIMEOUT !== undefined) {
517 args.push('TIMEOUT', options.TIMEOUT.toString());
518 }
519
520 return args;
521 }
522
523 interface SearchDocumentValue {
524 [key: string]: string | number | null | Array<SearchDocumentValue> | SearchDocumentValue;
525 }
526
527 export interface SearchReply {
528 total: number;
529 documents: Array<{
530 id: string;
531 value: SearchDocumentValue;
532 }>;
533 }
534
535 export interface ProfileOptions {
536 LIMITED?: true;
537 }
538
539 export type ProfileRawReply<T> = [
540 results: T,
541 profile: [
542 _: string,
543 TotalProfileTime: string,
544 _: string,
545 ParsingTime: string,
546 _: string,
547 PipelineCreationTime: string,
548 _: string,
549 IteratorsProfile: Array<any>
550 ]
551 ];
552
553 export interface ProfileReply {
554 results: SearchReply | AGGREGATE.AggregateReply;
555 profile: ProfileData;
556 }
557
558 interface ChildIterator {
559 type?: string,
560 counter?: number,
561 term?: string,
562 size?: number,
563 time?: string,
564 childIterators?: Array<ChildIterator>
565 }
566
567 interface IteratorsProfile {
568 type?: string,
569 counter?: number,
570 queryType?: string,
571 time?: string,
572 childIterators?: Array<ChildIterator>
573 }
574
575 interface ProfileData {
576 totalProfileTime: string,
577 parsingTime: string,
578 pipelineCreationTime: string,
579 iteratorsProfile: IteratorsProfile
580 }
581
582 export function transformProfile(reply: Array<any>): ProfileData{
583 return {
584 totalProfileTime: reply[0][1],
585 parsingTime: reply[1][1],
586 pipelineCreationTime: reply[2][1],
587 iteratorsProfile: transformIterators(reply[3][1])
588 };
589 }
590
591 function transformIterators(IteratorsProfile: Array<any>): IteratorsProfile {
592 var res: IteratorsProfile = {};
593 for (let i = 0; i < IteratorsProfile.length; i += 2) {
594 const value = IteratorsProfile[i+1];
595 switch (IteratorsProfile[i]) {
596 case 'Type':
597 res.type = value;
598 break;
599 case 'Counter':
600 res.counter = value;
601 break;
602 case 'Time':
603 res.time = value;
604 break;
605 case 'Query type':
606 res.queryType = value;
607 break;
608 case 'Child iterators':
609 res.childIterators = value.map(transformChildIterators);
610 break;
611 }
612 }
613
614 return res;
615 }
616
617 function transformChildIterators(IteratorsProfile: Array<any>): ChildIterator {
618 var res: ChildIterator = {};
619 for (let i = 1; i < IteratorsProfile.length; i += 2) {
620 const value = IteratorsProfile[i+1];
621 switch (IteratorsProfile[i]) {
622 case 'Type':
623 res.type = value;
624 break;
625 case 'Counter':
626 res.counter = value;
627 break;
628 case 'Time':
629 res.time = value;
630 break;
631 case 'Size':
632 res.size = value;
633 break;
634 case 'Term':
635 res.term = value;
636 break;
637 case 'Child iterators':
638 res.childIterators = value.map(transformChildIterators);
639 break;
640 }
641 }
642
643 return res;
644 }