CodexBloom - Programming Q&A Platform

Elasticsearch 8.5 Issues with Updating Nested Documents Using the Update API

👀 Views: 39 đŸ’Ŧ Answers: 1 📅 Created: 2025-09-06
elasticsearch nested-documents update-api scripting json

I've spent hours debugging this and I've spent hours debugging this and I've been struggling with this for a few days now and could really use some help... I'm stuck on something that should probably be simple. I'm working on a personal project and I can't seem to get I'm stuck on something that should probably be simple. I'm having trouble updating nested documents in Elasticsearch 8.5 using the Update API. I have a main document that contains a nested field representing a list of items. The structure looks like this: ```json { "user": "john_doe", "items": [ { "item_id": "1", "quantity": 2 }, { "item_id": "2", "quantity": 5 } ] } ``` I want to update the `quantity` of a specific item, but when I attempt to use the Update API, I keep getting an error. My request looks like this: ```json POST /my_index/_update/1 { "script": { "source": "ctx._source.items.find(i -> i.item_id == params.id).quantity += params.qty", "params": { "id": "1", "qty": 3 } } } ``` However, I receive the following error: ``` ScriptException[runtime error]; nested: IllegalArgumentException[No such property: i for class: Script1]; ``` I've tested various approaches to access and update the nested item, including trying a for loop and different script syntax, but none seem to work. I've verified that the document is indexed correctly and that I'm using the correct document ID, but I'm puzzled as to why the script cannot recognize `i`. I've also tried setting `inline` to `true`, but that didn't change the outcome. Is there a recommended way to structure the script for updating nested documents within Elasticsearch 8.5? Any insights would be greatly appreciated! For context: I'm using Json on macOS. Any ideas what could be causing this? Any help would be greatly appreciated! This is my first time working with Json latest. What would be the recommended way to handle this? The project is a microservice built with Json. For context: I'm using Json on Linux. I appreciate any insights! Any examples would be super helpful. I'm working in a macOS environment. Any pointers in the right direction? I recently upgraded to Json 3.9.