|
| 1 | +/** |
| 2 | + * Creates destination indices with -v9 suffix and correct mappings for Reindex V6 -> V9. |
| 3 | + * Run this before running reindex in migration.sh so that string fields (status, userId, etc.) |
| 4 | + * are keyword, not text. |
| 5 | + * |
| 6 | + * How to use: |
| 7 | + * 1. Set ELASTICSEARCH_URL in .env to your V9 cluster (e.g. http://localhost:62223) |
| 8 | + * 2. Run: npx babel-node db/migrations/202602-create-v9-indices.js |
| 9 | + * Or just: ELASTICSEARCH_URL="http://localhost:62223" npx babel-node --extensions .ts,.js db/migrations/202602-create-v9-indices.js |
| 10 | + */ |
| 11 | + |
| 12 | +/* eslint import/namespace: ['error', { allowComputed: true }] */ |
| 13 | +import 'dotenv/config'; |
| 14 | +import '../../util/catchUnhandledRejection'; |
| 15 | +import { Client } from '@elastic/elasticsearch'; |
| 16 | +import getIndexName from '../../util/getIndexName'; |
| 17 | +import indexSetting from '../../util/indexSetting'; |
| 18 | + |
| 19 | +import * as schema from '../../schema'; |
| 20 | + |
| 21 | +const client = new Client({ |
| 22 | + node: process.env.ELASTICSEARCH_URL, |
| 23 | +}); |
| 24 | + |
| 25 | +async function createV9Indices() { |
| 26 | + const schemaNames = Object.keys(schema).filter( |
| 27 | + (k) => typeof schema[k] === 'object' && schema[k].properties |
| 28 | + ); |
| 29 | + for (const index of schemaNames) { |
| 30 | + const baseName = getIndexName(index); |
| 31 | + const indexName = `${baseName}-v9`; |
| 32 | + try { |
| 33 | + const exists = await client.indices.exists({ index: indexName }); |
| 34 | + if (exists) { |
| 35 | + const aliasRes = await client.indices |
| 36 | + .getAlias({ name: index }) |
| 37 | + .catch(() => ({})); |
| 38 | + const aliasMapping = aliasRes || {}; |
| 39 | + const indicesWithAlias = |
| 40 | + typeof aliasMapping === 'object' && aliasMapping !== null |
| 41 | + ? Object.keys(aliasMapping) |
| 42 | + : []; |
| 43 | + const actions = []; |
| 44 | + indicesWithAlias.forEach((idx) => { |
| 45 | + if (idx !== indexName) |
| 46 | + actions.push({ remove: { index: idx, alias: index } }); |
| 47 | + }); |
| 48 | + if (!indicesWithAlias.includes(indexName)) { |
| 49 | + actions.push({ add: { index: indexName, alias: index } }); |
| 50 | + } |
| 51 | + if (actions.length > 0) { |
| 52 | + await client.indices.updateAliases({ actions }); |
| 53 | + console.log(`Index "${indexName}" alias "${index}" updated`); |
| 54 | + } else { |
| 55 | + console.log( |
| 56 | + `Index "${indexName}" already exists with alias "${index}", skip` |
| 57 | + ); |
| 58 | + } |
| 59 | + continue; |
| 60 | + } |
| 61 | + await client.indices.create({ |
| 62 | + index: indexName, |
| 63 | + settings: indexSetting, |
| 64 | + mappings: schema[index], |
| 65 | + aliases: { [index]: {} }, |
| 66 | + }); |
| 67 | + console.log( |
| 68 | + `Index "${indexName}" created with mappings and alias "${index}"` |
| 69 | + ); |
| 70 | + } catch (e) { |
| 71 | + console.error(`Error creating index "${indexName}"`, e); |
| 72 | + throw e; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +createV9Indices().catch((e) => { |
| 78 | + console.error('[createV9Indices]', e); |
| 79 | + process.exit(1); |
| 80 | +}); |
0 commit comments