create_mapping
Define or modify the field structure of an Elasticsearch index to specify data types and properties for efficient search and analysis.
Instructions
Create or update the mapping structure of an Elasticsearch index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | Elasticsearch index name | |
| mappings | Yes | Index mappings, defining field types, etc. |
Implementation Reference
- src/tools/createMapping.ts:3-61 (handler)The handler function that implements the core logic for creating or updating Elasticsearch index mappings, checking if index exists, applying mappings, and returning status and current mappings.export async function createMapping( esClient: Client, index: string, mappings: Record<string, any> ) { try { // check if index exists const indexExists = await esClient.indices.exists({ index }); let response; const content: { type: "text"; text: string }[] = []; if (!indexExists) { // if index does not exist, create it and apply mapping response = await esClient.indices.create({ index, mappings }); content.push({ type: "text" as const, text: `Index "${index}" does not exist. Created new index and applied mapping.` }); } else { // if index exists, update mapping response = await esClient.indices.putMapping({ index, ...mappings }); content.push({ type: "text" as const, text: `Updated mapping for index "${index}".` }); } // get current mapping structure const updatedMappings = await esClient.indices.getMapping({ index }); content.push({ type: "text" as const, text: `\nCurrent mapping structure:\n${JSON.stringify(updatedMappings[index].mappings, null, 2)}` }); return { content }; } catch (error) { console.error(`Failed to set mapping: ${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:149-167 (registration)Registers the 'create_mapping' tool with the MCP server, including input schema validation using Zod for 'index' and 'mappings' parameters, description, and links to the handler function.server.tool( "create_mapping", "Create or update the mapping structure of an Elasticsearch index", { index: z .string() .trim() .min(1, "Index name is required") .describe("Elasticsearch index name"), mappings: z .record(z.any()) .describe("Index mappings, defining field types, etc.") }, async ({ index, mappings }) => { return await createMapping(esClient, index, mappings); } );
- src/server.ts:152-162 (schema)Input schema for the create_mapping tool defined using Zod, specifying required 'index' string and 'mappings' object.{ index: z .string() .trim() .min(1, "Index name is required") .describe("Elasticsearch index name"), mappings: z .record(z.any()) .describe("Index mappings, defining field types, etc.") },