Elasticsearch Parent-Child Mapping and Indexing

585 views Asked by At

I was following the book "Elasticsearch: The Definitive Guide". This book is outdated and when something was not working I was searching it on the internet and making it work with newer versions. But I can't find anything useful for Parent-Child Mapping and Indexing.

For example:

{
    "mappings": {
        "branch": {},
        "employee": {
            "_parent": {
                "type": "branch" 
            }
        }
    }
 }

How can I represent following mapping in new version of Elasticsearch. And How can I index following parent:

{ "name": "London Westminster", "city": "London", "country": "UK" }

and following childer:

PUT company/employee/1?parent=London
{
    "name": "Alice Smith",
    "dob": "1970-10-24",
    "hobby": "hiking"
}

Also, I am using elasticsearch python client and providing examples in it would be appreciated.

1

There are 1 answers

0
ESCoder On BEST ANSWER

The _parent field has been removed in favor of the join field.

The join data type is a special field that creates parent/child relation within documents of the same index. The relations section defines a set of possible relations within the documents, each relation being a parent name and a child name.

Consider company as the parent and employee as its child

Index Mapping:

{
  "mappings": {
    "properties": {
      "my_join_field": { 
        "type": "join",
        "relations": {
          "company": "employee" 
        }
      }
    }
  }
}

Parent document in the company context

PUT /index-name/_doc/1
{
  "name": "London Westminster",
  "city": "London",
  "country": "UK",
  "my_join_field": {
    "name": "company"
  }
}

Child document

PUT /index-name/_doc/2?routing=1&refresh
{
  "name": "Alice Smith",
  "dob": "1970-10-24",
  "hobby": "hiking",
  "my_join_field": {
    "name": "employee",
    "parent": "1"
  }
}