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

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...

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.

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...

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.

Hi @yash.tandon

Here an example from the documentation:
https://www.elastic.co/guide/en/elasticsearch/guide/master/partial-updates.html#_updates_and_conflicts

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