1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

fix #2364 - fix FT.SEARCH RETURN [] (#2366)

* fix #2364 - fix FT.SEARCH RETURN []

* remove console.log
This commit is contained in:
Leibale Eidelman
2023-01-18 12:54:42 -05:00
committed by GitHub
parent aa75ee49c6
commit a1dfa22517
2 changed files with 35 additions and 38 deletions

View File

@@ -236,7 +236,7 @@ describe('SEARCH', () => {
}); });
describe('client.ft.search', () => { describe('client.ft.search', () => {
testUtils.testWithClient('DIALECT 1', async client => { testUtils.testWithClient('without optional options', async client => {
await Promise.all([ await Promise.all([
client.ft.create('index', { client.ft.create('index', {
field: SchemaFieldTypes.NUMERIC field: SchemaFieldTypes.NUMERIC
@@ -245,9 +245,7 @@ describe('SEARCH', () => {
]); ]);
assert.deepEqual( assert.deepEqual(
await client.ft.search('index', '*', { await client.ft.search('index', '*'),
DIALECT: 1
}),
{ {
total: 1, total: 1,
documents: [{ documents: [{
@@ -264,44 +262,23 @@ describe('SEARCH', () => {
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('DIALECT 2', async client => { testUtils.testWithClient('RETURN []', async client => {
await Promise.all([ await Promise.all([
client.ft.create('index', { client.ft.create('index', {
field: SchemaFieldTypes.NUMERIC field: SchemaFieldTypes.NUMERIC
}), }),
client.hSet('1', 'field', '1'), client.hSet('1', 'field', '1')
client.hSet('2', 'field', '2'),
client.hSet('3', 'field', '3')
]); ]);
assert.deepEqual( assert.deepEqual(
await client.ft.search('index', '@field:[$min $max]', { await client.ft.search('index', '*', {
PARAMS: { RETURN: []
min: 1,
max: 2
},
DIALECT: 2
}), }),
{ {
total: 2, total: 1,
documents: [{ documents: [{
id: '1', id: '1',
value: Object.create(null, { value: Object.create(null)
field: {
value: '1',
configurable: true,
enumerable: true
}
})
}, {
id: '2',
value: Object.create(null, {
field: {
value: '2',
configurable: true,
enumerable: true
}
})
}] }]
} }
); );

View File

@@ -1,5 +1,4 @@
import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; 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 '.'; import { pushSearchOptions, RedisSearchLanguages, Params, PropertyName, SortByProperty, SearchReply } from '.';
export const FIRST_KEY_INDEX = 1; export const FIRST_KEY_INDEX = 1;
@@ -73,13 +72,11 @@ export type SearchRawReply = Array<any>;
export function transformReply(reply: SearchRawReply): SearchReply { export function transformReply(reply: SearchRawReply): SearchReply {
const documents = []; const documents = [];
for (let i = 1; i < reply.length; i += 2) { let i = 1;
const tuples = reply[i + 1]; while (i < reply.length) {
documents.push({ documents.push({
id: reply[i], id: reply[i++],
value: tuples.length === 2 && tuples[0] === '$' ? value: documentValue(reply[i++])
JSON.parse(tuples[1]) :
transformTuplesReply(tuples)
}); });
} }
@@ -88,3 +85,26 @@ export function transformReply(reply: SearchRawReply): SearchReply {
documents 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;
}