Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete / add nested objects in Elastic search

I cannot find examples in the Elastic manual on nested objects on how to modify fields and nested objects of documents using RESTful commands in Kibana Sense. I am looking for something similar to Solrs atomic updates here, which allow to update specific fields of documents.

How do RESTful commands in Kibana Sense look like that accomplish this? The only related info in the manual I can find is on Partial Updates to Documents, but I do not know how this can be applied for this use case.

For example, straight from the Elastic docs:

PUT my_index
{
"mappings": {
    "my_type": {
    "properties": {
        "user": {
        "type": "nested" 
        }
    }
    }
}
}

PUT my_index/my_type/1
{
"group" : "fans",
"user" : [
    {
    "first" : "John",
    "last" :  "Smith"
    },
    {
    "first" : "Alice",
    "last" :  "White"
    }
]
} 

How can I delete an entry in the nested object, so that the document "1" looks like:

{
"group" : "fans",
"user" : [
    {
    "first" : "John",
    "last" :  "Smith"
    }
]
}

How can I add an entry in the nested object, so that the document "1" looks like:

{
"group" : "fans",
"user" : [
    {
    "first" : "John",
    "last" :  "Smith"
    },
    {
    "first" : "Alice",
    "last" :  "White"
    },
    {
    "first" : "Peter",
    "last" :  "Parker"
    }
]
}
like image 464
tkja Avatar asked Aug 12 '26 03:08

tkja


1 Answers

You will have to use scripted updates unless you want to fetch all nested objects then add / remove items and re-index them all which is the previous answer proposed. However if you have a lot of nested documents you should be doing partial updates / additions and deletes. It is much quicker from data transfer and indexing point of view.

Here is a good article how to do scripted updates in general:

https://iridakos.com/programming/2019/05/02/add-update-delete-elasticsearch-nested-objects

like image 179
Juha Vehnia Avatar answered Aug 14 '26 22:08

Juha Vehnia