Skip to content

Commit

Permalink
fix FT.SEARCH RETURN [] (#2421)
Browse files Browse the repository at this point in the history
* ref #2419 - fix FT.SEARCH RETURN []

* fix transformReply

* fix PROFILE SEARCH as well

* fix PROFILE SEARCH preserve

* move preserve login to `pushSearchOptions`

* attach preserve only if true

* fix RETURN: [] test
  • Loading branch information
leibale authored Feb 24, 2023
1 parent 0f28dad commit 26e057e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/search/lib/commands/PROFILE_SEARCH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function transformArguments(
query: string,
options?: ProfileOptions & SearchOptions
): RedisCommandArguments {
const args = ['FT.PROFILE', index, 'SEARCH'];
let args: RedisCommandArguments = ['FT.PROFILE', index, 'SEARCH'];

if (options?.LIMITED) {
args.push('LIMITED');
Expand All @@ -21,9 +21,9 @@ export function transformArguments(

type ProfileSearchRawReply = ProfileRawReply<SearchRawReply>;

export function transformReply(reply: ProfileSearchRawReply): ProfileReply {
export function transformReply(reply: ProfileSearchRawReply, withoutDocuments: boolean): ProfileReply {
return {
results: transformSearchReply(reply[0]),
results: transformSearchReply(reply[0], withoutDocuments),
profile: transformProfile(reply[1])
};
}
8 changes: 6 additions & 2 deletions packages/search/lib/commands/SEARCH.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,22 @@ 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(
await client.ft.search('index', '*', {
RETURN: []
}),
{
total: 1,
total: 2,
documents: [{
id: '1',
value: Object.create(null)
}, {
id: '2',
value: Object.create(null)
}]
}
);
Expand Down
5 changes: 2 additions & 3 deletions packages/search/lib/commands/SEARCH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export function transformArguments(

export type SearchRawReply = Array<any>;

export function transformReply(reply: SearchRawReply): SearchReply {
export function transformReply(reply: SearchRawReply, withoutDocuments: boolean): SearchReply {
const documents = [];
let i = 1;
while (i < reply.length) {
documents.push({
id: reply[i++],
value: documentValue(reply[i++])
value: withoutDocuments ? Object.create(null) : documentValue(reply[i++])
});
}

Expand All @@ -88,7 +88,6 @@ 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) {
Expand Down
4 changes: 4 additions & 0 deletions packages/search/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ export function pushSearchOptions(
args.push('DIALECT', options.DIALECT.toString());
}

if (options?.RETURN?.length === 0) {
args.preserve = true;
}

return args;
}

Expand Down

0 comments on commit 26e057e

Please sign in to comment.