diff --git a/packages/search/lib/commands/SEARCH.spec.ts b/packages/search/lib/commands/SEARCH.spec.ts index aecf6c8b1a..a5d8ae9e6c 100644 --- a/packages/search/lib/commands/SEARCH.spec.ts +++ b/packages/search/lib/commands/SEARCH.spec.ts @@ -236,7 +236,7 @@ describe('SEARCH', () => { }); describe('client.ft.search', () => { - testUtils.testWithClient('DIALECT 1', async client => { + testUtils.testWithClient('without optional options', async client => { await Promise.all([ client.ft.create('index', { field: SchemaFieldTypes.NUMERIC @@ -245,9 +245,7 @@ describe('SEARCH', () => { ]); assert.deepEqual( - await client.ft.search('index', '*', { - DIALECT: 1 - }), + await client.ft.search('index', '*'), { total: 1, documents: [{ @@ -264,44 +262,23 @@ describe('SEARCH', () => { ); }, GLOBAL.SERVERS.OPEN); - testUtils.testWithClient('DIALECT 2', async client => { + testUtils.testWithClient('RETURN []', async client => { await Promise.all([ client.ft.create('index', { field: SchemaFieldTypes.NUMERIC }), - client.hSet('1', 'field', '1'), - client.hSet('2', 'field', '2'), - client.hSet('3', 'field', '3') + client.hSet('1', 'field', '1') ]); assert.deepEqual( - await client.ft.search('index', '@field:[$min $max]', { - PARAMS: { - min: 1, - max: 2 - }, - DIALECT: 2 + await client.ft.search('index', '*', { + RETURN: [] }), { - total: 2, + total: 1, documents: [{ id: '1', - value: Object.create(null, { - field: { - value: '1', - configurable: true, - enumerable: true - } - }) - }, { - id: '2', - value: Object.create(null, { - field: { - value: '2', - configurable: true, - enumerable: true - } - }) + value: Object.create(null) }] } ); diff --git a/packages/search/lib/commands/SEARCH.ts b/packages/search/lib/commands/SEARCH.ts index 4590997b24..bed06e22c3 100644 --- a/packages/search/lib/commands/SEARCH.ts +++ b/packages/search/lib/commands/SEARCH.ts @@ -1,5 +1,4 @@ import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; -import { transformTuplesReply } from '@redis/client/dist/lib/commands/generic-transformers'; import { pushSearchOptions, RedisSearchLanguages, Params, PropertyName, SortByProperty, SearchReply } from '.'; export const FIRST_KEY_INDEX = 1; @@ -73,13 +72,11 @@ export type SearchRawReply = Array; export function transformReply(reply: SearchRawReply): SearchReply { const documents = []; - for (let i = 1; i < reply.length; i += 2) { - const tuples = reply[i + 1]; + let i = 1; + while (i < reply.length) { documents.push({ - id: reply[i], - value: tuples.length === 2 && tuples[0] === '$' ? - JSON.parse(tuples[1]) : - transformTuplesReply(tuples) + id: reply[i++], + value: documentValue(reply[i++]) }); } @@ -88,3 +85,26 @@ export function transformReply(reply: SearchRawReply): SearchReply { documents }; } + +function documentValue(tuples: any) { + const message = Object.create(null); + if (tuples === undefined) return message; + + let i = 0; + while (i < tuples.length) { + const key = tuples[i++], + value = tuples[i++]; + if (key === '$') { // might be a JSON reply + try { + Object.assign(message, JSON.parse(value)); + continue; + } catch { + // set as a regular property if not a valid JSON + } + } + + message[key] = value; + } + + return message; +}