setup:
  - do:
      indices.create:
        index: test
        body:
          mappings:
            doc:
              properties:
                join_field: { "type": "join", "relations": { "parent": "child", "child": "grand_child" } }

  - do:
      index:
        index: test
        type:  doc
        id:    1
        body:  { "join_field": { "name": "parent" } }

  - do:
      index:
        index: test
        type:  doc
        id:    2
        body:  { "join_field": { "name": "parent" } }

  - do:
      index:
        index: test
        type:  doc
        id:    3
        routing: 1
        body:  { "join_field": { "name": "child", "parent": "1" } }

  - do:
      index:
        index: test
        type:  doc
        id:    4
        routing: 1
        body:  { "join_field": { "name": "child", "parent": "1" } }

  - do:
      index:
        index: test
        type:  doc
        id:    5
        routing: 1
        body:  { "join_field": { "name": "child", "parent": "2" } }

  - do:
      index:
        index: test
        type:  doc
        id:    6
        routing: 1
        body:  { "join_field": { "name": "grand_child", "parent": "5" } }

  - do:
      indices.refresh: {}

---
"Test basic":
    - skip:
        version: " - 5.5.99"
        reason:  parent-join was added in 5.6

    - do:
        search:
          body: { sort: ["join_field", "_id"] }

    - match: { hits.total: 6 }
    - match: { hits.hits.0._index: "test" }
    - match: { hits.hits.0._type: "doc" }
    - match: { hits.hits.0._id: "3" }
    - match: { hits.hits.0._source.join_field.name: "child" }
    - match: { hits.hits.0._source.join_field.parent: "1" }
    - is_false: hits.hits.0.fields.join_field#child }
    - match: { hits.hits.1._index: "test" }
    - match: { hits.hits.1._type: "doc" }
    - match: { hits.hits.1._id: "4" }
    - match: { hits.hits.1._source.join_field.name: "child" }
    - match: { hits.hits.1._source.join_field.parent: "1" }
    - is_false: hits.hits.1.fields.join_field#child }
    - match: { hits.hits.2._index: "test" }
    - match: { hits.hits.2._type: "doc" }
    - match: { hits.hits.2._id: "5" }
    - match: { hits.hits.2._source.join_field.name: "child" }
    - match: { hits.hits.2._source.join_field.parent: "2" }
    - is_false: hits.hits.2.fields.join_field#child }
    - match: { hits.hits.3._index: "test" }
    - match: { hits.hits.3._type: "doc" }
    - match: { hits.hits.3._id: "6" }
    - match: { hits.hits.3._source.join_field.name: "grand_child" }
    - match: { hits.hits.3._source.join_field.parent: "5" }
    - match: { hits.hits.4._index: "test" }
    - match: { hits.hits.4._type: "doc" }
    - match: { hits.hits.4._id: "1" }
    - match: { hits.hits.4._source.join_field.name: "parent" }
    - is_false: hits.hits.4._source.join_field.parent
    - match: { hits.hits.5._index: "test" }
    - match: { hits.hits.5._type: "doc" }
    - match: { hits.hits.5._id: "2" }
    - match: { hits.hits.5._source.join_field.name: "parent" }
    - is_false: hits.hits.5._source.join_field.parent

---
"Test parent_id query":
    - skip:
        version: " - 5.5.99"
        reason:  parent-join was added in 5.6.

    - do:
        search:
          body:
            sort: [ "_id" ]
            query:
              parent_id:
                type: child
                id: 1

    - match: { hits.total: 2 }
    - match: { hits.hits.0._index: "test" }
    - match: { hits.hits.0._type: "doc" }
    - match: { hits.hits.0._id: "3" }
    - match: { hits.hits.0._source.join_field.name: "child" }
    - match: { hits.hits.0._source.join_field.parent: "1" }
    - match: { hits.hits.1._index: "test" }
    - match: { hits.hits.1._type: "doc" }
    - match: { hits.hits.1._id: "4" }
    - match: { hits.hits.1._source.join_field.name: "child" }
    - match: { hits.hits.1._source.join_field.parent: "1" }


