index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformProfile = exports.pushSearchOptions = exports.pushParamsArgs = exports.pushSchema = exports.VectorAlgorithms = exports.SchemaTextFieldPhonetics = exports.SchemaFieldTypes = exports.pushArgumentsWithLength = exports.pushSortByArguments = exports.pushSortByProperty = exports.RedisSearchLanguages = void 0;
  4. const _LIST = require("./_LIST");
  5. const ALTER = require("./ALTER");
  6. const AGGREGATE_WITHCURSOR = require("./AGGREGATE_WITHCURSOR");
  7. const AGGREGATE = require("./AGGREGATE");
  8. const ALIASADD = require("./ALIASADD");
  9. const ALIASDEL = require("./ALIASDEL");
  10. const ALIASUPDATE = require("./ALIASUPDATE");
  11. const CONFIG_GET = require("./CONFIG_GET");
  12. const CONFIG_SET = require("./CONFIG_SET");
  13. const CREATE = require("./CREATE");
  14. const CURSOR_DEL = require("./CURSOR_DEL");
  15. const CURSOR_READ = require("./CURSOR_READ");
  16. const DICTADD = require("./DICTADD");
  17. const DICTDEL = require("./DICTDEL");
  18. const DICTDUMP = require("./DICTDUMP");
  19. const DROPINDEX = require("./DROPINDEX");
  20. const EXPLAIN = require("./EXPLAIN");
  21. const EXPLAINCLI = require("./EXPLAINCLI");
  22. const INFO = require("./INFO");
  23. const PROFILESEARCH = require("./PROFILE_SEARCH");
  24. const PROFILEAGGREGATE = require("./PROFILE_AGGREGATE");
  25. const SEARCH = require("./SEARCH");
  26. const SEARCH_NOCONTENT = require("./SEARCH_NOCONTENT");
  27. const SPELLCHECK = require("./SPELLCHECK");
  28. const SUGADD = require("./SUGADD");
  29. const SUGDEL = require("./SUGDEL");
  30. const SUGGET_WITHPAYLOADS = require("./SUGGET_WITHPAYLOADS");
  31. const SUGGET_WITHSCORES_WITHPAYLOADS = require("./SUGGET_WITHSCORES_WITHPAYLOADS");
  32. const SUGGET_WITHSCORES = require("./SUGGET_WITHSCORES");
  33. const SUGGET = require("./SUGGET");
  34. const SUGLEN = require("./SUGLEN");
  35. const SYNDUMP = require("./SYNDUMP");
  36. const SYNUPDATE = require("./SYNUPDATE");
  37. const TAGVALS = require("./TAGVALS");
  38. const generic_transformers_1 = require("@redis/client/dist/lib/commands/generic-transformers");
  39. exports.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. var RedisSearchLanguages;
  110. (function (RedisSearchLanguages) {
  111. RedisSearchLanguages["ARABIC"] = "Arabic";
  112. RedisSearchLanguages["BASQUE"] = "Basque";
  113. RedisSearchLanguages["CATALANA"] = "Catalan";
  114. RedisSearchLanguages["DANISH"] = "Danish";
  115. RedisSearchLanguages["DUTCH"] = "Dutch";
  116. RedisSearchLanguages["ENGLISH"] = "English";
  117. RedisSearchLanguages["FINNISH"] = "Finnish";
  118. RedisSearchLanguages["FRENCH"] = "French";
  119. RedisSearchLanguages["GERMAN"] = "German";
  120. RedisSearchLanguages["GREEK"] = "Greek";
  121. RedisSearchLanguages["HUNGARIAN"] = "Hungarian";
  122. RedisSearchLanguages["INDONESAIN"] = "Indonesian";
  123. RedisSearchLanguages["IRISH"] = "Irish";
  124. RedisSearchLanguages["ITALIAN"] = "Italian";
  125. RedisSearchLanguages["LITHUANIAN"] = "Lithuanian";
  126. RedisSearchLanguages["NEPALI"] = "Nepali";
  127. RedisSearchLanguages["NORWEIGAN"] = "Norwegian";
  128. RedisSearchLanguages["PORTUGUESE"] = "Portuguese";
  129. RedisSearchLanguages["ROMANIAN"] = "Romanian";
  130. RedisSearchLanguages["RUSSIAN"] = "Russian";
  131. RedisSearchLanguages["SPANISH"] = "Spanish";
  132. RedisSearchLanguages["SWEDISH"] = "Swedish";
  133. RedisSearchLanguages["TAMIL"] = "Tamil";
  134. RedisSearchLanguages["TURKISH"] = "Turkish";
  135. RedisSearchLanguages["CHINESE"] = "Chinese";
  136. })(RedisSearchLanguages || (exports.RedisSearchLanguages = RedisSearchLanguages = {}));
  137. function pushSortByProperty(args, sortBy) {
  138. if (typeof sortBy === 'string') {
  139. args.push(sortBy);
  140. }
  141. else {
  142. args.push(sortBy.BY);
  143. if (sortBy.DIRECTION) {
  144. args.push(sortBy.DIRECTION);
  145. }
  146. }
  147. }
  148. exports.pushSortByProperty = pushSortByProperty;
  149. function pushSortByArguments(args, name, sortBy) {
  150. const lengthBefore = args.push(name, '' // will be overwritten
  151. );
  152. if (Array.isArray(sortBy)) {
  153. for (const field of sortBy) {
  154. pushSortByProperty(args, field);
  155. }
  156. }
  157. else {
  158. pushSortByProperty(args, sortBy);
  159. }
  160. args[lengthBefore - 1] = (args.length - lengthBefore).toString();
  161. return args;
  162. }
  163. exports.pushSortByArguments = pushSortByArguments;
  164. function pushArgumentsWithLength(args, fn) {
  165. const lengthIndex = args.push('') - 1;
  166. fn(args);
  167. args[lengthIndex] = (args.length - lengthIndex - 1).toString();
  168. return args;
  169. }
  170. exports.pushArgumentsWithLength = pushArgumentsWithLength;
  171. var SchemaFieldTypes;
  172. (function (SchemaFieldTypes) {
  173. SchemaFieldTypes["TEXT"] = "TEXT";
  174. SchemaFieldTypes["NUMERIC"] = "NUMERIC";
  175. SchemaFieldTypes["GEO"] = "GEO";
  176. SchemaFieldTypes["TAG"] = "TAG";
  177. SchemaFieldTypes["VECTOR"] = "VECTOR";
  178. })(SchemaFieldTypes || (exports.SchemaFieldTypes = SchemaFieldTypes = {}));
  179. var SchemaTextFieldPhonetics;
  180. (function (SchemaTextFieldPhonetics) {
  181. SchemaTextFieldPhonetics["DM_EN"] = "dm:en";
  182. SchemaTextFieldPhonetics["DM_FR"] = "dm:fr";
  183. SchemaTextFieldPhonetics["FM_PT"] = "dm:pt";
  184. SchemaTextFieldPhonetics["DM_ES"] = "dm:es";
  185. })(SchemaTextFieldPhonetics || (exports.SchemaTextFieldPhonetics = SchemaTextFieldPhonetics = {}));
  186. var VectorAlgorithms;
  187. (function (VectorAlgorithms) {
  188. VectorAlgorithms["FLAT"] = "FLAT";
  189. VectorAlgorithms["HNSW"] = "HNSW";
  190. })(VectorAlgorithms || (exports.VectorAlgorithms = VectorAlgorithms = {}));
  191. function pushSchema(args, schema) {
  192. for (const [field, fieldOptions] of Object.entries(schema)) {
  193. args.push(field);
  194. if (typeof fieldOptions === 'string') {
  195. args.push(fieldOptions);
  196. continue;
  197. }
  198. if (fieldOptions.AS) {
  199. args.push('AS', fieldOptions.AS);
  200. }
  201. args.push(fieldOptions.type);
  202. switch (fieldOptions.type) {
  203. case SchemaFieldTypes.TEXT:
  204. if (fieldOptions.NOSTEM) {
  205. args.push('NOSTEM');
  206. }
  207. if (fieldOptions.WEIGHT) {
  208. args.push('WEIGHT', fieldOptions.WEIGHT.toString());
  209. }
  210. if (fieldOptions.PHONETIC) {
  211. args.push('PHONETIC', fieldOptions.PHONETIC);
  212. }
  213. if (fieldOptions.WITHSUFFIXTRIE) {
  214. args.push('WITHSUFFIXTRIE');
  215. }
  216. break;
  217. // case SchemaFieldTypes.NUMERIC:
  218. // case SchemaFieldTypes.GEO:
  219. // break;
  220. case SchemaFieldTypes.TAG:
  221. if (fieldOptions.SEPARATOR) {
  222. args.push('SEPARATOR', fieldOptions.SEPARATOR);
  223. }
  224. if (fieldOptions.CASESENSITIVE) {
  225. args.push('CASESENSITIVE');
  226. }
  227. if (fieldOptions.WITHSUFFIXTRIE) {
  228. args.push('WITHSUFFIXTRIE');
  229. }
  230. break;
  231. case SchemaFieldTypes.VECTOR:
  232. args.push(fieldOptions.ALGORITHM);
  233. pushArgumentsWithLength(args, () => {
  234. args.push('TYPE', fieldOptions.TYPE, 'DIM', fieldOptions.DIM.toString(), 'DISTANCE_METRIC', fieldOptions.DISTANCE_METRIC);
  235. if (fieldOptions.INITIAL_CAP) {
  236. args.push('INITIAL_CAP', fieldOptions.INITIAL_CAP.toString());
  237. }
  238. switch (fieldOptions.ALGORITHM) {
  239. case VectorAlgorithms.FLAT:
  240. if (fieldOptions.BLOCK_SIZE) {
  241. args.push('BLOCK_SIZE', fieldOptions.BLOCK_SIZE.toString());
  242. }
  243. break;
  244. case VectorAlgorithms.HNSW:
  245. if (fieldOptions.M) {
  246. args.push('M', fieldOptions.M.toString());
  247. }
  248. if (fieldOptions.EF_CONSTRUCTION) {
  249. args.push('EF_CONSTRUCTION', fieldOptions.EF_CONSTRUCTION.toString());
  250. }
  251. if (fieldOptions.EF_RUNTIME) {
  252. args.push('EF_RUNTIME', fieldOptions.EF_RUNTIME.toString());
  253. }
  254. break;
  255. }
  256. });
  257. continue; // vector fields do not contain SORTABLE and NOINDEX options
  258. }
  259. if (fieldOptions.SORTABLE) {
  260. args.push('SORTABLE');
  261. if (fieldOptions.SORTABLE === 'UNF') {
  262. args.push('UNF');
  263. }
  264. }
  265. if (fieldOptions.NOINDEX) {
  266. args.push('NOINDEX');
  267. }
  268. }
  269. }
  270. exports.pushSchema = pushSchema;
  271. function pushParamsArgs(args, params) {
  272. if (params) {
  273. const enrties = Object.entries(params);
  274. args.push('PARAMS', (enrties.length * 2).toString());
  275. for (const [key, value] of enrties) {
  276. args.push(key, typeof value === 'number' ? value.toString() : value);
  277. }
  278. }
  279. return args;
  280. }
  281. exports.pushParamsArgs = pushParamsArgs;
  282. function pushSearchOptions(args, options) {
  283. if (options?.VERBATIM) {
  284. args.push('VERBATIM');
  285. }
  286. if (options?.NOSTOPWORDS) {
  287. args.push('NOSTOPWORDS');
  288. }
  289. // if (options?.WITHSCORES) {
  290. // args.push('WITHSCORES');
  291. // }
  292. // if (options?.WITHPAYLOADS) {
  293. // args.push('WITHPAYLOADS');
  294. // }
  295. (0, generic_transformers_1.pushOptionalVerdictArgument)(args, 'INKEYS', options?.INKEYS);
  296. (0, generic_transformers_1.pushOptionalVerdictArgument)(args, 'INFIELDS', options?.INFIELDS);
  297. (0, generic_transformers_1.pushOptionalVerdictArgument)(args, 'RETURN', options?.RETURN);
  298. if (options?.SUMMARIZE) {
  299. args.push('SUMMARIZE');
  300. if (typeof options.SUMMARIZE === 'object') {
  301. if (options.SUMMARIZE.FIELDS) {
  302. args.push('FIELDS');
  303. (0, generic_transformers_1.pushVerdictArgument)(args, options.SUMMARIZE.FIELDS);
  304. }
  305. if (options.SUMMARIZE.FRAGS) {
  306. args.push('FRAGS', options.SUMMARIZE.FRAGS.toString());
  307. }
  308. if (options.SUMMARIZE.LEN) {
  309. args.push('LEN', options.SUMMARIZE.LEN.toString());
  310. }
  311. if (options.SUMMARIZE.SEPARATOR) {
  312. args.push('SEPARATOR', options.SUMMARIZE.SEPARATOR);
  313. }
  314. }
  315. }
  316. if (options?.HIGHLIGHT) {
  317. args.push('HIGHLIGHT');
  318. if (typeof options.HIGHLIGHT === 'object') {
  319. if (options.HIGHLIGHT.FIELDS) {
  320. args.push('FIELDS');
  321. (0, generic_transformers_1.pushVerdictArgument)(args, options.HIGHLIGHT.FIELDS);
  322. }
  323. if (options.HIGHLIGHT.TAGS) {
  324. args.push('TAGS', options.HIGHLIGHT.TAGS.open, options.HIGHLIGHT.TAGS.close);
  325. }
  326. }
  327. }
  328. if (options?.SLOP) {
  329. args.push('SLOP', options.SLOP.toString());
  330. }
  331. if (options?.INORDER) {
  332. args.push('INORDER');
  333. }
  334. if (options?.LANGUAGE) {
  335. args.push('LANGUAGE', options.LANGUAGE);
  336. }
  337. if (options?.EXPANDER) {
  338. args.push('EXPANDER', options.EXPANDER);
  339. }
  340. if (options?.SCORER) {
  341. args.push('SCORER', options.SCORER);
  342. }
  343. // if (options?.EXPLAINSCORE) {
  344. // args.push('EXPLAINSCORE');
  345. // }
  346. // if (options?.PAYLOAD) {
  347. // args.push('PAYLOAD', options.PAYLOAD);
  348. // }
  349. if (options?.SORTBY) {
  350. args.push('SORTBY');
  351. pushSortByProperty(args, options.SORTBY);
  352. }
  353. // if (options?.MSORTBY) {
  354. // pushSortByArguments(args, 'MSORTBY', options.MSORTBY);
  355. // }
  356. if (options?.LIMIT) {
  357. args.push('LIMIT', options.LIMIT.from.toString(), options.LIMIT.size.toString());
  358. }
  359. if (options?.PARAMS) {
  360. pushParamsArgs(args, options.PARAMS);
  361. }
  362. if (options?.DIALECT) {
  363. args.push('DIALECT', options.DIALECT.toString());
  364. }
  365. if (options?.RETURN?.length === 0) {
  366. args.preserve = true;
  367. }
  368. if (options?.TIMEOUT !== undefined) {
  369. args.push('TIMEOUT', options.TIMEOUT.toString());
  370. }
  371. return args;
  372. }
  373. exports.pushSearchOptions = pushSearchOptions;
  374. function transformProfile(reply) {
  375. return {
  376. totalProfileTime: reply[0][1],
  377. parsingTime: reply[1][1],
  378. pipelineCreationTime: reply[2][1],
  379. iteratorsProfile: transformIterators(reply[3][1])
  380. };
  381. }
  382. exports.transformProfile = transformProfile;
  383. function transformIterators(IteratorsProfile) {
  384. var res = {};
  385. for (let i = 0; i < IteratorsProfile.length; i += 2) {
  386. const value = IteratorsProfile[i + 1];
  387. switch (IteratorsProfile[i]) {
  388. case 'Type':
  389. res.type = value;
  390. break;
  391. case 'Counter':
  392. res.counter = value;
  393. break;
  394. case 'Time':
  395. res.time = value;
  396. break;
  397. case 'Query type':
  398. res.queryType = value;
  399. break;
  400. case 'Child iterators':
  401. res.childIterators = value.map(transformChildIterators);
  402. break;
  403. }
  404. }
  405. return res;
  406. }
  407. function transformChildIterators(IteratorsProfile) {
  408. var res = {};
  409. for (let i = 1; i < IteratorsProfile.length; i += 2) {
  410. const value = IteratorsProfile[i + 1];
  411. switch (IteratorsProfile[i]) {
  412. case 'Type':
  413. res.type = value;
  414. break;
  415. case 'Counter':
  416. res.counter = value;
  417. break;
  418. case 'Time':
  419. res.time = value;
  420. break;
  421. case 'Size':
  422. res.size = value;
  423. break;
  424. case 'Term':
  425. res.term = value;
  426. break;
  427. case 'Child iterators':
  428. res.childIterators = value.map(transformChildIterators);
  429. break;
  430. }
  431. }
  432. return res;
  433. }