From 8824115ca88e2ff1fd2d02a4da97cb6ce67aacab Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 8 Nov 2024 11:57:34 +0000 Subject: [PATCH 1/4] DOC-4345 added testable JSON index/query example --- doctests/home-json.js | 147 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 doctests/home-json.js diff --git a/doctests/home-json.js b/doctests/home-json.js new file mode 100644 index 0000000000..910007c2ba --- /dev/null +++ b/doctests/home-json.js @@ -0,0 +1,147 @@ +// EXAMPLE: js_home_json +// REMOVE_START +import assert from 'assert'; +import { Console } from 'console'; +// REMOVE_END +// STEP_START import +import { + createClient, + SchemaFieldTypes, + AggregateGroupByReducers, + AggregateSteps, +} from 'redis'; +// STEP_END + +// STEP_START connect +const client = await createClient(); +await client.connect(); +// STEP_END +// REMOVE_START +await client.ft.dropIndex('idx:users', { DD: true }); +await client.del('user:1', 'user:2', 'user:3'); +// REMOVE_END + +// STEP_START create_data +var user1 = { + name: "Paul John", + email: "paul.john@example.com", + age: 42, + city: "London" +} + +var user2 = { + name: "Eden Zamir", + email: "eden.zamir@example.com", + age: 29, + city: "Tel Aviv" +} + +var user3 = { + name: "Paul Zamir", + email: "paul.zamir@example.com", + age: 35, + city: "Tel Aviv" +} +// STEP_END + +// STEP_START make_index +await client.ft.create('idx:users', { + '$.name': { + type: SchemaFieldTypes.TEXT, + AS: 'name' + }, + "$.city": { + type: SchemaFieldTypes.TEXT, + AS: 'city' + }, + '$.age': { + type: SchemaFieldTypes.NUMERIC, + AS: 'age' + } +}, { + ON: 'JSON', + PREFIX: 'user:' +}); +// STEP_END + +// STEP_START add_data +var user1Added = await client.json.set('user:1', '$', user1); +var user2Added = await client.json.set('user:2', '$', user2); +var user3Added = await client.json.set('user:3', '$', user3); +// STEP_END +// REMOVE_START +assert.equal('OK', user1Added); +assert.equal('OK', user2Added); +assert.equal('OK', user3Added); +// REMOVE_END + +// STEP_START query1 +var findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]'); + +console.log(findPaulResult.total); // >>> 1 + +findPaulResult.documents.forEach(doc => { + console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`); +}); +// >>> ID: user:3, name: Paul Zamir, age: 35 +// STEP_END +// REMOVE_START +assert.strictEqual(1, findPaulResult.total); + +var paulDoc = findPaulResult.documents[0]; + +assert.equal('user:3', paulDoc.id); +// REMOVE_END + +// STEP_START query2 +var citiesResult = await client.ft.search('idx:users', '*',{ + RETURN: "city" +}); + +console.log(citiesResult.total); // >>> 3 + +citiesResult.documents.forEach(cityDoc => { + console.log(cityDoc.value); +}); +// >>> {city: 'London'} +// >>> {city: 'Tel Aviv'} +// >>> {city: 'Tel Aviv'} +// STEP_END +// REMOVE_START +assert.strictEqual(3, citiesResult.total); + +citiesResult.documents.sort((a, b)=> a.id < b.id); +assert.equal('user:1', citiesResult.documents[0].id); +assert.equal('user:2', citiesResult.documents[1].id); +assert.equal('user:3', citiesResult.documents[2].id); +// REMOVE_END + +// STEP_START query3 +var aggResult = await client.ft.aggregate('idx:users', '*', { + STEPS: [{ + type: AggregateSteps.GROUPBY, + properties: '@city', + REDUCE: [{ + type: AggregateGroupByReducers.COUNT, + AS: 'count' + }] + }] +}); + +console.log(aggResult.total); // >>> 2 + +aggResult.results.forEach(result => { + console.log(`${result.city} - ${result.count}`); +}); +// >>> London - 1 +// >>> Tel Aviv - 2 +// STEP_END +// REMOVE_START +assert.strictEqual(2, aggResult.total); + +aggResult.results.sort((a, b) => a.city < b.city); +assert.equal('London - 1', `${aggResult.results[0].city} - ${aggResult.results[0].count}`); +assert.equal('Tel Aviv - 2', `${aggResult.results[1].city} - ${aggResult.results[1].count}`) +// REMOVE_END + +await client.quit(); \ No newline at end of file From 127dc2a026d0db1c7c2c4574815aa072797b939f Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 8 Nov 2024 13:05:19 +0000 Subject: [PATCH 2/4] DOC-4345 removed incorrect Console import --- doctests/home-json.js | 1 - 1 file changed, 1 deletion(-) diff --git a/doctests/home-json.js b/doctests/home-json.js index 910007c2ba..2c0129a1be 100644 --- a/doctests/home-json.js +++ b/doctests/home-json.js @@ -1,7 +1,6 @@ // EXAMPLE: js_home_json // REMOVE_START import assert from 'assert'; -import { Console } from 'console'; // REMOVE_END // STEP_START import import { From 3a56084cab874a315566dfa9f632683b888c6498 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 8 Nov 2024 13:09:13 +0000 Subject: [PATCH 3/4] DOC-4345 added try...catch around index deletion --- doctests/home-json.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doctests/home-json.js b/doctests/home-json.js index 2c0129a1be..9e5f597fa3 100644 --- a/doctests/home-json.js +++ b/doctests/home-json.js @@ -16,7 +16,10 @@ const client = await createClient(); await client.connect(); // STEP_END // REMOVE_START -await client.ft.dropIndex('idx:users', { DD: true }); +try { + await client.ft.dropIndex('idx:users', { DD: true }); +} catch{} + await client.del('user:1', 'user:2', 'user:3'); // REMOVE_END From 8b804e5030ac7f22b9c8db5d4b89b0f759abbadd Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 15 Nov 2024 10:28:39 +0000 Subject: [PATCH 4/4] DOC-4345 implemented feedback --- doctests/home-json.js | 56 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/doctests/home-json.js b/doctests/home-json.js index 9e5f597fa3..3adfc3fae7 100644 --- a/doctests/home-json.js +++ b/doctests/home-json.js @@ -24,26 +24,26 @@ await client.del('user:1', 'user:2', 'user:3'); // REMOVE_END // STEP_START create_data -var user1 = { - name: "Paul John", - email: "paul.john@example.com", +const user1 = { + name: 'Paul John', + email: 'paul.john@example.com', age: 42, - city: "London" -} + city: 'London' +}; -var user2 = { - name: "Eden Zamir", - email: "eden.zamir@example.com", +const user2 = { + name: 'Eden Zamir', + email: 'eden.zamir@example.com', age: 29, - city: "Tel Aviv" -} + city: 'Tel Aviv' +}; -var user3 = { - name: "Paul Zamir", - email: "paul.zamir@example.com", +const user3 = { + name: 'Paul Zamir', + email: 'paul.zamir@example.com', age: 35, - city: "Tel Aviv" -} + city: 'Tel Aviv' +}; // STEP_END // STEP_START make_index @@ -52,7 +52,7 @@ await client.ft.create('idx:users', { type: SchemaFieldTypes.TEXT, AS: 'name' }, - "$.city": { + '$.city': { type: SchemaFieldTypes.TEXT, AS: 'city' }, @@ -67,18 +67,20 @@ await client.ft.create('idx:users', { // STEP_END // STEP_START add_data -var user1Added = await client.json.set('user:1', '$', user1); -var user2Added = await client.json.set('user:2', '$', user2); -var user3Added = await client.json.set('user:3', '$', user3); +const [user1Reply, user2Reply, user3Reply] = await Promise.all([ + client.json.set('user:1', '$', user1), + client.json.set('user:2', '$', user2), + client.json.set('user:3', '$', user3) +]); // STEP_END // REMOVE_START -assert.equal('OK', user1Added); -assert.equal('OK', user2Added); -assert.equal('OK', user3Added); +assert.equal('OK', user1Reply); +assert.equal('OK', user2Reply); +assert.equal('OK', user3Reply); // REMOVE_END // STEP_START query1 -var findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]'); +let findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]'); console.log(findPaulResult.total); // >>> 1 @@ -90,14 +92,14 @@ findPaulResult.documents.forEach(doc => { // REMOVE_START assert.strictEqual(1, findPaulResult.total); -var paulDoc = findPaulResult.documents[0]; +let paulDoc = findPaulResult.documents[0]; assert.equal('user:3', paulDoc.id); // REMOVE_END // STEP_START query2 -var citiesResult = await client.ft.search('idx:users', '*',{ - RETURN: "city" +let citiesResult = await client.ft.search('idx:users', '*',{ + RETURN: 'city' }); console.log(citiesResult.total); // >>> 3 @@ -119,7 +121,7 @@ assert.equal('user:3', citiesResult.documents[2].id); // REMOVE_END // STEP_START query3 -var aggResult = await client.ft.aggregate('idx:users', '*', { +let aggResult = await client.ft.aggregate('idx:users', '*', { STEPS: [{ type: AggregateSteps.GROUPBY, properties: '@city',