Skip to content

Commit b9a5d36

Browse files
authored
fix(search): properly decide if response has docs (#3060)
fixes: #3056
1 parent e347d56 commit b9a5d36

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,84 @@ describe('FT.SEARCH', () => {
326326
}
327327
);
328328
}, GLOBAL.SERVERS.OPEN);
329+
330+
testUtils.testWithClient('properly parse content/nocontent scenarios', async client => {
331+
332+
const indexName = 'foo';
333+
await client.ft.create(
334+
indexName,
335+
{
336+
itemOrder: {
337+
type: 'NUMERIC',
338+
SORTABLE: true,
339+
},
340+
name: {
341+
type: 'TEXT',
342+
},
343+
},
344+
{
345+
ON: 'HASH',
346+
PREFIX: 'item:',
347+
}
348+
);
349+
350+
await client.hSet("item:1", {
351+
itemOrder: 1,
352+
name: "First item",
353+
});
354+
355+
await client.hSet("item:2", {
356+
itemOrder: 2,
357+
name: "Second item",
358+
});
359+
360+
await client.hSet("item:3", {
361+
itemOrder: 3,
362+
name: "Third item",
363+
});
364+
365+
// Search with SORTBY and LIMIT
366+
let result = await client.ft.search(indexName, "@itemOrder:[0 10]", {
367+
SORTBY: {
368+
BY: "itemOrder",
369+
DIRECTION: "ASC",
370+
},
371+
LIMIT: {
372+
from: 0,
373+
size: 1, // only get first result
374+
},
375+
});
376+
377+
assert.equal(result.total, 3, "Result's `total` value reflects the total scanned documents");
378+
assert.equal(result.documents.length, 1);
379+
let doc = result.documents[0];
380+
assert.equal(doc.id, 'item:1');
381+
assert.equal(doc.value.itemOrder, '1');
382+
assert.equal(doc.value.name, 'First item');
383+
384+
await client.del("item:3");
385+
386+
// Search again after removing item:3
387+
result = await client.ft.search(indexName, "@itemOrder:[0 10]", {
388+
SORTBY: {
389+
BY: "itemOrder",
390+
DIRECTION: "ASC",
391+
},
392+
LIMIT: {
393+
from: 0,
394+
size: 1, // only get first result
395+
},
396+
});
397+
398+
assert.equal(result.total, 2, "Result's `total` value reflects the total scanned documents");
399+
assert.equal(result.documents.length, 1);
400+
doc = result.documents[0];
401+
assert.equal(doc.id, 'item:1');
402+
assert.equal(doc.value.itemOrder, '1');
403+
assert.equal(doc.value.name, 'First item');
404+
405+
406+
}, GLOBAL.SERVERS.OPEN);
407+
329408
});
330409
});

packages/search/lib/commands/SEARCH.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ export default {
183183
},
184184
transformReply: {
185185
2: (reply: SearchRawReply): SearchReply => {
186-
const withoutDocuments = (reply[0] + 1 == reply.length)
186+
// if reply[2] is array, then we have content/documents. Otherwise, only ids
187+
const withoutDocuments = reply.length > 2 && !Array.isArray(reply[2]);
187188

188189
const documents = [];
189190
let i = 1;

0 commit comments

Comments
 (0)