create_index
Create an Elasticsearch index with custom settings and mappings to optimize data storage and retrieval for Elasticsearch MCP Server.
Instructions
Create an Elasticsearch index, optionally configure settings and mappings
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | Name of the Elasticsearch index to create | |
| mappings | No | Index mappings, defining field types, etc. | |
| settings | No | Index settings, such as number of shards and replicas |
Implementation Reference
- src/tools/createIndex.ts:3-53 (handler)The core handler function that creates an Elasticsearch index using esClient.indices.create, conditionally includes settings and mappings, and returns formatted success or error content.export async function createIndex( esClient: Client, index: string, settings?: Record<string, any>, mappings?: Record<string, any> ) { try { const body: Record<string, any> = {}; if (settings) { body.settings = settings; } if (mappings) { body.mappings = mappings; } const response = await esClient.indices.create({ index, ...body }); const content: { type: "text"; text: string }[] = []; if (response.acknowledged) { content.push({ type: "text" as const, text: `索引 "${index}" 创建成功!\n分片数: ${response.shards_acknowledged ? '已确认' : '等待确认'}` }); } else { content.push({ type: "text" as const, text: `索引 "${index}" 创建请求已发送,但未得到确认。请检查集群状态。` }); } return { content }; } catch (error) { console.error(`创建索引失败: ${error instanceof Error ? error.message : String(error)}`); return { content: [ { type: "text" as const, text: `错误: ${error instanceof Error ? error.message : String(error)}` } ] }; } }
- src/server.ts:123-146 (registration)Registers the 'create_index' tool on the MCP server, defines input schema using Zod for index (required), settings and mappings (optional), and wires to the createIndex handler function.server.tool( "create_index", "Create an Elasticsearch index, optionally configure settings and mappings", { index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the Elasticsearch index to create"), settings: z .record(z.any()) .optional() .describe("Index settings, such as number of shards and replicas"), mappings: z .record(z.any()) .optional() .describe("Index mappings, defining field types, etc.") }, async ({ index, settings, mappings }) => { return await createIndex(esClient, index, settings, mappings); } );
- src/server.ts:127-146 (schema)Input schema definition for the create_index tool using Zod validators for index, settings, and mappings parameters.index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the Elasticsearch index to create"), settings: z .record(z.any()) .optional() .describe("Index settings, such as number of shards and replicas"), mappings: z .record(z.any()) .optional() .describe("Index mappings, defining field types, etc.") }, async ({ index, settings, mappings }) => { return await createIndex(esClient, index, settings, mappings); } );