diff --git a/packages/search/lib/commands/SEARCH.spec.ts b/packages/search/lib/commands/SEARCH.spec.ts index a5d8ae9e6c4..4f74202cbc7 100644 --- a/packages/search/lib/commands/SEARCH.spec.ts +++ b/packages/search/lib/commands/SEARCH.spec.ts @@ -267,7 +267,8 @@ describe('SEARCH', () => { client.ft.create('index', { field: SchemaFieldTypes.NUMERIC }), - client.hSet('1', 'field', '1') + client.hSet('1', 'field', '1'), + client.hSet('2', 'field', '2') ]); assert.deepEqual( @@ -279,6 +280,9 @@ describe('SEARCH', () => { documents: [{ id: '1', value: Object.create(null) + }, { + id: '2', + value: Object.create(null) }] } ); diff --git a/packages/search/lib/commands/SEARCH.ts b/packages/search/lib/commands/SEARCH.ts index bed06e22c36..d40467f507f 100644 --- a/packages/search/lib/commands/SEARCH.ts +++ b/packages/search/lib/commands/SEARCH.ts @@ -62,21 +62,23 @@ export function transformArguments( query: string, options?: SearchOptions ): RedisCommandArguments { - return pushSearchOptions( + const args = pushSearchOptions( ['FT.SEARCH', index, query], options ); + + args.preserve = options?.RETURN.length === 0; } export type SearchRawReply = Array; -export function transformReply(reply: SearchRawReply): SearchReply { +export function transformReply(reply: SearchRawReply, hasDocuments: boolean): SearchReply { const documents = []; let i = 1; while (i < reply.length) { documents.push({ id: reply[i++], - value: documentValue(reply[i++]) + value: hasDocuments ? documentValue(reply[i++]) : null }); } @@ -88,8 +90,7 @@ export function transformReply(reply: SearchRawReply): SearchReply { 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++],