---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features: "mapper.vectors.bit_vectors"
    test_runner_features: "close_to"
    reason: "bit vectors added in 8.15"
- do:
    indices.create:
      index: "test"
      body:
        settings:
          index:
            number_of_shards: 2
        mappings:
          properties:
            name:
              type: "keyword"
            nested:
              type: "nested"
              properties:
                paragraph_id:
                  type: "keyword"
                vector:
                  type: "dense_vector"
                  dims: 40
                  index: true
                  element_type: "bit"
                  similarity: "l2_norm"
    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"
      body:
        name: "cow.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - 100
          - 20
          - -34
          - 15
          - -100
        - paragraph_id: 1
          vector:
          - 40
          - 30
          - -3
          - 1
          - -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"
- do:
    index:
      index: "test"
      id: "2"
      body:
        name: "moose.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - -1
          - 100
          - -13
          - 14
          - -127
        - paragraph_id: 2
          vector:
          - 0
          - 100
          - 0
          - 15
          - -127
        - paragraph_id: 3
          vector:
          - 0
          - 1
          - 0
          - 2
          - -15
    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"
      body:
        name: "rabbit.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - 1
          - 111
          - -13
          - 14
          - -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"
- 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"
---
nested kNN search only:
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 14
          - -127
          k: 2
          num_candidates: 3
    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._id: "2"
- match:
    hits.hits.0.fields.name.0: "moose.jpg"
- match:
    hits.hits.1._id: "1"
- match:
    hits.hits.1.fields.name.0: "cow.jpg"
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 14
          - -127
          k: 2
          num_candidates: 3
          inner_hits:
            size: 1
            fields:
            - "nested.paragraph_id"
            _source: false
    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._id: "2"
- match:
    hits.hits.0.fields.name.0: "moose.jpg"
- match:
    hits.hits.0.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.1._id: "1"
- match:
    hits.hits.1.fields.name.0: "cow.jpg"
- match:
    hits.hits.1.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
---
nested kNN search filtered:
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 14
          - -127
          k: 2
          num_candidates: 3
          filter:
            term:
              name: "rabbit.jpg"
    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: 1
- match:
    hits.hits.0._id: "3"
- match:
    hits.hits.0.fields.name.0: "rabbit.jpg"
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 14
          - -127
          k: 3
          num_candidates: 3
          filter:
            term:
              name: "rabbit.jpg"
          inner_hits:
            size: 1
            fields:
            - "nested.paragraph_id"
            _source: false
    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: 1
- match:
    hits.hits.0._id: "3"
- match:
    hits.hits.0.fields.name.0: "rabbit.jpg"
- match:
    hits.hits.0.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
---
nested kNN search inner_hits size > 1:
- do:
    index:
      index: "test"
      id: "4"
      body:
        name: "moose.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - -1
          - 90
          - -10
          - 14
          - -127
        - paragraph_id: 2
          vector:
          - 0
          - 100.0
          - 0
          - 14
          - -127
        - paragraph_id: 3
          vector:
          - 0
          - 1.0
          - 0
          - 2
          - -15
    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"
      body:
        name: "moose.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - -1
          - 100
          - -13
          - 14
          - -127
        - paragraph_id: 2
          vector:
          - 0
          - 100
          - 0
          - 15
          - -127
        - paragraph_id: 3
          vector:
          - 0
          - 1
          - 0
          - 2
          - -15
    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"
      body:
        name: "moose.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - -1
          - 100
          - -13
          - 15
          - -127
        - paragraph_id: 2
          vector:
          - 0
          - 100
          - 0
          - 15
          - -127
        - paragraph_id: 3
          vector:
          - 0
          - 1
          - 0
          - 2
          - -15
    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"
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 15
          - -127
          k: 3
          num_candidates: 5
          inner_hits:
            size: 2
            fields:
            - "nested.paragraph_id"
            _source: false
    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: 3
- length:
    hits.hits.0.inner_hits.nested.hits.hits: 2
- length:
    hits.hits.1.inner_hits.nested.hits.hits: 2
