The join data type represents parent-child relationships between documents in the same Elasticsearch index. Although Elasticsearch normally keeps documents independent and denormalized for speed, join supports relationships when they are needed. section 4.6.5
Listing 4.36 Mapping of the doctors schema definition
PUT doctors
{
"mappings": {
"properties": {
"relationship":{ #1
"type": "join",
"relations":{
"doctor":"patient" #2
}
}
}
}
}
Define a join field and declare the allowed relationship with a relations object; this example connects doctors to patients. section 4.6.5
A doctor-patient relationship is modeled by:
- Indexing the doctor with relationship.name set to the parent value, doctor. section 4.6.5
- Indexing each patient with relationship.name set to patient and parent set to the associated doctor's document ID. section 4.6.5
- Using routing so the parent and children are indexed into the same shard, avoiding multi-shard search overhead. section 4.6.5
Find a doctor's patients
Run a parent_id query with type set to patient and id set to the doctor's document ID. For Dr. Montgomery, the query retrieves patients whose parent is document 1. section 4.6.5
