Commit ae8e1498 authored by Cole Bemis's avatar Cole Bemis

build: Refactor algolia script to reindex data atomically

parent 962870d5
...@@ -18,20 +18,32 @@ function syncAlgolia() { ...@@ -18,20 +18,32 @@ function syncAlgolia() {
// ALGOLIA_ADMIN_KEY must be added as an environment variable in Travis CI // ALGOLIA_ADMIN_KEY must be added as an environment variable in Travis CI
const client = algolia(ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_KEY); const client = algolia(ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_KEY);
// Initialize the target and temporary indexes
const index = client.initIndex('icons'); const index = client.initIndex('icons');
const indexTmp = client.initIndex('icons_tmp');
// Copy the settings, synonyms and rules (but not the records)
// of the target index into the temporary index
client.copyIndex(index.indexName, indexTmp.indexName, [
'settings',
'synonyms',
'rules',
]);
// Push data to the temporary index
const records = Object.keys(icons).map(name => ({ const records = Object.keys(icons).map(name => ({
name, name,
tags: tags[name] || [], tags: tags[name] || [],
})); }));
index.clearIndex((err, content) => { indexTmp.addObjects(records, (err, content) => {
if (err) throw err; if (err) throw err;
console.log(content); console.log(content);
});
index.addObjects(records, (err, content) => { // Move the temporary index to the target index
if (err) throw err; client.moveIndex(indexTmp.indexName, index.indexName, (err, content) => {
console.log(content); if (err) throw err;
}); console.log(content);
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment