reindex
Reindex data from a source Elasticsearch index to a target index, optionally applying a query filter or script transformation for document manipulation.
Instructions
Reindex data from a source index to a target index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| destIndex | Yes | Name of the destination Elasticsearch index | |
| query | No | Optional query to filter which documents to reindex | |
| script | No | Optional script to transform documents during reindex | |
| sourceIndex | Yes | Name of the source Elasticsearch index |
Implementation Reference
- src/tools/reindex.ts:3-62 (handler)The main handler function for the reindex tool. It constructs an Elasticsearch ReindexRequest, optionally adds query and script, executes the reindex operation asynchronously (wait_for_completion: false), returns task ID and monitoring instructions on success, or error message on failure.export async function reindex( esClient: Client, sourceIndex: string, destIndex: string, script?: Record<string, any>, query?: Record<string, any> ) { try { const reindexRequest: estypes.ReindexRequest = { source: { index: sourceIndex }, dest: { index: destIndex }, wait_for_completion: false // Async operation for large indices }; // Add query if provided if (query) { reindexRequest.source.query = query; } // Add script if provided if (script) { reindexRequest.script = script; } const response = await esClient.reindex(reindexRequest); const taskId = response.task; return { content: [ { type: "text" as const, text: `Reindex operation started. Task ID: ${taskId}` }, { type: "text" as const, text: `Source index: ${sourceIndex} -> Destination index: ${destIndex}` }, { type: "text" as const, text: `Use Task API to monitor progress: GET _tasks/${taskId}` } ] }; } catch (error) { console.error(`Reindex operation failed: ${error instanceof Error ? error.message : String(error)}`); return { content: [ { type: "text" as const, text: `Error: ${error instanceof Error ? error.message : String(error)}` } ] }; } }
- src/server.ts:195-224 (registration)MCP server registration of the 'reindex' tool, including description, input schema, and thin wrapper handler that invokes the main reindex function with the Elasticsearch client.server.tool( "reindex", "Reindex data from a source index to a target index", { sourceIndex: z .string() .trim() .min(1, "Source index name is required") .describe("Name of the source Elasticsearch index"), destIndex: z .string() .trim() .min(1, "Destination index name is required") .describe("Name of the destination Elasticsearch index"), query: z .record(z.any()) .optional() .describe("Optional query to filter which documents to reindex"), script: z .record(z.any()) .optional() .describe("Optional script to transform documents during reindex") }, async ({ sourceIndex, destIndex, query, script }) => { return await reindex(esClient, sourceIndex, destIndex, script, query); } );
- src/server.ts:198-220 (schema)Zod input schema for the reindex tool, validating sourceIndex and destIndex as required strings, and query/script as optional objects.{ sourceIndex: z .string() .trim() .min(1, "Source index name is required") .describe("Name of the source Elasticsearch index"), destIndex: z .string() .trim() .min(1, "Destination index name is required") .describe("Name of the destination Elasticsearch index"), query: z .record(z.any()) .optional() .describe("Optional query to filter which documents to reindex"), script: z .record(z.any()) .optional() .describe("Optional script to transform documents during reindex") },