reindex
Copies documents from a source Elasticsearch index to a target index, with optional query filtering and script-based transformation.
Instructions
Reindex data from a source index to a target index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceIndex | Yes | Name of the source Elasticsearch index | |
| 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 |
Implementation Reference
- src/tools/reindex.ts:3-62 (handler)The main handler function for the reindex tool. It takes an Elasticsearch client, source index, destination index, optional script, and optional query. It creates a ReindexRequest (with wait_for_completion: false for async operation), optionally adds query/script filters, calls the esClient.reindex() API, and returns a response with the task ID and instructions.
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:198-220 (schema)Input schema definition for the reindex tool, defined via Zod. Requires sourceIndex and destIndex (both trimmed non-empty strings), and optionally accepts query and script (both Record<string, any>).
{ 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") }, - src/server.ts:194-224 (registration)Registration of the 'reindex' tool on the MCP server using server.tool(), binding the schema to the async handler that calls reindex().
// Reindex from source to target index with optional query and script 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:12-12 (registration)Import statement for the reindex function from ./tools/reindex.js
import { reindex } from "./tools/reindex.js";