update_document
Modify specific fields in an existing document within an Elasticsearch index by providing the index name, document ID, and updated content.
Instructions
Update an existing document in a specific Elasticsearch index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| document | Yes | Partial document body to update (fields to change) | |
| id | Yes | Document ID to update | |
| index | Yes | Name of the Elasticsearch index |
Implementation Reference
- src/index.ts:477-500 (handler)MCP tool handler for 'update_document' that validates input, calls the Elasticsearch service, and formats success/error responses.async ({ index, id, document }) => { try { await esService.updateDocument(index, id, document); return { content: [ { type: "text", text: `Document with ID '${id}' updated in index '${index}'.`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error updating document: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/index.ts:463-476 (schema)Zod schema defining input parameters for the update_document tool: index (string), id (string), document (record).{ index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the Elasticsearch index"), id: z .string() .min(1, "Document ID is required") .describe("Document ID to update"), document: z .record(z.any()) .describe("Partial document body to update (fields to change)"), },
- src/index.ts:460-501 (registration)Registration of the 'update_document' tool on the MCP server using server.tool(), including name, description, schema, and handler.server.tool( "update_document", "Update an existing document in a specific Elasticsearch index", { index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the Elasticsearch index"), id: z .string() .min(1, "Document ID is required") .describe("Document ID to update"), document: z .record(z.any()) .describe("Partial document body to update (fields to change)"), }, async ({ index, id, document }) => { try { await esService.updateDocument(index, id, document); return { content: [ { type: "text", text: `Document with ID '${id}' updated in index '${index}'.`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error updating document: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } );
- Helper function in ElasticsearchService that executes the actual Elasticsearch client update operation.async updateDocument(index: string, id: string, document: any): Promise<any> { return await this.client.update({ index, id, doc: document, }); }