· 7 years ago · Mar 29, 2018, 02:50 AM
1const algolia = require('algoliasearch');
2
3function updateByQuery(index, query, fields) {
4 let objects = index.search({query: query}, (err, content) => {
5 if (err) {
6 console.error(err);
7 } else {
8 let objects = content.hits;
9
10 let updatedObjects = objects.map((obj) => Object.assign({}, obj, fields));
11 index.partialUpdateObjects(updatedObjects, (err, content) => {
12 if (err) {
13 console.error(err);
14 }
15 });
16 }
17 });
18}
19
20const app = algolia('app_id', 'secret_key');
21const index = app.initIndex('your_index');
22
23// Updates all products that match the query 'Game'
24updateByQuery(index, 'Game', {'Genre': 'Action'});