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

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

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

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

  - do:
      index:
        index: source
        type:  doc
        id:    3
        routing: 1
        body:  { "join_field": { "name": "grand_child", "parent": "2" } }

  - do:
      indices.refresh: {}


---
"Reindex with parent join field":
  - do:
      reindex:
        refresh: true
        body:
          source:
            index: source
          dest:
            index: dest
  - match: {created: 3}

  - do:
      search:
        index: dest
        body:
          query:
            parent_id:
              type: child
              id: 1
  - match: {hits.total: 1}
  - match: {hits.hits.0._id: "2"}

  - do:
      search:
        index: dest
        body:
          query:
            has_parent:
              parent_type: child
              query:
                parent_id:
                  type: child
                  id: 1
  - match: {hits.total: 1}
  - match: {hits.hits.0._id: "3"}

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}


---
"Reindex from remote with parent join field":
  # Fetch the http host. We use the host of the master because we know there will always be a master.
  - do:
      cluster.state: {}
  - set: { master_node: master }
  - do:
      nodes.info:
        metric: [ http ]
  - is_true: nodes.$master.http.publish_address
  - set: {nodes.$master.http.publish_address: host}
  - do:
      reindex:
        refresh: true
        body:
          source:
            remote:
              host: http://${host}
            index: source
          dest:
            index: dest
  - match: {created: 3}

  - do:
      search:
        index: dest
        body:
          query:
            parent_id:
              type: child
              id: 1
  - match: {hits.total: 1}
  - match: {hits.hits.0._id: "2"}

  - do:
      search:
        index: dest
        body:
          query:
            has_parent:
              parent_type: child
              query:
                parent_id:
                  type: child
                  id: 1
  - match: {hits.total: 1}
  - match: {hits.hits.0._id: "3"}

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}
