create_mapping
Define or modify the mapping structure of an Elasticsearch index to specify field types and properties, ensuring accurate data organization and retrieval.
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 main handler function that creates or updates Elasticsearch index mappings. Handles index existence check, creation or update, fetches updated mappings, and returns formatted content with success/error messages.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-166 (registration)Registers the 'create_mapping' tool with the MCP server. Defines input schema using Zod for 'index' (string) and 'mappings' (object), provides description, and wires 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:153-162 (schema)Input schema for the 'create_mapping' tool using Zod: requires 'index' (non-empty trimmed string) and 'mappings' (any record/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.") },
- src/server.ts:10-10 (registration)Import statement for the createMapping handler function.import { createMapping } from "./tools/createMapping.js";