# Defining a mapping for join datatype

**URL:** https://discuss.elastic.co/t/defining-a-mapping-for-join-datatype/113429
**Category:** Elasticsearch
**Created:** [December 28, 2017, 9:42am UTC](https://discuss.elastic.co/t/defining-a-mapping-for-join-datatype/113429 "2017-12-28T09:42:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![oguz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oguz/32/41862_2.png) [@oguz](https://discuss.elastic.co/u/oguz)
#### Post date: [December 28, 2017, 9:42am UTC](https://discuss.elastic.co/t/defining-a-mapping-for-join-datatype/113429/1 "2017-12-28T09:42:46Z")

</div>

Hi All,

i couldn't figure out how to define a join datatype relationship following the documentation on [join datatype](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html)

Mapping:

```
curl -XPUT localhost:9200/bookings -H 'Content-Type: application/json' -d '
{
    "mappings": {
        "doc": {
            "properties": {
                "join_field": {
                    "type": "join",
                    "relations": {
                        "booking": "segment"
                    }
                },
                "booking": {
                    "type": "nested",
                    "properties": {
                        "booking_id": {
                            "type": "integer"
                        }
                    }
                },
                "segment": {
                    "type": "nested",
                    "properties": {
                        "service": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}

```

Parent:

```
curl -XPUT 'localhost:9200/bookings/doc/1?routing=1&refresh&pretty' -H 'Content-Type: application/json' -d'
{
    "booking": {
        "booking_id": 1,
        "join_field": {
            "name": "booking"
        }
    }
}
'

```

Child:

```
curl -XPUT 'localhost:9200/bookings/doc/2?routing=1&refresh&pretty' -H 'Content-Type: application/json' -d'
{
    "segment": {
        "service": "asdasd",
        "join_field": {
            "name": "segment",
            "parent": 1
        }
    }
}
'

```

Query:

```
curl -XGET 'localhost:9200/bookings/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "has_child" : {
            "type" : "segment",
            "query" : {
               "match_all": {}
            }
        }
    }
}'

```

Result:

```
    {
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : []
  }
}

```

Could you tell me what am i doing wrong here?

---

<div class="post-metadata">

### Author: ![oguz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oguz/32/41862_2.png) [@oguz](https://discuss.elastic.co/u/oguz)
#### Post date: [December 28, 2017, 12:14pm UTC](https://discuss.elastic.co/t/defining-a-mapping-for-join-datatype/113429/2 "2017-12-28T12:14:42Z")

</div>

I figured it, join\_fields should not be inside nested elements when indexing, so correct inputs should be as below:

```
curl -XPUT 'localhost:9200/bookings/doc/1?routing=1&refresh&pretty' -H 'Content-Type: application/json' -d'
{
    "booking": {
        "booking_id": 1
    },
        "join_field": {
            "name": "booking"
        }
}
'

curl -XPUT 'localhost:9200/bookings/doc/2?routing=1&refresh&pretty' -H 'Content-Type: application/json' -d'
{
    "segment": {
        "service": "asdasd"
    },
        "join_field": {
            "name": "segment",
            "parent": 1
        }
}
'
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 25, 2018, 12:15pm UTC](https://discuss.elastic.co/t/defining-a-mapping-for-join-datatype/113429/3 "2018-01-25T12:15:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
