---
setup:
  - requires:
      cluster_features: ["gte_v7.6.0"]
      reason: "The bug was corrected from 7.6.0"

  - do:
      indices.create:
        index: test
        body:
          mappings:
            properties:
              entity_type: { "type": "keyword" }
              join_field: { "type": "join", "relations": { "question": "answer", "person": "address" } }
          settings:
            number_of_shards: 1

  - do:
      index:
        index: test
        id: "1"
        body: { "join_field": { "name": "question" }, "entity_type": "question" }

  - do:
      index:
        index: test
        id: "2"
        routing: "1"
        body: { "join_field": { "name": "answer", "parent": 1} , "entity_type": "answer" }

  - do:
      index:
        index: test
        id: "3"
        body: { "join_field": { "name": "person" }, "entity_type": "person" }

  - do:
      index:
        index: test
        routing: "3"
        id: "4"
        body: { "join_field": { "name": "address", "parent": 3 }, "entity_type": "address" }

  - do:
      indices.refresh: {}

---
teardown:
  - requires:
      cluster_features: ["gte_v7.7.0"]
      reason: "implemented in 7.7.0"

  - do:
      cluster.put_settings:
        body:
          persistent:
            search.allow_expensive_queries: null

---
"Test two sub-queries with only one having inner_hits":
  - requires:
      cluster_features: ["gte_v7.7.0"]
      reason: "The bug was corrected from 7.7.0"

  - do:
      search:
        index: test
        body:
          query:
            bool:
              should:
                - term:
                    entity_type: person
                - has_parent:
                    parent_type: question
                    query:
                      match_all: {}
                    inner_hits: {}


  - match: { hits.total.value: 2 }
  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.0.inner_hits.question.hits.total.value: 0}
  - match: { hits.hits.1._id: "2" }
  - match: { hits.hits.1.inner_hits.question.hits.total.value: 1}
  - match: { hits.hits.1.inner_hits.question.hits.hits.0._id: "1"}

---
"HasParent disallow expensive queries":
  - requires:
      cluster_features: ["gte_v7.7.0"]
      reason: "implemented in 7.7.0"

  ### Update setting to false
  - do:
      cluster.put_settings:
        body:
          persistent:
            search.allow_expensive_queries: "false"
        flat_settings: true

  - match: {persistent: {search.allow_expensive_queries: "false"}}

  - do:
      catch: /\[joining\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./
      search:
        index: test
        body:
          query:
            bool:
              should:
                - term:
                    entity_type: person
                - has_parent:
                    parent_type: question
                    query:
                      match_all: {}
                    inner_hits: {}

---
profile fetch:
  - requires:
      cluster_features: "gte_v8.15.0"
      reason: "fetch fields and stored_fields using ValueFetcher"

  - do:
      search:
        index: test
        body:
          profile: true
          query:
            has_parent:
              parent_type: question
              query:
                match_all: {}
              inner_hits: {}

  - gt: { profile.shards.0.fetch.time_in_nanos: 0 }
  - gt: { profile.shards.0.fetch.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.breakdown.next_reader: 0 }
  - gt: { profile.shards.0.fetch.breakdown.load_stored_fields_count: 0 }
  - gt: { profile.shards.0.fetch.breakdown.load_stored_fields: 0 }
  - match: { profile.shards.0.fetch.debug.stored_fields: [_id, _routing, _source] }
  - length: { profile.shards.0.fetch.children: 4 }
  - match: { profile.shards.0.fetch.children.0.type: FetchFieldsPhase }
  - gt: { profile.shards.0.fetch.children.0.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.children.0.breakdown.next_reader: 0 }
  - gt: { profile.shards.0.fetch.children.0.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.children.0.breakdown.next_reader: 0 }
  - match: { profile.shards.0.fetch.children.1.type: FetchSourcePhase }
  - gt: { profile.shards.0.fetch.children.1.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.children.1.breakdown.next_reader: 0 }
  - gt: { profile.shards.0.fetch.children.1.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.children.1.breakdown.next_reader: 0 }
  - match: { profile.shards.0.fetch.children.2.type: InnerHitsPhase }
  - gt: { profile.shards.0.fetch.children.2.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.children.2.breakdown.next_reader: 0 }
  - gt: { profile.shards.0.fetch.children.2.breakdown.next_reader_count: 0 }
  - gt: { profile.shards.0.fetch.children.2.breakdown.next_reader: 0 }
  - match: { profile.shards.0.fetch.children.3.type: StoredFieldsPhase }
