create_index
Create an Elasticsearch index with configurable settings and mappings to organize and structure searchable data.
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 | |
| settings | No | Index settings, such as number of shards and replicas | |
| mappings | No | Index mappings, defining field types, etc. |
Implementation Reference
- src/tools/createIndex.ts:3-53 (handler)Handler function that executes the logic to create an Elasticsearch index with optional settings and mappings. Returns 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:127-142 (schema)Zod schema defining the input parameters for the create_index tool: index (required string), settings (optional record), mappings (optional record).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.") },
- src/server.ts:123-146 (registration)MCP server.tool registration for 'create_index', providing description, input schema, and 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); } );