Skip to content

Commit a1dfa22

Browse files
authored
fix #2364 - fix FT.SEARCH RETURN [] (#2366)
* fix #2364 - fix FT.SEARCH RETURN [] * remove console.log
1 parent aa75ee4 commit a1dfa22

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

packages/search/lib/commands/SEARCH.spec.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe('SEARCH', () => {
236236
});
237237

238238
describe('client.ft.search', () => {
239-
testUtils.testWithClient('DIALECT 1', async client => {
239+
testUtils.testWithClient('without optional options', async client => {
240240
await Promise.all([
241241
client.ft.create('index', {
242242
field: SchemaFieldTypes.NUMERIC
@@ -245,9 +245,7 @@ describe('SEARCH', () => {
245245
]);
246246

247247
assert.deepEqual(
248-
await client.ft.search('index', '*', {
249-
DIALECT: 1
250-
}),
248+
await client.ft.search('index', '*'),
251249
{
252250
total: 1,
253251
documents: [{
@@ -264,44 +262,23 @@ describe('SEARCH', () => {
264262
);
265263
}, GLOBAL.SERVERS.OPEN);
266264

267-
testUtils.testWithClient('DIALECT 2', async client => {
265+
testUtils.testWithClient('RETURN []', async client => {
268266
await Promise.all([
269267
client.ft.create('index', {
270268
field: SchemaFieldTypes.NUMERIC
271269
}),
272-
client.hSet('1', 'field', '1'),
273-
client.hSet('2', 'field', '2'),
274-
client.hSet('3', 'field', '3')
270+
client.hSet('1', 'field', '1')
275271
]);
276272

277273
assert.deepEqual(
278-
await client.ft.search('index', '@field:[$min $max]', {
279-
PARAMS: {
280-
min: 1,
281-
max: 2
282-
},
283-
DIALECT: 2
274+
await client.ft.search('index', '*', {
275+
RETURN: []
284276
}),
285277
{
286-
total: 2,
278+
total: 1,
287279
documents: [{
288280
id: '1',
289-
value: Object.create(null, {
290-
field: {
291-
value: '1',
292-
configurable: true,
293-
enumerable: true
294-
}
295-
})
296-
}, {
297-
id: '2',
298-
value: Object.create(null, {
299-
field: {
300-
value: '2',
301-
configurable: true,
302-
enumerable: true
303-
}
304-
})
281+
value: Object.create(null)
305282
}]
306283
}
307284
);

packages/search/lib/commands/SEARCH.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { RedisCommandArguments } from '@redis/client/dist/lib/commands';
2-
import { transformTuplesReply } from '@redis/client/dist/lib/commands/generic-transformers';
32
import { pushSearchOptions, RedisSearchLanguages, Params, PropertyName, SortByProperty, SearchReply } from '.';
43

54
export const FIRST_KEY_INDEX = 1;
@@ -73,13 +72,11 @@ export type SearchRawReply = Array<any>;
7372

7473
export function transformReply(reply: SearchRawReply): SearchReply {
7574
const documents = [];
76-
for (let i = 1; i < reply.length; i += 2) {
77-
const tuples = reply[i + 1];
75+
let i = 1;
76+
while (i < reply.length) {
7877
documents.push({
79-
id: reply[i],
80-
value: tuples.length === 2 && tuples[0] === '$' ?
81-
JSON.parse(tuples[1]) :
82-
transformTuplesReply(tuples)
78+
id: reply[i++],
79+
value: documentValue(reply[i++])
8380
});
8481
}
8582

@@ -88,3 +85,26 @@ export function transformReply(reply: SearchRawReply): SearchReply {
8885
documents
8986
};
9087
}
88+
89+
function documentValue(tuples: any) {
90+
const message = Object.create(null);
91+
if (tuples === undefined) return message;
92+
93+
let i = 0;
94+
while (i < tuples.length) {
95+
const key = tuples[i++],
96+
value = tuples[i++];
97+
if (key === '$') { // might be a JSON reply
98+
try {
99+
Object.assign(message, JSON.parse(value));
100+
continue;
101+
} catch {
102+
// set as a regular property if not a valid JSON
103+
}
104+
}
105+
106+
message[key] = value;
107+
}
108+
109+
return message;
110+
}

0 commit comments

Comments
 (0)