Skip to content

Commit 2fb5c5f

Browse files
committed
fix vsim_withscores
1 parent 2854cfd commit 2fb5c5f

File tree

3 files changed

+95
-20
lines changed

3 files changed

+95
-20
lines changed

packages/client/lib/commands/VSIM.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,30 @@ describe('VSIM', () => {
4949
client: GLOBAL.SERVERS.OPEN,
5050
cluster: GLOBAL.CLUSTERS.OPEN
5151
});
52+
53+
testUtils.testWithClient('vSim with RESP3', async client => {
54+
await client.vAdd('resp3-key', [1.0, 2.0, 3.0], 'element1');
55+
await client.vAdd('resp3-key', [1.1, 2.1, 3.1], 'element2');
56+
await client.vAdd('resp3-key', [2.0, 3.0, 4.0], 'element3');
57+
58+
// Test similarity search with vector
59+
const resultWithVector = await client.vSim('resp3-key', [1.05, 2.05, 3.05]);
60+
assert.ok(Array.isArray(resultWithVector));
61+
assert.ok(resultWithVector.length > 0);
62+
63+
// Test similarity search with element
64+
const resultWithElement = await client.vSim('resp3-key', 'element1');
65+
assert.ok(Array.isArray(resultWithElement));
66+
assert.ok(resultWithElement.includes('element1'));
67+
68+
// Test with options
69+
const resultWithOptions = await client.vSim('resp3-key', 'element1', { COUNT: 2 });
70+
assert.ok(Array.isArray(resultWithOptions));
71+
assert.ok(resultWithOptions.length <= 2);
72+
}, {
73+
...GLOBAL.SERVERS.OPEN,
74+
clientOptions: {
75+
RESP: 3
76+
}
77+
});
5278
});

packages/client/lib/commands/VSIM_WITHSCORES.spec.ts

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,55 @@ import { parseArgs } from './generic-transformers';
55

66
describe('VSIM WITHSCORES', () => {
77
it('transformArguments', () => {
8-
assert.deepEqual(
9-
parseArgs(VSIM_WITHSCORES, 'key', 'element'),
10-
['VSIM', 'key', 'ELE', 'element', 'WITHSCORES']
11-
);
8+
assert.deepEqual(parseArgs(VSIM_WITHSCORES, 'key', 'element'), [
9+
'VSIM',
10+
'key',
11+
'ELE',
12+
'element',
13+
'WITHSCORES'
14+
]);
1215
});
1316

14-
testUtils.testAll('vSimWithScores', async client => {
15-
await client.vAdd('key', [1.0, 2.0, 3.0], 'element1');
16-
await client.vAdd('key', [1.1, 2.1, 3.1], 'element2');
17+
testUtils.testAll(
18+
'vSimWithScores',
19+
async client => {
20+
await client.vAdd('key', [1.0, 2.0, 3.0], 'element1');
21+
await client.vAdd('key', [1.1, 2.1, 3.1], 'element2');
1722

18-
const result = await client.vSimWithScores('key', 'element1');
19-
assert.ok(Array.isArray(result));
20-
assert.ok(result.length > 0);
21-
assert.ok(typeof result[0] === 'object');
22-
assert.ok('value' in result[0] && 'score' in result[0]);
23-
}, {
24-
client: GLOBAL.SERVERS.OPEN,
25-
cluster: GLOBAL.CLUSTERS.OPEN
26-
});
23+
const result = await client.vSimWithScores('key', 'element1');
24+
25+
assert.ok(typeof result === 'object');
26+
assert.ok('element1' in result);
27+
assert.ok('element2' in result);
28+
assert.equal(typeof result['element1'], 'number');
29+
assert.equal(typeof result['element2'], 'number');
30+
},
31+
{
32+
client: GLOBAL.SERVERS.OPEN,
33+
cluster: GLOBAL.CLUSTERS.OPEN
34+
}
35+
);
36+
37+
testUtils.testWithClient(
38+
'vSimWithScores with RESP3 - returns Map with scores',
39+
async client => {
40+
await client.vAdd('resp3-key', [1.0, 2.0, 3.0], 'element1');
41+
await client.vAdd('resp3-key', [1.1, 2.1, 3.1], 'element2');
42+
await client.vAdd('resp3-key', [2.0, 3.0, 4.0], 'element3');
43+
44+
const result = await client.vSimWithScores('resp3-key', 'element1');
45+
46+
assert.ok(typeof result === 'object');
47+
assert.ok('element1' in result);
48+
assert.ok('element2' in result);
49+
assert.equal(typeof result['element1'], 'number');
50+
assert.equal(typeof result['element2'], 'number');
51+
},
52+
{
53+
...GLOBAL.SERVERS.OPEN,
54+
clientOptions: {
55+
RESP: 3
56+
}
57+
}
58+
);
2759
});

packages/client/lib/commands/VSIM_WITHSCORES.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { Command } from '../RESP/types';
2-
import { transformSortedSetReply } from './generic-transformers';
1+
import {
2+
ArrayReply,
3+
BlobStringReply,
4+
Command,
5+
DoubleReply,
6+
MapReply,
7+
TuplesReply,
8+
UnwrapReply
9+
} from '../RESP/types';
10+
import { transformDoubleReply } from './generic-transformers';
311
import VSIM from './VSIM';
412

513
export default {
6-
CACHEABLE: VSIM.CACHEABLE,
714
IS_READ_ONLY: VSIM.IS_READ_ONLY,
815
/**
916
* Retrieve elements similar to a given vector or element with similarity scores
@@ -16,5 +23,15 @@ export default {
1623
VSIM.parseCommand(...args);
1724
parser.push('WITHSCORES');
1825
},
19-
transformReply: transformSortedSetReply
26+
transformReply: {
27+
2: (reply: ArrayReply<BlobStringReply>) => {
28+
const inferred = reply as unknown as UnwrapReply<typeof reply>;
29+
const members: Record<string, DoubleReply> = {};
30+
for (let i = 0; i < inferred.length; i += 2) {
31+
members[inferred[i].toString()] = transformDoubleReply[2](inferred[i + 1]);
32+
}
33+
return members;
34+
},
35+
3: undefined as unknown as () => MapReply<BlobStringReply, DoubleReply>
36+
}
2037
} as const satisfies Command;

0 commit comments

Comments
 (0)