Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - simulate foreign key behavior

I am having problem modeling my documents in Elasticsearch, while a foreign key in a relational db would have solved my problem very easily. The closer I got was trying a parent child relationship, but my case has multiple parents to the same document ( I might looked at it the wrong way)

Consider the following 3 documents from type person:
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":{}
  }
}

My problem is when the mother update her last name or first name. Do I need to find all documents, that id 100 is their mother, and change their inner documents to the right first name and last name?

This is just an example, my case is that I need to update 100+ children with the correct and new name of their mother. In a relational database this would be easy by using foreign key.

I tried to use this reference but couldn't find a solution to my problem.

Any idea?

like image 883
Tomer Avatar asked Aug 31 '25 04:08

Tomer


1 Answers

Looks like you look for this: Parent and Child documents in Elasticsearch https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html

So you would first need to adapt the mappingso that mother can be used as parent and as a type itself -> more on https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html

Then index as described in https://www.elastic.co/guide/en/elasticsearch/guide/current/indexing-parent-child.html .

PUT /index_name/child/10?parent=100

A change to the parent document now should lead to a change in all child documents.

Side new: This plugin might be interesting for the solution: http://siren.solutions/relational-joins-for-elasticsearch-the-siren-join-plugin/

like image 57
Andreas Neumann Avatar answered Sep 02 '25 18:09

Andreas Neumann