# How can i have a field in my parent doc in ES to store the number of child docs in it

**URL:** <https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693>\
**Category:** Elasticsearch\
**Created:** [September 2, 2019, 1:17pm UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693 "2019-09-02T13:17:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 2, 2019, 1:17pm UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/1 "2019-09-02T13:17:45Z")

</div>

> <https://stackoverflow.com/questions/57753384/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs>

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [September 3, 2019, 12:53am UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/2 "2019-09-03T00:53:11Z")

</div>

Hi @yash.tandon ,

I'm confuse with your question do you want to store the count i.e 3 \<--- numeral value? or the id of your child in a list like the example you provide? "eg parent:[child1, child2, child3]."

If it's just a numeral value it can be more easy to update as it can be update by script for one field, keeping a list of value is not easy to maintain and take space if you only need the count. But have the benefice to know exactly which value are inside and can prevent some data corruption.

In all case each time you update your document lucene will delete and create a new document so if you think that you'll have a lot of update, it's better to use a time based index, here also it depend on your context. Do you need to always update your documents?  
If you have too much deleted document in your index, it will become very slow, you can always reindex your data when your deleted documents reach 15%~20%. Depends also on your case, you need to make some tests.

> Also the child docs are of three types and parent is single.

After reading 3 times I understand even differently as your parent document will always have three value one for each child example: father [1,3,2] \<-- with 1 is the count for child1, 3 for child2 and so on...

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 3, 2019, 5:01am UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/3 "2019-09-03T05:01:01Z")

</div>

let me rephrase it once again suppose i have a parent doc 'p1' and i have 3 types of child doc i.e c1, c2, c3 now in my relationship i have 100 'c1' doc, 200 'c2' doc and 300 'c3' doc asscociated with my parent doc 'p1' now i want to maintain these c1, c2, c3 doc's count in my parent doc i.e 100, 200 and 300 respectively. This is the scenario that i want.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [September 3, 2019, 1:49pm UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/4 "2019-09-03T13:49:43Z")

</div>

What about this if your children are only 3 types, you can make a parent document like this:

```
{
    "id": 123,
    "name": "parent",
    "c1": 100,
    "c2": 200,
    "c3": 300,
   .....
}

```

You can update by script, make aggregations, check easily if your data are corrupted etc...

---

<div class="post-metadata">

**Author:** ![yash.tandon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash.tandon/32/53428_2.png) [@yash.tandon](https://discuss.elastic.co/u/yash.tandon)\
**Post date:** [September 4, 2019, 4:57am UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/5 "2019-09-04T04:57:33Z")

</div>

well what after it how will i update these fields in the parent doc using script whenever a child doc is getting inserted in ES what is the query for it.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [September 6, 2019, 12:48am UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/6 "2019-09-06T00:48:13Z")

</div>

Hi @yash.tandon

Here an example from the documentation:  
[https://www.elastic.co/guide/en/elasticsearch/guide/master/partial-updates.html#\_updates\_and\_conflicts](https://www.elastic.co/guide/en/elasticsearch/guide/master/partial-updates.html#_updates_and_conflicts)

---

<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:** [October 4, 2019, 12:48am UTC](https://discuss.elastic.co/t/how-can-i-have-a-field-in-my-parent-doc-in-es-to-store-the-number-of-child-docs-in-it/197693/7 "2019-10-04T00:48:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
