create_mapping
Create or update the mapping structure for an Elasticsearch index. Specify the index name and field type definitions to control how documents are indexed and searched.
Instructions
Create or update the mapping structure of an Elasticsearch index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | Elasticsearch index name | |
| mappings | Yes | Index mappings, defining field types, etc. |
Implementation Reference
- src/tools/createMapping.ts:1-61 (handler)The main handler function for the 'create_mapping' tool. It checks if an index exists, creates/updates mappings accordingly, and returns the current mapping structure.
import { Client } from "@elastic/elasticsearch"; 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:148-166 (registration)Registration of the 'create_mapping' tool using server.tool() with Zod schema for input validation (index and mappings parameters).
// Create or update the mapping structure of an Elasticsearch index 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:10-10 (helper)Import of the createMapping function from its implementation file.
import { createMapping } from "./tools/createMapping.js"; - src/server.ts:21-21 (helper)Re-export of createMapping from the server module.
createMapping, - src/server.ts:152-161 (schema)Zod schema definitions for the 'create_mapping' tool input validation: 'index' (required string) and 'mappings' (required record of any).
{ 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.")