- length:
    hits.hits.2.inner_hits.nested.hits.hits: 2
- match:
    hits.hits.0.fields.name.0: "moose.jpg"
- match:
    hits.hits.0.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 15
          - -127
          k: 5
          num_candidates: 5
          inner_hits:
            size: 2
            fields:
            - "nested.paragraph_id"
            _source: false
    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
- match:
    hits.hits.0.fields.name.0: "moose.jpg"
- length:
    hits.hits.0.inner_hits.nested.hits.hits: 2
- match:
    hits.hits.0.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.0.inner_hits.nested.hits.hits.1.fields.nested.0.paragraph_id.0: "2"
- match:
    hits.hits.1.fields.name.0: "moose.jpg"
- length:
    hits.hits.1.inner_hits.nested.hits.hits: 2
- match:
    hits.hits.1.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.1.inner_hits.nested.hits.hits.1.fields.nested.0.paragraph_id.0: "2"
- match:
    hits.hits.2.fields.name.0: "moose.jpg"
- length:
    hits.hits.2.inner_hits.nested.hits.hits: 2
- match:
    hits.hits.2.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.2.inner_hits.nested.hits.hits.1.fields.nested.0.paragraph_id.0: "2"
- match:
    hits.hits.3.fields.name.0: "moose.jpg"
- length:
    hits.hits.3.inner_hits.nested.hits.hits: 2
- match:
    hits.hits.3.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.3.inner_hits.nested.hits.hits.1.fields.nested.0.paragraph_id.0: "2"
- match:
    hits.hits.4.fields.name.0: "cow.jpg"
- length:
    hits.hits.4.inner_hits.nested.hits.hits: 2
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 15
          - -127
          k: 3
          num_candidates: 3
          filter:
            term:
              name: "cow.jpg"
          inner_hits:
            size: 3
            fields:
            - "nested.paragraph_id"
            _source: false
    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: 1
- match:
    hits.hits.0._id: "1"
- length:
    hits.hits.0.inner_hits.nested.hits.hits: 2
- match:
    hits.hits.0.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.0.inner_hits.nested.hits.hits.1.fields.nested.0.paragraph_id.0: "1"
---
nested kNN search inner_hits & boosting:
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 15
          - -127
          k: 3
          num_candidates: 5
          inner_hits:
            size: 2
            fields:
            - "nested.paragraph_id"
            _source: false
    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"
- close_to:
    hits.hits.0._score:
      value: 0.8
      error: 1.0E-5
- close_to:
    hits.hits.0.inner_hits.nested.hits.hits.0._score:
      value: 0.8
      error: 1.0E-5
- close_to:
    hits.hits.1._score:
      value: 0.625
      error: 1.0E-5
- close_to:
    hits.hits.1.inner_hits.nested.hits.hits.0._score:
      value: 0.625
      error: 1.0E-5
- close_to:
    hits.hits.2._score:
      value: 0.5
      error: 1.0E-5
- close_to:
    hits.hits.2.inner_hits.nested.hits.hits.0._score:
      value: 0.5
      error: 1.0E-5
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        knn:
          field: "nested.vector"
          query_vector:
          - -1
          - 90
          - -10
          - 15
          - -127
          k: 3
          num_candidates: 5
          boost: 2
          inner_hits:
            size: 2
            fields:
            - "nested.paragraph_id"
            _source: false
    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"
- close_to:
    hits.hits.0._score:
      value: 1.6
      error: 1.0E-5
- close_to:
    hits.hits.0.inner_hits.nested.hits.hits.0._score:
      value: 1.6
      error: 1.0E-5
- close_to:
    hits.hits.1._score:
      value: 1.25
      error: 1.0E-5
- close_to:
    hits.hits.1.inner_hits.nested.hits.hits.0._score:
      value: 1.25
      error: 1.0E-5
- close_to:
    hits.hits.2._score:
      value: 1
      error: 1.0E-5
- close_to:
    hits.hits.2.inner_hits.nested.hits.hits.0._score:
      value: 1.0
      error: 1.0E-5
