---
setup:
- skip:
    reason: "https://github.com/elastic/elasticsearch/issues/109476"
    known_issues:
    - cluster_feature: "gte_v8.13.0"
      fixed_by: "gte_v8.14.0"
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    indices.create:
      index: "test"
      body:
        mappings:
          properties:
            numeric_group:
              type: "integer"
            tag:
              type: "keyword"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "test"
      id: "1"
      version_type: "external"
      version: 11
      body:
        numeric_group: 1
        tag: "A"
        sort: 10
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "test"
      id: "2"
      version_type: "external"
      version: 22
      body:
        numeric_group: 1
        tag: "B"
        sort: 6
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "test"
      id: "3"
      version_type: "external"
      version: 33
      body:
        numeric_group: 1
        tag: "A"
        sort: 24
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "test"
      id: "4"
      version_type: "external"
      version: 44
      body:
        numeric_group: 25
        tag: "B"
        sort: 10
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "test"
      id: "5"
      version_type: "external"
      version: 55
      body:
        numeric_group: 25
        tag: "A"
        sort: 5
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "test"
      id: "6"
      version_type: "external"
      version: 66
      body:
        numeric_group: 3
        tag: "B"
        sort: 36
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    indices.refresh:
      index: "test"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
---
field collapsing:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 3
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 36
- match:
    hits.hits.0._id: "6"
- is_false: "hits.hits.0.inner_hits"
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 24
- match:
    hits.hits.1._id: "3"
- is_false: "hits.hits.1.inner_hits"
- match:
    hits.hits.2._index: "test"
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.sort:
    - 10
- match:
    hits.hits.2._id: "4"
- is_false: "hits.hits.2.inner_hits"
---
field collapsing and from:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        from: 2
        collapse:
          field: "numeric_group"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 1
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 25
- match:
    hits.hits.0.sort:
    - 10
- match:
    hits.hits.0._id: "4"
- is_false: "hits.hits.0.inner_hits"
---
field collapsing and inner_hits:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
            size: 2
            sort:
            - sort: "asc"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 3
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 36
- match:
    hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits.hits.total: 1
- length:
    hits.hits.0.inner_hits.sub_hits.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._id: "6"
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 24
- match:
    hits.hits.1._id: "3"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.total: 3
- length:
    hits.hits.1.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._id: "2"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._id: "1"
- match:
    hits.hits.2._index: "test"
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.sort:
    - 10
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.total: 2
- length:
    hits.hits.2.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._id: "5"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4"
---
field collapsing, inner_hits, and fields:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
            size: 2
            sort:
            - sort: "asc"
            fields:
            - "tag"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- length:
    hits.hits: 3
- length:
    hits.hits.0.inner_hits.sub_hits.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0.fields.tag:
    - "B"
- length:
    hits.hits.1.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._id: "2"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0.fields.tag:
    - "B"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._id: "1"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1.fields.tag:
    - "A"
- length:
    hits.hits.2.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._id: "5"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0.fields.tag:
    - "A"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1.fields.tag:
    - "B"
---
field collapsing, inner_hits and maxConcurrentGroupRequests:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          max_concurrent_group_searches: 10
          inner_hits:
            name: "sub_hits"
            size: 2
            sort:
            - sort: "asc"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 3
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 36
- match:
    hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits.hits.total: 1
- length:
    hits.hits.0.inner_hits.sub_hits.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._id: "6"
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 24
- match:
    hits.hits.1._id: "3"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.total: 3
- length:
    hits.hits.1.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._id: "2"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._id: "1"
- match:
    hits.hits.2._index: "test"
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.sort:
    - 10
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.total: 2
- length:
    hits.hits.2.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._id: "5"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4"
---
field collapsing and scroll:
- do:
    catch: "/cannot use \\`collapse\\` in a scroll context/"
    search:
      rest_total_hits_as_int: true
      index: "test"
      scroll: "1s"
      body:
        collapse:
          field: "numeric_group"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
---
field collapsing and search_after with invalid sort:
- do:
    catch: "/Cannot use \\[collapse\\] in conjunction with \\[search_after\\] unless\
      \ the search is sorted on the same field. Multiple sort fields are not allowed./"
    search:
      index: "test"
      body:
        collapse:
          field: "numeric_group"
        search_after:
        - 6
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    catch: "/Cannot use \\[collapse\\] in conjunction with \\[search_after\\] unless\
      \ the search is sorted on the same field. Multiple sort fields are not allowed./"
    search:
      index: "test"
      body:
        collapse:
          field: "numeric_group"
        search_after:
        - 6
        - 42
        sort:
        - numeric_group: "asc"
          sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
---
field collapsing and search_after:
- do:
    search:
      index: "test"
      body:
        collapse:
          field: "numeric_group"
        sort:
        - numeric_group: "desc"
        size: 1
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total.value: 6
- length:
    hits.hits: 1
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 25
- match:
    hits.hits.0.sort:
    - 25
- do:
    search:
      index: "test"
      body:
        collapse:
          field: "numeric_group"
        sort:
        - numeric_group: "desc"
        search_after:
        - 25
        size: 2
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total.value: 6
- length:
    hits.hits: 2
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 3
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 1
---
no hits and inner_hits:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        size: 0
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
            size: 1
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 0
---
no hits and inner_hits max_score null:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        size: 0
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
            size: 1
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.max_score: null
---
field collapsing and multiple inner_hits:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
          - name: "sub_hits_asc"
            size: 2
            sort:
            - sort: "asc"
          - name: "sub_hits_desc"
            size: 1
            sort:
            - sort: "desc"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 3
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 36
- match:
    hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits_asc.hits.total: 1
- length:
    hits.hits.0.inner_hits.sub_hits_asc.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits_asc.hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits_desc.hits.total: 1
