liveBook · Manning

Join data type

As explained in Elasticsearch in Action, Second Edition

The join data type is an Elasticsearch field type that represents parent-child relationships between documents in the same index.

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

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains mapping, parent and child indexing, routing, and parent_id querying, without detailing join internals.
Examples
Uses a doctor with two patients, a doctors index mapping, parent and child documents, and a parent_id search.
What it assumes
Assumes basic Elasticsearch indices, mappings, documents, IDs, and queries while defining the relationship model explicitly.
What you can do after
Build it from scratch by mapping a join field, indexing related documents, routing them, and querying children by parent ID.
How it connects
Connects join fields with parent-child relationships, denormalization, document IDs, routing, shards, and parent_id queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.