create_index_template
Define index templates in Elasticsearch to automatically apply settings, mappings, and aliases to matching indices, ensuring consistent data structure and configuration.
Instructions
Create or update an Elasticsearch index template
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Name of the index template | |
| indexPatterns | Yes | Array of index patterns this template applies to | |
| template | Yes | Template configuration including settings, mappings, and aliases | |
| priority | No | Optional template priority - higher values have higher precedence | |
| version | No | Optional template version number |
Implementation Reference
- src/tools/createIndexTemplate.ts:3-64 (handler)The async handler function implementing the core logic for creating an Elasticsearch index template, including building the template body, calling the Elasticsearch API, and handling responses/errors.export async function createIndexTemplate( esClient: Client, name: string, indexPatterns: string[], template: Record<string, any>, priority?: number, version?: number ) { try { const body: Record<string, any> = { index_patterns: indexPatterns, template: { settings: template.settings || {}, mappings: template.mappings || {}, aliases: template.aliases || {} } }; // Add optional parameters if provided if (priority !== undefined) { body.priority = priority; } if (version !== undefined) { body.version = version; } const response = await esClient.indices.putIndexTemplate({ name, body }); return { content: [ { type: "text" as const, text: `Index template "${name}" created successfully.` }, { type: "text" as const, text: `Index patterns: ${indexPatterns.join(", ")}` }, { type: "text" as const, text: response.acknowledged ? "Template was acknowledged by the cluster." : "Template was not acknowledged. Check cluster status." } ] }; } catch (error) { console.error(`Create index template 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:231-255 (schema)Zod input schema validating parameters for the create_index_template tool: name (string), indexPatterns (string[]), template (record), optional priority and version (numbers).name: z .string() .trim() .min(1, "Template name is required") .describe("Name of the index template"), indexPatterns: z .array(z.string()) .min(1, "At least one index pattern is required") .describe("Array of index patterns this template applies to"), template: z .record(z.any()) .describe("Template configuration including settings, mappings, and aliases"), priority: z .number() .optional() .describe("Optional template priority - higher values have higher precedence"), version: z .number() .optional() .describe("Optional template version number") },
- src/server.ts:227-259 (registration)MCP server tool registration for 'create_index_template', including name, description, input schema, and handler invocation.server.tool( "create_index_template", "Create or update an Elasticsearch index template", { name: z .string() .trim() .min(1, "Template name is required") .describe("Name of the index template"), indexPatterns: z .array(z.string()) .min(1, "At least one index pattern is required") .describe("Array of index patterns this template applies to"), template: z .record(z.any()) .describe("Template configuration including settings, mappings, and aliases"), priority: z .number() .optional() .describe("Optional template priority - higher values have higher precedence"), version: z .number() .optional() .describe("Optional template version number") }, async ({ name, indexPatterns, template, priority, version }) => { return await createIndexTemplate(esClient, name, indexPatterns, template, priority, version); } );