diff --git a/dev/tool/src/index.ts b/dev/tool/src/index.ts index f432e9639c..8cd8e5d343 100644 --- a/dev/tool/src/index.ts +++ b/dev/tool/src/index.ts @@ -51,7 +51,7 @@ import { buildStorageFromConfig, storageConfigFromEnv } from '@hcengineering/ser import { program, type Command } from 'commander' import { type Db, type MongoClient } from 'mongodb' import { clearTelegramHistory } from './telegram' -import { diffWorkspace, updateField } from './workspace' +import { diffWorkspace, recreateElastic, updateField } from './workspace' import core, { AccountRole, @@ -989,6 +989,14 @@ export function devTool ( } ) + program + .command('recreate-elastic-indexes ') + .description('reindex workspace to elastic') + .action(async (workspace: string) => { + const { mongodbUri } = prepareTools() + await recreateElastic(mongodbUri, getWorkspaceId(workspace, productId), transactorUrl) + }) + program .command('fix-json-markup ') .description('fixes double converted json markup') diff --git a/dev/tool/src/workspace.ts b/dev/tool/src/workspace.ts index c8dc1b18e8..c67283e906 100644 --- a/dev/tool/src/workspace.ts +++ b/dev/tool/src/workspace.ts @@ -22,7 +22,8 @@ import core, { type Tx, type WorkspaceId, type Ref, - type Doc + type Doc, + DOMAIN_DOC_INDEX_STATE } from '@hcengineering/core' import { getWorkspaceDB } from '@hcengineering/mongo' import { MongoClient } from 'mongodb' @@ -97,3 +98,25 @@ export async function updateField ( await connection.close() } } + +export async function recreateElastic ( + mongoUrl: string, + workspaceId: WorkspaceId, + transactorUrl: string +): Promise { + const client = new MongoClient(mongoUrl) + const connection = (await connect(transactorUrl, workspaceId, undefined, { + mode: 'backup' + })) as unknown as CoreClient & BackupClient + try { + await client.connect() + const db = getWorkspaceDB(client, workspaceId) + await db + .collection(DOMAIN_DOC_INDEX_STATE) + .updateMany({ _class: core.class.DocIndexState }, { $set: { stages: {} } }) + await connection.sendForceClose() + } finally { + await client.close() + await connection.close() + } +}