From 38011c82c8813075b0ad98359fead86e8b7cba23 Mon Sep 17 00:00:00 2001 From: Garrett Gu Date: Fri, 13 Dec 2024 14:43:33 -0600 Subject: [PATCH 1/2] Add support for range queries in vectorize --- .../test/vectorize/vectorize-api-test.js | 24 +++++++++++ .../internal/test/vectorize/vectorize-mock.js | 42 ++++++++++++------- src/cloudflare/internal/vectorize.d.ts | 8 +++- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/cloudflare/internal/test/vectorize/vectorize-api-test.js b/src/cloudflare/internal/test/vectorize/vectorize-api-test.js index af0ffcb3c44..b1061cb447e 100644 --- a/src/cloudflare/internal/test/vectorize/vectorize-api-test.js +++ b/src/cloudflare/internal/test/vectorize/vectorize-api-test.js @@ -238,6 +238,30 @@ export const test_vector_search_vector_query = { }; assert.deepStrictEqual(results, expected); } + + { + const results = await IDX.query(new Float32Array(new Array(5).fill(0)), { + topK: 1, + filter: { + text: { + $gt: 'Peter Piper picked a peck of pickled peppers', + $lt: 'You know New York, you need New York, you know you need unique New York', + }, + }, + }); + assert.equal(true, results.count > 0); + /** @type {VectorizeMatches} */ + const expected = { + matches: [ + { + id: 'b0daca4a-ffd8-4865-926b-e24800af2a2d', + score: 0.71151, + }, + ], + count: 1, + }; + assert.deepStrictEqual(results, expected); + } }, }; diff --git a/src/cloudflare/internal/test/vectorize/vectorize-mock.js b/src/cloudflare/internal/test/vectorize/vectorize-mock.js index 7d075efbe9d..41e88e46828 100644 --- a/src/cloudflare/internal/test/vectorize/vectorize-mock.js +++ b/src/cloudflare/internal/test/vectorize/vectorize-mock.js @@ -102,24 +102,34 @@ export default { let returnSet = structuredClone(exampleVectorMatches); if ( body?.filter?.['text'] && - typeof body?.filter?.['text'] === 'object' && - body?.filter?.['text']?.['$eq'] !== undefined + typeof body?.filter?.['text'] === 'object' ) { - const criteria = body?.filter?.['text']?.['$eq']; - returnSet = returnSet.filter( - (m) => m.metadata?.['text'] === criteria - ); - } - if ( - body?.filter?.['text'] && - typeof body?.filter?.['text'] === 'object' && - body?.filter?.['text']?.['$in'] !== undefined - ) { - const criteria = body?.filter?.['text']?.['$in']; - returnSet = returnSet.filter((m) => - criteria.includes(m.metadata?.['text']) - ); + if (body?.filter?.['text']?.['$eq'] !== undefined) { + const criteria = body?.filter?.['text']?.['$eq']; + returnSet = returnSet.filter( + (m) => m.metadata?.['text'] === criteria + ); + } + if (body?.filter?.['text']?.['$in'] !== undefined) { + const criteria = body?.filter?.['text']?.['$in']; + returnSet = returnSet.filter((m) => + criteria.includes(m.metadata?.['text']) + ); + } + if (body?.filter?.['text']?.['$gt'] !== undefined) { + const criteria = body?.filter?.['text']?.['$gt']; + returnSet = returnSet.filter( + (m) => m.metadata?.['text'] > criteria + ); + } + if (body?.filter?.['text']?.['$lt'] !== undefined) { + const criteria = body?.filter?.['text']?.['$lt']; + returnSet = returnSet.filter( + (m) => m.metadata?.['text'] < criteria + ); + } } + if (!body?.returnValues) returnSet.forEach((v) => { delete v.values; diff --git a/src/cloudflare/internal/vectorize.d.ts b/src/cloudflare/internal/vectorize.d.ts index d5828a423f7..ba583c887be 100644 --- a/src/cloudflare/internal/vectorize.d.ts +++ b/src/cloudflare/internal/vectorize.d.ts @@ -33,7 +33,13 @@ interface VectorizeError { * * This list is expected to grow as support for more operations are released. */ -type VectorizeVectorMetadataFilterOp = '$eq' | '$ne'; +type VectorizeVectorMetadataFilterOp = + | '$eq' + | '$ne' + | '$lt' + | '$lte' + | '$gt' + | '$gte'; type VectorizeVectorMetadataFilterCollectionOp = '$in' | '$nin'; /** From 6ce07d8225214d661be215de31cb7f7decccdd8c Mon Sep 17 00:00:00 2001 From: Garrett Gu Date: Mon, 16 Dec 2024 01:41:05 -0600 Subject: [PATCH 2/2] Update src/cloudflare/internal/test/vectorize/vectorize-api-test.js Co-authored-by: Yagiz Nizipli --- src/cloudflare/internal/test/vectorize/vectorize-api-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cloudflare/internal/test/vectorize/vectorize-api-test.js b/src/cloudflare/internal/test/vectorize/vectorize-api-test.js index b1061cb447e..827ae27cb8c 100644 --- a/src/cloudflare/internal/test/vectorize/vectorize-api-test.js +++ b/src/cloudflare/internal/test/vectorize/vectorize-api-test.js @@ -240,7 +240,7 @@ export const test_vector_search_vector_query = { } { - const results = await IDX.query(new Float32Array(new Array(5).fill(0)), { + const results = await IDX.query(new Float32Array(5), { topK: 1, filter: { text: {