Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-4039: add TCEs to the query pages - reissue #2846

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,989 changes: 3,909 additions & 80 deletions doctests/data/query_vector.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion doctests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"private": true,
"type": "module",
"dependencies": {
"redis": "../"
"redis": "../",
"@xenova/transformers": "^2.17.2"
}
}

139 changes: 139 additions & 0 deletions doctests/query-agg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// EXAMPLE: query_agg
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SchemaFieldTypes, AggregateSteps, AggregateGroupByReducers } from '@redis/search';

const client = createClient();

await client.connect().catch(console.error);

// create index
await client.ft.create('idx:bicycle', {
'$.condition': {
type: SchemaFieldTypes.TAG,
AS: 'condition'
},
'$.price': {
type: SchemaFieldTypes.NUMERIC,
AS: 'price'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})

// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));

await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END

// STEP_START agg1
const res1 = await client.ft.aggregate('idx:bicycle', '@condition:{new}', {
LOAD: ['__key', 'price'],
APPLY: {
expression: '@price - (@price * 0.1)',
AS: 'discounted'
}
});

console.log(res1.results.length); // >>> 5
console.log(res1.results); // >>>
//[
// [Object: null prototype] { __key: 'bicycle:0', price: '270' },
// [Object: null prototype] { __key: 'bicycle:5', price: '810' },
// [Object: null prototype] { __key: 'bicycle:6', price: '2300' },
// [Object: null prototype] { __key: 'bicycle:7', price: '430' },
// [Object: null prototype] { __key: 'bicycle:8', price: '1200' }
//]
// REMOVE_START
assert.strictEqual(res1.results.length, 5);
// REMOVE_END
// STEP_END

// STEP_START agg2
const res2 = await client.ft.aggregate('idx:bicycle', '*', {
LOAD: ['@price'],
STEPS: [{
type: AggregateSteps.APPLY,
expression: '@price<1000',
AS: 'price_category'
},{
type: AggregateSteps.GROUPBY,
properties: '@condition',
REDUCE:[{
type: AggregateGroupByReducers.SUM,
property: '@price_category',
AS: 'num_affordable'
}]
}]
});
console.log(res2.results.length); // >>> 3
console.log(res2.results); // >>>
//[[Object: null prototype] { condition: 'refurbished', num_affordable: '1' },
// [Object: null prototype] { condition: 'used', num_affordable: '1' },
// [Object: null prototype] { condition: 'new', num_affordable: '3' }
//]
// REMOVE_START
assert.strictEqual(res2.results.length, 3);
// REMOVE_END
// STEP_END

// STEP_START agg3
const res3 = await client.ft.aggregate('idx:bicycle', '*', {
STEPS: [{
type: AggregateSteps.APPLY,
expression: "'bicycle'",
AS: 'type'
}, {
type: AggregateSteps.GROUPBY,
properties: '@type',
REDUCE: [{
type: AggregateGroupByReducers.COUNT,
property: null,
AS: 'num_total'
}]
}]
});
console.log(res3.results.length); // >>> 1
console.log(res3.results); // >>>
//[ [Object: null prototype] { type: 'bicycle', num_total: '10' } ]
// REMOVE_START
assert.strictEqual(res3.results.length, 1);
// REMOVE_END
// STEP_END

// STEP_START agg4
const res4 = await client.ft.aggregate('idx:bicycle', '*', {
LOAD: ['__key'],
STEPS: [{
type: AggregateSteps.GROUPBY,
properties: '@condition',
REDUCE: [{
type: AggregateGroupByReducers.TOLIST,
property: '__key',
AS: 'bicycles'
}]
}]
});
console.log(res4.results.length); // >>> 3
console.log(res4.results); // >>>
//[[Object: null prototype] {condition: 'refurbished', bicycles: [ 'bicycle:9' ]},
// [Object: null prototype] {condition: 'used', bicycles: [ 'bicycle:1', 'bicycle:2', 'bicycle:3', 'bicycle:4' ]},
// [Object: null prototype] {condition: 'new', bicycles: [ 'bicycle:5', 'bicycle:6', 'bicycle:7', 'bicycle:0', 'bicycle:8' ]}]
// REMOVE_START
assert.strictEqual(res4.results.length, 3);
// REMOVE_END
// STEP_END

// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.disconnect();
// REMOVE_END
192 changes: 192 additions & 0 deletions doctests/query-combined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// EXAMPLE: query_combined
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SchemaFieldTypes, VectorAlgorithms } 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 query = 'Bike for small kids';
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: SchemaFieldTypes.TEXT,
AS: 'description'
},
'$.condition': {
type: SchemaFieldTypes.TAG,
AS: 'condition'
},
'$.price': {
type: SchemaFieldTypes.NUMERIC,
AS: 'price'
},
'$.description_embeddings': {
type: SchemaFieldTypes.VECTOR,
TYPE: 'FLOAT32',
ALGORITHM: VectorAlgorithms.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.disconnect();
// REMOVE_END
Loading
Loading