---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    indices.create:
      index: "test1"
      body:
        settings:
          number_of_shards: 1
          index.highlight.max_analyzed_offset: 30
        mappings:
          properties:
            field1:
              type: "text"
            field2:
              type: "text"
              index_options: "offsets"
    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: "test1"
      id: "1"
      body:
        field1: "The quick brown fox went to the forest and saw another fox."
        field2: "The quick brown fox went to the forest and saw another fox."
    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: {}
    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"
---
Unified highlighter on a field WITHOUT OFFSETS exceeding index.highlight.max_analyzed_offset should FAIL:
- do:
    catch: "bad_request"
    search:
      rest_total_hits_as_int: true
      index: "test1"
      body:
        query:
          match:
            field1: "fox"
        highlight:
          type: "unified"
          fields:
            field1: {}
    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:
    error.root_cause.0.type: "illegal_argument_exception"
---
? Unified highlighter on a field WITHOUT OFFSETS exceeding index.highlight.max_analyzed_offset
  with max_analyzed_offset=20 should SUCCEED
: - requires:
      cluster_features:
      - "gte_v7.12.0"
      reason: "max_analyzed_offset query param added in 7.12.0"
  - do:
      search:
        rest_total_hits_as_int: true
        index: "test1"
        body:
          query:
            match:
              field1: "fox"
          highlight:
            type: "unified"
            fields:
              field1: {}
            max_analyzed_offset: "20"
      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.hits.0.highlight.field1.0: "The quick brown <em>fox</em> went to the forest\
        \ and saw another fox."
---
Plain highlighter on a field WITHOUT OFFSETS exceeding index.highlight.max_analyzed_offset should FAIL:
- do:
    catch: "bad_request"
    search:
      rest_total_hits_as_int: true
      index: "test1"
      body:
        query:
          match:
            field1: "fox"
        highlight:
          type: "plain"
          fields:
            field1: {}
    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:
    error.root_cause.0.type: "illegal_argument_exception"
---
? Plain highlighter on a field WITHOUT OFFSETS exceeding index.highlight.max_analyzed_offset
  with max_analyzed_offset=20 should SUCCEED
: - requires:
      cluster_features:
      - "gte_v7.12.0"
      reason: "max_analyzed_offset query param added in 7.12.0"
  - do:
      search:
        rest_total_hits_as_int: true
        index: "test1"
        body:
          query:
            match:
              field1: "fox"
          highlight:
            type: "plain"
            fields:
              field1: {}
            max_analyzed_offset: 20
      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.hits.0.highlight.field1.0: "The quick brown <em>fox</em> went to the forest\
        \ and saw another fox."
---
Unified highlighter on a field WITH OFFSETS exceeding index.highlight.max_analyzed_offset should SUCCEED:
- do:
    search:
      rest_total_hits_as_int: true
      index: "test1"
      body:
        query:
          match:
            field2: "fox"
        highlight:
          type: "unified"
          fields:
            field2: {}
    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.hits.0.highlight.field2.0: "The quick brown <em>fox</em> went to the forest\
      \ and saw another <em>fox</em>."
---
Plain highlighter on a field WITH OFFSETS exceeding index.highlight.max_analyzed_offset should FAIL:
- do:
    catch: "bad_request"
    search:
      rest_total_hits_as_int: true
      index: "test1"
      body:
        query:
          match:
            field2: "fox"
        highlight:
          type: "plain"
          fields:
            field2: {}
    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:
    error.root_cause.0.type: "illegal_argument_exception"
---
? Plain highlighter on a field WITH OFFSETS exceeding index.highlight.max_analyzed_offset
  with max_analyzed_offset=20 should SUCCEED
: - requires:
      cluster_features:
      - "gte_v7.12.0"
      reason: "max_analyzed_offset query param added in 7.12.0"
  - do:
      search:
        rest_total_hits_as_int: true
        index: "test1"
        body:
          query:
            match:
              field2: "fox"
          highlight:
            type: "plain"
            fields:
              field2: {}
            max_analyzed_offset: 20
      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.hits.0.highlight.field2.0: "The quick brown <em>fox</em> went to the forest\
        \ and saw another fox."
---
Plain highlighter with max_analyzed_offset < 0 should FAIL:
- skip:
    awaits_fix: "semantics of test has changed"
- requires:
    cluster_features:
    - "gte_v7.12.0"
    reason: "max_analyzed_offset query param added in 7.12.0"
- do:
    catch: "bad_request"
    search:
      rest_total_hits_as_int: true
      index: "test1"
      body:
        query:
          match:
            field2: "fox"
        highlight:
          type: "plain"
          fields:
            field2: {}
          max_analyzed_offset: -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"
- match:
    status: 400
- match:
    error.root_cause.0.type: "x_content_parse_exception"
- match:
    error.caused_by.type: "illegal_argument_exception"
- match:
    error.caused_by.reason: "[max_analyzed_offset] must be a positive integer"
