Multi parent child relation

Hey,

After reading a lot, looks like there is no support for multi parent documents in elasticsearch.
It maybe indicate wrong modeling of my documents.
The thing is I am not sure what or how I can handle this use case which used to be easy in a relational database by using a foreign key.

I have the following documents:

doc1:

{
  _id: 10
  "source":{
    "first_name": "child1_first_name",
    "last_name": "child1_last_name",
    "age" : 10,
    "phone": "0505055050",
    .
    .
    "mother":{
      "id": 100
      "firt_name": "mother1",
      "last_name": "last_name1"
    }
  }
}

doc2:

{
  _id: 11
  "source":{
    "first_name": "child2_first_name",
    "last_name": "child2_last_name",
    "age" : 12,
    "phone": "878787878",
    .
    .
    "mother":{
      "id": 100
      "firt_name": "mother1",
      "last_name": "last_name1"
    }
  }
}

doc3:

{
  _id: 100
  "source":{
    "first_name": "mother1",
    "last_name": "last_name1",
    "age" : 40,
    "phone": "12212121212",
    .
    .
    "mother":{}
  }
}

The above documents have only one mother and it can be a potential parent document,
but it can also have a father.
My problem is what to do when the mother or father update their first name?
How does the children document will get this information?

Should I, on a change, iterate on the relevant children and update them?
Or there is a more elegant way?

You can have parents, grand parents, great grand parents etc.
But you cannot have multiple parents, no.

If you want that sort of functionality the you need to index the documents multiple times, one per parent.

As Elasticsearch is not a relational system, data often need to be modelled quite differently compared to a traditional relational database. Looking at parent-child relationships as a substitute for foreign key constraints is rarely the right approach. When performing this modelling, there are a number of tools to your disposal. While parent-child relationships are useful when there is a single parent entity that is updated frequently and have a large number of children, denormalising your model generally performs and scales better even though there may be some extra work to do when updating entities.

1 Like