# Can ES support to add a new item into the nested objects of existing document?

**URL:** https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646
**Category:** Elasticsearch
**Created:** [August 19, 2015, 7:46am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646 "2015-08-19T07:46:53Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![gin](https://avatars.discourse-cdn.com/v4/letter/g/94ad74/32.png) [@gin](https://discuss.elastic.co/u/gin)
#### Post date: [August 19, 2015, 7:46am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/1 "2015-08-19T07:46:53Z")

</div>

Hi all,

I have a ES document like:

```
{
  "title": "Nest eggs",
  "comments": [ 
    {
      "name": "John Smith",
      "comment": "Great article",
    },
    {
      "name": "Alice White",
      "comment": "More like this please",
    }
  ]
}

```

and now I'd like to add a new "comments" in this document and finially the document will be:

```
{
  "title": "Nest eggs",
  "comments": [ 
    {
      "name": "John Smith",
      "comment": "Great article",
    },
    {
      "name": "Alice White",
      "comment": "More like this please",
    },
	{
      "name": "New guy",
      "comment": "something here",
    }
  ]
}

```

Does someone know how to do it?

Thanks a lot

---

<div class="post-metadata">

### Author: ![gin](https://avatars.discourse-cdn.com/v4/letter/g/94ad74/32.png) [@gin](https://discuss.elastic.co/u/gin)
#### Post date: [August 21, 2015, 8:37am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/2 "2015-08-21T08:37:00Z")

</div>

Any one knows it?

---

<div class="post-metadata">

### Author: ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)
#### Post date: [August 21, 2015, 8:58am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/3 "2015-08-21T08:58:39Z")

</div>

You can update the comments array, but you would have to send an array with the two comments that already exist plus the new comment.

[https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html)

---

<div class="post-metadata">

### Author: ![gin](https://avatars.discourse-cdn.com/v4/letter/g/94ad74/32.png) [@gin](https://discuss.elastic.co/u/gin)
#### Post date: [August 24, 2015, 1:43am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/4 "2015-08-24T01:43:44Z")

</div>

> [@dantuff](#):
>
> You can update the comments array, but you would have to send an array with the two comments that already exist plus the new comment.
> 
> [Partial Updates to Documents | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html)

Thanks, Dan. It should have a more smart way 😄

---

<div class="post-metadata">

### Author: ![remram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remram/32/4077_2.png) [@remram](https://discuss.elastic.co/u/remram)
#### Post date: [August 24, 2015, 4:49am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/5 "2015-08-24T04:49:46Z")

</div>

I know probably this is not the solution you are looking for. But it might help you!

You can try parent child relationship instead of nested object: [https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html)

It will allow you to add/update/delete child in each parent. But you have to put them all in the same shard!

Cheers, Ramy

---

<div class="post-metadata">

### Author: ![gin](https://avatars.discourse-cdn.com/v4/letter/g/94ad74/32.png) [@gin](https://discuss.elastic.co/u/gin)
#### Post date: [August 24, 2015, 6:08am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/6 "2015-08-24T06:08:56Z")

</div>

> [@remram](#):
>
> I know probably this is not the solution you are looking for. But it might help you!
> 
> You can try parent child relationship instead of nested object: [Parent-Child Relationship | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html)
> 
> It will allow you to add/update/delete child in each parent. But you have to put them all in the same shard!
> 
> Cheers, Ramy

Thanks, Ramy! It looks quite nice for adding/updating/delete.  
But seems it hard to got parent and child docment in same query.

Still, appreciate your suggestion.

Cheers, Gin

---

<div class="post-metadata">

### Author: ![remram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remram/32/4077_2.png) [@remram](https://discuss.elastic.co/u/remram)
#### Post date: [August 24, 2015, 6:26am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/7 "2015-08-24T06:26:09Z")

</div>

Hi Gin,  
What do you mean with it?

> [@gin](#):
>
> But seems it hard to got parent and child docment in same query.

You are able to query parents by their children and on the opposite way as well!

- [Finding Parents by Their Children | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/has-child.html)
- [Finding Children by Their Parents | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/has-parent.html)

---

<div class="post-metadata">

### Author: ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)
#### Post date: [August 24, 2015, 7:47am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/8 "2015-08-24T07:47:53Z")

</div>

> [@gin](#):
>
> But seems it hard to got parent and child docment in same query.

You can use the [inner hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html) functionality to get the child documents in the same query when you use the has\_child query.

The parent/child approach will allow you to index a child document individually which is more efficient if you have large documents and a requirement to update frequently. However, parent/child queries are 5-10 times slower than nested queries. So it is worth keeping that in mind when deciding what is the best approach for your use case.

---

<div class="post-metadata">

### Author: ![gin](https://avatars.discourse-cdn.com/v4/letter/g/94ad74/32.png) [@gin](https://discuss.elastic.co/u/gin)
#### Post date: [August 25, 2015, 1:11am UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/9 "2015-08-25T01:11:22Z")

</div>

Ramy and Dan,

Thanks for your suggestion and those help a lot 😄

BR. Gin

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 11:54pm UTC](https://discuss.elastic.co/t/can-es-support-to-add-a-new-item-into-the-nested-objects-of-existing-document/27646/10 "2017-07-05T23:54:06Z")

</div>


