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', () => {
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)
}]
}
);

View File

@@ -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<any>;
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;
}