CodexBloom - Programming Q&A Platform

Elasticsearch 8.5 scenarios to Update Nested Document Fields with Bulk API

👀 Views: 64 💬 Answers: 1 📅 Created: 2025-06-14
elasticsearch bulk-api nested-documents json

I'm working with an scenario while trying to update fields of nested documents in Elasticsearch 8.5 using the Bulk API. My index has a nested structure for user profiles, where each profile can have multiple addresses. Here’s a simplified mapping of my index: ```json { "mappings": { "properties": { "user_id": {"type": "keyword"}, "profiles": { "type": "nested", "properties": { "address": {"type": "text"}, "city": {"type": "text"} } } } } } ``` I’m trying to update the city for a specific address within a profile using the Bulk API. Here’s the format of my bulk request: ```json { "update": { "_id": "1", "_index": "users" } } { "script": { "source": "for (int i = 0; i < ctx._source.profiles.size(); i++) { if (ctx._source.profiles[i].address == params.address) { ctx._source.profiles[i].city = params.city; } }", "params": { "address": "123 Main St", "city": "New City" } } } ``` However, I’m running into an behavior message that says: `"TypeError: want to read property 'size' of null"`. I've double-checked the document with ID `1` and it does have profiles with the specified address, but the script seems to unexpected result with a null pointer exception. I’ve verified that the profiles field is correctly populated in the document. In addition, I tried to log the contents of `ctx._source.profiles` within the script but it’s not giving any output. Is there a specific way to handle updates for nested fields in the Bulk API? What could I be missing here?