create_index
Define and configure Elasticsearch index with custom settings and field mappings for efficient data organization and retrieval.
Instructions
Create a new Elasticsearch index with optional settings and mappings
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | Name of the new Elasticsearch index to create | |
| mappings | No | Optional index mappings defining field types and properties | |
| settings | No | Optional index settings like number of shards, replicas, etc. |
Implementation Reference
- src/index.ts:959-987 (handler)The handler function for the 'create_index' tool. It calls esService.createIndex with the provided index name, settings, and mappings, then returns a success message or error.async ({ index, settings, mappings }) => { try { const response = await esService.createIndex(index, settings, mappings); return { content: [ { type: "text", text: `Index '${index}' created successfully.`, }, ], }; } catch (error) { console.error( `Failed to create index: ${ error instanceof Error ? error.message : String(error) }` ); return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/index.ts:940-958 (schema)Input schema for the 'create_index' tool using Zod, defining parameters: index (required string), settings (optional record), mappings (optional record).{ index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the new Elasticsearch index to create"), settings: z .record(z.any()) .optional() .describe( "Optional index settings like number of shards, replicas, etc." ), mappings: z .record(z.any()) .optional() .describe( "Optional index mappings defining field types and properties" ), },
- src/index.ts:938-988 (registration)Registration of the 'create_index' tool using server.tool(), including name, description, input schema, and handler function."create_index", "Create a new Elasticsearch index with optional settings and mappings", { index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the new Elasticsearch index to create"), settings: z .record(z.any()) .optional() .describe( "Optional index settings like number of shards, replicas, etc." ), mappings: z .record(z.any()) .optional() .describe( "Optional index mappings defining field types and properties" ), }, async ({ index, settings, mappings }) => { try { const response = await esService.createIndex(index, settings, mappings); return { content: [ { type: "text", text: `Index '${index}' created successfully.`, }, ], }; } catch (error) { console.error( `Failed to create index: ${ error instanceof Error ? error.message : String(error) }` ); return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } );
- The ElasticsearchService.createIndex method, which performs the actual index creation using the Elasticsearch client, incorporating optional settings and mappings.async createIndex( index: string, settings?: any, mappings?: any ): Promise<any> { const body: any = {}; if (settings) { body.settings = settings; } if (mappings) { body.mappings = mappings; } return await this.client.indices.create({ index, ...(Object.keys(body).length > 0 ? { body } : {}), }); }