- length:
    hits.hits.0.inner_hits.sub_hits_desc.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits_desc.hits.hits.0._id: "6"
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 24
- match:
    hits.hits.1._id: "3"
- match:
    hits.hits.1.inner_hits.sub_hits_asc.hits.total: 3
- length:
    hits.hits.1.inner_hits.sub_hits_asc.hits.hits: 2
- match:
    hits.hits.1.inner_hits.sub_hits_asc.hits.hits.0._id: "2"
- match:
    hits.hits.1.inner_hits.sub_hits_asc.hits.hits.1._id: "1"
- match:
    hits.hits.1.inner_hits.sub_hits_desc.hits.total: 3
- length:
    hits.hits.1.inner_hits.sub_hits_desc.hits.hits: 1
- match:
    hits.hits.1.inner_hits.sub_hits_desc.hits.hits.0._id: "3"
- match:
    hits.hits.2._index: "test"
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.sort:
    - 10
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits_asc.hits.total: 2
- length:
    hits.hits.2.inner_hits.sub_hits_asc.hits.hits: 2
- match:
    hits.hits.2.inner_hits.sub_hits_asc.hits.hits.0._id: "5"
- match:
    hits.hits.2.inner_hits.sub_hits_asc.hits.hits.1._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits_desc.hits.total: 2
- length:
    hits.hits.2.inner_hits.sub_hits_desc.hits.hits: 1
- match:
    hits.hits.2.inner_hits.sub_hits_desc.hits.hits.0._id: "4"
---
field collapsing, inner_hits and version:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
            version: true
            size: 2
            sort:
            - sort: "asc"
        sort:
        - sort: "desc"
        version: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 3
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 36
- match:
    hits.hits.0._id: "6"
- match:
    hits.hits.0._version: 66
- match:
    hits.hits.0.inner_hits.sub_hits.hits.total: 1
- length:
    hits.hits.0.inner_hits.sub_hits.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._version: 66
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 24
- match:
    hits.hits.1._id: "3"
- match:
    hits.hits.1._version: 33
- match:
    hits.hits.1.inner_hits.sub_hits.hits.total: 3
- length:
    hits.hits.1.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._id: "2"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._version: 22
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._id: "1"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._version: 11
- match:
    hits.hits.2._index: "test"
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.sort:
    - 10
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2._version: 44
- match:
    hits.hits.2.inner_hits.sub_hits.hits.total: 2
- length:
    hits.hits.2.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._id: "5"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._version: 55
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._version: 44
---
field collapsing on a field alias:
- do:
    indices.create:
      index: "alias-test"
      body:
        mappings:
          properties:
            numeric_group:
              type: "alias"
              path: "other_numeric_group"
            other_numeric_group:
              type: "integer"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "alias-test"
      id: "1"
      body:
        other_numeric_group: 1
        sort: 6
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "alias-test"
      id: "2"
      body:
        other_numeric_group: 25
        sort: 10
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    indices.refresh:
      index: "alias-test"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    search:
      rest_total_hits_as_int: true
      index:
      - "alias-test"
      - "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 8
- length:
    hits.hits: 3
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.inner_hits.sub_hits.hits.total: 1
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.inner_hits.sub_hits.hits.total: 4
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.inner_hits.sub_hits.hits.total: 3
---
field collapsing, inner_hits and seq_no:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
            name: "sub_hits"
            seq_no_primary_term: true
            size: 2
            sort:
            - sort: "asc"
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.total: 6
- length:
    hits.hits: 3
- match:
    hits.hits.0._index: "test"
- match:
    hits.hits.0.fields.numeric_group:
    - 3
- match:
    hits.hits.0.sort:
    - 36
- match:
    hits.hits.0._id: "6"
- match:
    hits.hits.0.inner_hits.sub_hits.hits.total: 1
- length:
    hits.hits.0.inner_hits.sub_hits.hits.hits: 1
- match:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._id: "6"
- gte:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._seq_no: 0
- gte:
    hits.hits.0.inner_hits.sub_hits.hits.hits.0._primary_term: 1
- match:
    hits.hits.1._index: "test"
- match:
    hits.hits.1.fields.numeric_group:
    - 1
- match:
    hits.hits.1.sort:
    - 24
- match:
    hits.hits.1._id: "3"
- match:
    hits.hits.1.inner_hits.sub_hits.hits.total: 3
- length:
    hits.hits.1.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._id: "2"
- gte:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._seq_no: 0
- gte:
    hits.hits.1.inner_hits.sub_hits.hits.hits.0._primary_term: 1
- match:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._id: "1"
- gte:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._seq_no: 0
- gte:
    hits.hits.1.inner_hits.sub_hits.hits.hits.1._primary_term: 1
- match:
    hits.hits.2._index: "test"
- match:
    hits.hits.2.fields.numeric_group:
    - 25
- match:
    hits.hits.2.sort:
    - 10
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2.inner_hits.sub_hits.hits.total: 2
- length:
    hits.hits.2.inner_hits.sub_hits.hits.hits: 2
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._id: "5"
- gte:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._seq_no: 0
- gte:
    hits.hits.2.inner_hits.sub_hits.hits.hits.0._primary_term: 1
- match:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4"
- gte:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._seq_no: 0
- gte:
    hits.hits.2.inner_hits.sub_hits.hits.hits.1._primary_term: 1
---
Test collapse with inner_hits and missing name fails:
- requires:
    cluster_features:
    - "gte_v8.13.0"
    reason: "fixed in 8.13"
- do:
    catch: "bad_request"
    search:
      index: "test"
      body:
        collapse:
          field: "numeric_group"
          inner_hits:
            size: 1
        sort:
        - sort: "desc"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
