---
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"
---
two levels fields collapsing:
- do:
    indices.create:
      index: "addresses"
      body:
        settings:
          number_of_shards: 1
        mappings:
          properties:
            country:
              type: "keyword"
            city:
              type: "keyword"
            address:
              type: "text"
    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:
    bulk:
      refresh: true
      body:
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"1\" } }"
      - "{\"country\" : \"Canada\", \"city\" : \"Saskatoon\", \"address\" : \"701\
        \ Victoria Avenue\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"2\" } }"
      - "{\"country\" : \"Canada\", \"city\" : \"Toronto\", \"address\" : \"74 Victoria\
        \ Street, Suite, 74 Victoria Street, Suite 300\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"3\" } }"
      - "{\"country\" : \"Canada\", \"city\" : \"Toronto\", \"address\" : \"350 Victoria\
        \ St\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"4\" } }"
      - "{\"country\" : \"Canada\", \"city\" : \"Toronto\", \"address\" : \"20 Victoria\
        \ Street\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"5\" } }"
      - "{\"country\" : \"UK\", \"city\" : \"London\", \"address\" : \"58 Victoria\
        \ Street\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"6\" } }"
      - "{\"country\" : \"UK\", \"city\" : \"London\", \"address\" : \"Victoria Street\
        \ Victoria Palace Theatre\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"7\" } }"
      - "{\"country\" : \"UK\", \"city\" : \"Manchester\", \"address\" : \"75 Victoria\
        \ street Westminster\" }"
      - "{ \"index\" : { \"_index\" : \"addresses\", \"_id\" : \"8\" } }"
      - "{\"country\" : \"UK\", \"city\" : \"London\", \"address\" : \"Victoria Station\
        \ Victoria Arcade\" }"
    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: "/parse_exception/"
    search:
      rest_total_hits_as_int: true
      index: "addresses"
      body:
        query:
          match:
            address: "victoria"
        collapse:
          field: "country"
          inner_hits:
            collapse:
              field: "city"
              inner_hits: {}
    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: "/parse_exception/"
    search:
      rest_total_hits_as_int: true
      index: "addresses"
      body:
        query:
          match:
            address: "victoria"
        collapse:
          field: "country"
          inner_hits:
            collapse:
              field: "city"
              collapse:
                field: "city"
    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: "addresses"
      body:
        query:
          match:
            address: "victoria"
        collapse:
          field: "country"
          inner_hits:
            name: "by_location"
            size: 3
            collapse:
              field: "city"
    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: 2
- match:
    hits.hits.0.fields.country:
    - "UK"
- match:
    hits.hits.0.inner_hits.by_location.hits.total: 4
- length:
    hits.hits.0.inner_hits.by_location.hits.hits: 2
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.0._id: "8"
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.0.fields.city:
    - "London"
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.1._id: "7"
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.1.fields.city:
    - "Manchester"
- match:
    hits.hits.1.fields.country:
    - "Canada"
- match:
    hits.hits.1.inner_hits.by_location.hits.total: 4
- length:
    hits.hits.1.inner_hits.by_location.hits.hits: 2
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.0._id: "1"
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.0.fields.city:
    - "Saskatoon"
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.1._id: "3"
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.1.fields.city:
    - "Toronto"
- do:
    search:
      rest_total_hits_as_int: true
      index: "addresses"
      body:
        query:
          match:
            address: "victoria"
        collapse:
          field: "country"
          inner_hits:
            name: "by_location"
            size: 3
            sort:
            - city: "desc"
            collapse:
              field: "city"
    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: 2
- match:
    hits.hits.0.fields.country:
    - "UK"
- match:
    hits.hits.0.inner_hits.by_location.hits.total: 4
- length:
    hits.hits.0.inner_hits.by_location.hits.hits: 2
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.0._id: "7"
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.0.fields.city:
    - "Manchester"
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.1._id: "5"
- match:
    hits.hits.0.inner_hits.by_location.hits.hits.1.fields.city:
    - "London"
- match:
    hits.hits.1.fields.country:
    - "Canada"
- match:
    hits.hits.1.inner_hits.by_location.hits.total: 4
- length:
    hits.hits.1.inner_hits.by_location.hits.hits: 2
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.0._id: "2"
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.0.fields.city:
    - "Toronto"
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.1._id: "1"
- match:
    hits.hits.1.inner_hits.by_location.hits.hits.1.fields.city:
    - "Saskatoon"
