You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-07-31 05:44:24 +03:00
* move all doctests from emb-examples branch * fix readme * add package-lock.json * --wip-- [skip ci] * fix: replace client.quit() with client.close() as quit is deprecated - doctests/cmds-hash.js - doctests/cmds-list.js - doctests/cmds-servermgmt.js - doctests/cmds-set.js * fix: replace client.quit() with client.close() as quit is deprecated - doctests/cmds-sorted-set.js - doctests/cmds-string.js - doctests/dt-bitfield.js - doctests/dt-bitmap.js * fix: replace client.quit() with client.close() as quit is deprecated - dt-bloom.js: replace client.quit() with client.close() - dt-cms.js: replace client.quit() with client.close() - dt-cuckoo.js: replace client.quit() with client.close() and update expected output comments to reflect v5 boolean returns - dt-geo.js: replace client.quit() with client.close() * fix(doctests): correct pfAdd return values and replace quit with close - Fix dt-hll.js: pfAdd returns 1 instead of true in comments and assertions - Fix dt-hash.js and dt-hll.js: replace deprecated client.quit() with client.close() * fix(doctests): correct API usage and return values in json and list examples - Fix dt-json.js: use options object for json.type, json.strLen, json.del, json.arrPop, json.objLen, json.objKeys - Fix dt-json.js: correct json.del return value from [1] to 1 - Fix dt-list.js: correct client initialization, return values (null, OK, 1), and error type - Replace deprecated client.quit() with client.close() in both files * fix(doctests): update dt-set.js and dt-ss.js for v5 compliance - Updated boolean return values to numbers for SISMEMBER and SMISMEMBER commands - Fixed client lifecycle to use client.close() instead of client.quit() - Removed unnecessary await from createClient() - Added order-independent assertions for set operations - Removed debug statement * fix(doctests): update deprecated methods and imports for v5 compliance - Fix dt-string.js: remove await from client creation and replace client.quit() with client.close() - Fix dt-tdigest.js: replace deprecated client.quit() with client.close() - Fix dt-topk.js: replace client.quit() with client.close() and fix output comment from [1, 0] to [true, false] - Fix query-agg.js: update @redis/search imports to use new constant names and replace client.disconnect() with client.close() * fix(doctests): update imports and replace deprecated disconnect with close - Replace SchemaFieldTypes/VectorAlgorithms with SCHEMA_FIELD_TYPE/SCHEMA_VECTOR_FIELD_ALGORITHM - Replace client.disconnect() with client.close() for consistent deprecation handling - Update query-combined.js, query-em.js, query-ft.js, and query-geo.js * fix(doctests): update imports and replace deprecated methods in remaining files - Update imports to use SCHEMA_FIELD_TYPE and SCHEMA_VECTOR_FIELD_ALGORITHM constants - Replace deprecated disconnect() and quit() methods with close() - Fix assertion in search-quickstart.js to use correct bicycle ID * fix(doctests): update cmds-generic.js and cmds-cnxmgmt.js for v5 compliance - Replace deprecated client.quit() with client.close() - Update sScanIterator to use collection-yielding behavior (value -> values) - Fix HSCAN API changes: tuples renamed to entries - Fix cursor type issues: use string '0' instead of number 0 for hScan - Fix infinite loop in scan cleanup by using do-while pattern * fix(doctests): update dt-streams.js object shapes and parameters for v5 compliance - Update stream result objects from tuple format to proper object format with id/message properties - Change xRead/xReadGroup results from nested arrays to objects with name/messages structure - Update xAutoClaim results to use nextId, messages, and deletedMessages properties - Add missing properties to xInfo* results (max-deleted-entry-id, entries-added, recorded-first-entry-id, entries-read, lag, inactive) - Modernize parameter names (count -> COUNT, block -> BLOCK, etc.) - Update MAXLEN/APPROXIMATE options to new TRIM object structure - Fix error message format for XADD duplicate ID error - Update boolean return values (True -> OK) --------- Co-authored-by: Nikolay Karadzhov <nkaradzhov89@gmail.com>
191 lines
4.8 KiB
JavaScript
191 lines
4.8 KiB
JavaScript
// EXAMPLE: query_combined
|
|
// HIDE_START
|
|
import assert from 'node:assert';
|
|
import fs from 'node:fs';
|
|
import { createClient } from 'redis';
|
|
import { SCHEMA_FIELD_TYPE, SCHEMA_VECTOR_FIELD_ALGORITHM } from '@redis/search';
|
|
import { pipeline } from '@xenova/transformers';
|
|
|
|
function float32Buffer(arr) {
|
|
const floatArray = new Float32Array(arr);
|
|
const float32Buffer = Buffer.from(floatArray.buffer);
|
|
return float32Buffer;
|
|
}
|
|
|
|
async function embedText(sentence) {
|
|
let modelName = 'Xenova/all-MiniLM-L6-v2';
|
|
let pipe = await pipeline('feature-extraction', modelName);
|
|
|
|
let vectorOutput = await pipe(sentence, {
|
|
pooling: 'mean',
|
|
normalize: true,
|
|
});
|
|
|
|
if (vectorOutput == null) {
|
|
throw new Error('vectorOutput is undefined');
|
|
}
|
|
|
|
const embedding = Object.values(vectorOutput.data);
|
|
|
|
return embedding;
|
|
}
|
|
|
|
let vector_query = float32Buffer(await embedText('That is a very happy person'));
|
|
|
|
const client = createClient();
|
|
await client.connect().catch(console.error);
|
|
|
|
// create index
|
|
await client.ft.create('idx:bicycle', {
|
|
'$.description': {
|
|
type: SCHEMA_FIELD_TYPE.TEXT,
|
|
AS: 'description'
|
|
},
|
|
'$.condition': {
|
|
type: SCHEMA_FIELD_TYPE.TAG,
|
|
AS: 'condition'
|
|
},
|
|
'$.price': {
|
|
type: SCHEMA_FIELD_TYPE.NUMERIC,
|
|
AS: 'price'
|
|
},
|
|
'$.description_embeddings': {
|
|
type: SCHEMA_FIELD_TYPE.VECTOR,
|
|
TYPE: 'FLOAT32',
|
|
ALGORITHM: SCHEMA_VECTOR_FIELD_ALGORITHM.FLAT,
|
|
DIM: 384,
|
|
DISTANCE_METRIC: 'COSINE',
|
|
AS: 'vector',
|
|
}
|
|
}, {
|
|
ON: 'JSON',
|
|
PREFIX: 'bicycle:'
|
|
});
|
|
|
|
// load data
|
|
const bicycles = JSON.parse(fs.readFileSync('data/query_vector.json', 'utf8'));
|
|
|
|
await Promise.all(
|
|
bicycles.map((bicycle, bid) => {
|
|
return client.json.set(`bicycle:${bid}`, '$', bicycle);
|
|
})
|
|
);
|
|
// HIDE_END
|
|
|
|
// STEP_START combined1
|
|
const res1 = await client.ft.search('idx:bicycle', '@price:[500 1000] @condition:{new}');
|
|
console.log(res1.total); // >>> 1
|
|
console.log(res1); // >>>
|
|
//{
|
|
// total: 1,
|
|
// documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]
|
|
//}
|
|
// REMOVE_START
|
|
assert.strictEqual(res1.total, 1);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// STEP_START combined2
|
|
const res2 = await client.ft.search('idx:bicycle', 'kids @price:[500 1000] @condition:{used}');
|
|
console.log(res2.total); // >>> 1
|
|
console.log(res2); // >>>
|
|
// {
|
|
// total: 1,
|
|
// documents: [ { id: 'bicycle:2', value: [Object: null prototype] } ]
|
|
// }
|
|
// REMOVE_START
|
|
assert.strictEqual(res2.total, 1);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// STEP_START combined3
|
|
const res3 = await client.ft.search('idx:bicycle', '(kids | small) @condition:{used}');
|
|
console.log(res3.total); // >>> 2
|
|
console.log(res3); // >>>
|
|
//{
|
|
// total: 2,
|
|
// documents: [
|
|
// { id: 'bicycle:2', value: [Object: null prototype] },
|
|
// { id: 'bicycle:1', value: [Object: null prototype] }
|
|
// ]
|
|
//}
|
|
// REMOVE_START
|
|
assert.strictEqual(res3.total, 2);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// STEP_START combined4
|
|
const res4 = await client.ft.search('idx:bicycle', '@description:(kids | small) @condition:{used}');
|
|
console.log(res4.total); // >>> 2
|
|
console.log(res4); // >>>
|
|
//{
|
|
// total: 2,
|
|
// documents: [
|
|
// { id: 'bicycle:2', value: [Object: null prototype] },
|
|
// { id: 'bicycle:1', value: [Object: null prototype] }
|
|
// ]
|
|
//}
|
|
// REMOVE_START
|
|
assert.strictEqual(res4.total, 2);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// STEP_START combined5
|
|
const res5 = await client.ft.search('idx:bicycle', '@description:(kids | small) @condition:{new | used}');
|
|
console.log(res5.total); // >>> 3
|
|
console.log(res5); // >>>
|
|
//{
|
|
// total: 3,
|
|
// documents: [
|
|
// { id: 'bicycle:1', value: [Object: null prototype] },
|
|
// { id: 'bicycle:0', value: [Object: null prototype] },
|
|
// { id: 'bicycle:2', value: [Object: null prototype] }
|
|
// ]
|
|
//}
|
|
// REMOVE_START
|
|
assert.strictEqual(res5.total, 3);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// STEP_START combined6
|
|
const res6 = await client.ft.search('idx:bicycle', '@price:[500 1000] -@condition:{new}');
|
|
console.log(res6.total); // >>> 2
|
|
console.log(res6); // >>>
|
|
//{
|
|
// total: 2,
|
|
// documents: [
|
|
// { id: 'bicycle:2', value: [Object: null prototype] },
|
|
// { id: 'bicycle:9', value: [Object: null prototype] }
|
|
// ]
|
|
//}
|
|
// REMOVE_START
|
|
assert.strictEqual(res6.total, 2);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// STEP_START combined7
|
|
const res7 = await client.ft.search('idx:bicycle',
|
|
'(@price:[500 1000] -@condition:{new})=>[KNN 3 @vector $query_vector]', {
|
|
PARAMS: { query_vector: vector_query },
|
|
DIALECT: 2
|
|
}
|
|
);
|
|
console.log(res7.total); // >>> 2
|
|
console.log(res7); // >>>
|
|
//{
|
|
// total: 2,
|
|
// documents: [
|
|
// { id: 'bicycle:2', value: [Object: null prototype] },
|
|
// { id: 'bicycle:9', value: [Object: null prototype] }
|
|
// ]
|
|
//}
|
|
// REMOVE_START
|
|
assert.strictEqual(res7.total, 2);
|
|
// REMOVE_END
|
|
// STEP_END
|
|
|
|
// REMOVE_START
|
|
// destroy index and data
|
|
await client.ft.dropIndex('idx:bicycle', { DD: true });
|
|
await client.close();
|
|
// REMOVE_END
|