---
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"
- requires:
    cluster_features:
    - "gte_v8.10.0"
    reason: "Collapse with max score was fixed in 8.10.0"
    test_runner_features: "close_to"
- do:
    indices.create:
      index: "user_comments"
      body:
        settings:
          index:
            number_of_shards: 1
        mappings:
          properties:
            user_id:
              type: "keyword"
            comment:
              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:
      index: "user_comments"
      refresh: true
      body:
      - "{\"index\": {\"_id\": \"1\"}}"
      - "{\"user_id\": \"user1\", \"comment\": \"Canada is beautiful.\"}"
      - "{\"index\": {\"_id\": \"2\"}}"
      - "{\"user_id\": \"user1\", \"comment\": \"Canada is the second-largest country\
        \ in the world by land area.\"}"
      - "{\"index\": {\"_id\": \"3\"}}"
      - "{\"user_id\": \"user2\", \"comment\": \"The capital of Canada is Ottawa.\"\
        }"
      - "{\"index\": {\"_id\": \"4\"}}"
      - "{\"user_id\": \"user2\", \"comment\": \"Canada is cold.\"}"
      - "{\"index\": {\"_id\": \"5\"}}"
      - "{\"user_id\": \"user3\", \"comment\": \"Canada celebrates Canada Day on July\
        \ 1st.\"}"
    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"
---
Max score returned when there is no sort:
- do:
    search:
      index: "user_comments"
      body:
        query:
          match:
            comment: "Canada"
        collapse:
          field: "user_id"
    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: 5
- length:
    hits.hits: 3
- match:
    hits.hits.0.fields.user_id:
    - "user3"
- close_to:
    hits.max_score:
      value: 0.11545
      error: 1.0E-5
---
Max score is not returned when there is sort and track_scores is false (by default):
- do:
    search:
      index: "user_comments"
      body:
        sort:
          user_id: "asc"
        query:
          match:
            comment: "Canada"
        collapse:
          field: "user_id"
    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: 5
- length:
    hits.hits: 3
- match:
    hits.hits.0.fields.user_id:
    - "user1"
- is_false: "hits.max_score"
---
Max score returned when there is sort and track_scores is true:
- do:
    search:
      index: "user_comments"
      body:
        sort:
          user_id: "asc"
        query:
          match:
            comment: "Canada"
        collapse:
          field: "user_id"
        track_scores: 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.value: 5
- length:
    hits.hits: 3
- match:
    hits.hits.0.fields.user_id:
    - "user1"
- close_to:
    hits.max_score:
      value: 0.11545
      error: 1.0E-5
---
Max score returned over all documents not only top hits:
- do:
    search:
      index: "user_comments"
      body:
        size: 2
        sort:
          user_id: "asc"
        query:
          match:
            comment: "Canada"
        collapse:
          field: "user_id"
        track_scores: 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.value: 5
- length:
    hits.hits: 2
- close_to:
    hits.max_score:
      value: 0.11545
      error: 1.0E-5
- match:
    hits.hits.0.fields.user_id:
    - "user1"
- close_to:
    hits.hits.0._score:
      value: 0.1103
      error: 1.0E-5
- match:
    hits.hits.1.fields.user_id:
    - "user2"
- close_to:
    hits.hits.1._score:
      value: 0.08817
      error: 1.0E-5
