Last active
June 21, 2022 23:53
-
-
Save gabrielecanepa/dbae7f778c2cdccd41c30cf2cd7708f3 to your computer and use it in GitHub Desktop.
⏱ Simple script to copy Algolia's settings, synonyms and rules from one index to all others in the same app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Script to copy Algolia's settings, synonyms and rules from one index to all others in the same application. | |
*/ | |
import algoliasearch from 'algoliasearch' | |
const client = algoliasearch('APP_ID', 'ADMIN_API_KEY') | |
const sourceIndex = client.initIndex('SOURCE_INDEX_NAME') | |
const copySettings = async (source, target) => { | |
// Exclude the replicas | |
const { replicas, ...settings } = await source.getSettings() | |
await target.setSettings(settings) | |
} | |
const copySynonyms = async (source, target) => { | |
const synonyms = [] | |
await source.browseSynonyms({ | |
batch: batch => { | |
batch.forEach(synonym => { | |
synonyms.push(synonym) | |
}) | |
}, | |
}) | |
await target.saveSynonyms(synonyms) | |
} | |
const copyRules = async (source, target) => { | |
const rules = [] | |
await source.browseRules({ | |
batch: batch => { | |
batch.forEach(synonym => { | |
rules.push(synonym) | |
}) | |
}, | |
}) | |
await target.saveRules(rules) | |
} | |
const run = async () => { | |
try { | |
// Get all indices and filter out the source one | |
const { items } = await client.listIndices() | |
const indices = items.map(item => item.name).filter(index => index !== sourceIndex.indexName) | |
// For each index, copy settings, synonyms and rules | |
for (const index of indices) { | |
const targetIndex = client.initIndex(index) | |
await copySettings(sourceIndex, targetIndex) | |
await copySynonyms(sourceIndex, targetIndex) | |
await copyRules(sourceIndex, targetIndex) | |
} | |
} catch (e) { | |
console.error(e) | |
process.exit(1) | |
} | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment