---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features: "search.vectors.k_param_supported"
    reason: "k param for knn as query is required"
- do:
    indices.create:
      index: "test"
      body:
        settings:
          index:
            number_of_shards: 1
        mappings:
          properties:
            name:
              type: "keyword"
            nested:
              type: "nested"
              properties:
                paragraph_id:
                  type: "keyword"
                vector:
                  type: "dense_vector"
                  dims: 5
                  index: true
                  similarity: "l2_norm"
        aliases:
          my_alias:
            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"
- do:
    index:
      index: "test"
      id: "1"
      body:
        name: "cow.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - 230.0
          - 300.33
          - -34.8988
          - 15.555
          - -200.0
        - paragraph_id: 1
          vector:
          - 240.0
          - 300
          - -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:
          - -0.5
          - 100.0
          - -13
          - 14.8
          - -156.0
        - paragraph_id: 2
          vector:
          - 0
          - 100.0
          - 0
          - 14.8
          - -156.0
        - paragraph_id: 3
          vector:
          - 0
          - 1.0
          - 0
          - 1.8
          - -15.0
    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:
          - 0.5
          - 111.3
          - -13.0
          - 14.8
          - -156.0
    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 that returns diverse parents docs:
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        query:
          nested:
            path: "nested"
            query:
              knn:
                field: "nested.vector"
                query_vector:
                - -0.5
                - 90.0
                - -10
                - 14.8
                - -156.0
                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: "3"
- match:
    hits.hits.1.fields.name.0: "rabbit.jpg"
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        query:
          nested:
            path: "nested"
            query:
              knn:
                field: "nested.vector"
                query_vector:
                - -0.5
                - 90.0
                - -10
                - 14.8
                - -156.0
                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.total.value: 3
- 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: "3"
- match:
    hits.hits.1.fields.name.0: "rabbit.jpg"
- match:
    hits.hits.1.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
- match:
    hits.hits.2._id: "1"
- match:
    hits.hits.2.fields.name.0: "cow.jpg"
- match:
    hits.hits.2.inner_hits.nested.hits.hits.0.fields.nested.0.paragraph_id.0: "0"
---
nested kNN search pre-filtered on alias with filter on top level fields:
- do:
    search:
      index: "my_alias"
      body:
        fields:
        - "name"
        query:
          nested:
            path: "nested"
            query:
              knn:
                field: "nested.vector"
                query_vector:
                - -0.5
                - 90.0
                - -10
                - 14.8
                - -156.0
                num_candidates: 1
            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 post-filtered on top level fields:
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        query:
          bool:
            must:
            - term:
                name: "rabbit.jpg"
            - nested:
                path: "nested"
                query:
                  knn:
                    field: "nested.vector"
                    query_vector:
                    - -0.5
                    - 90.0
                    - -10
                    - 14.8
                    - -156.0
                    num_candidates: 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: 0
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        query:
          bool:
            must:
            - term:
                name: "rabbit.jpg"
            - nested:
                path: "nested"
                query:
                  knn:
                    field: "nested.vector"
                    query_vector:
                    - -0.5
                    - 90.0
                    - -10
                    - 14.8
                    - -156.0
                    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.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 post-filtered on nested fields DOES NOT work:
- do:
    search:
      index: "test"
      body:
        fields:
        - "name"
        query:
          nested:
            path: "nested"
            query:
              bool:
                must:
                - term:
                    nested.paragraph_id: 3
                - knn:
                    field: "nested.vector"
                    query_vector:
                    - -0.5
                    - 90.0
                    - -10
                    - 14.8
                    - -156.0
                    num_candidates: 6
            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: 0
---
nested kNN search inner_hits size > 1:
- requires:
    cluster_features: "gte_v8.13.0"
    reason: "inner_hits on nested kNN search added in 8.13"
- do:
    index:
      index: "test"
      id: "4"
      body:
        name: "moose.jpg"
        nested:
        - paragraph_id: 0
          vector:
          - -0.5
          - 100.0
          - -13
          - 14.8
          - -156.0
        - paragraph_id: 2
          vector:
          - 0
          - 100.0
          - 0
          - 14.8
          - -156.0
        - paragraph_id: 3
          vector:
          - 0
          - 1.0
          - 0
          - 1.8
          - -15.0
    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:
          - -0.5
          - 100.0
          - -13
          - 14.8
          - -156.0
        - paragraph_id: 2
          vector:
          - 0
          - 100.0
          - 0
          - 14.8
          - -156.0
        - paragraph_id: 3
          vector:
          - 0
          - 1.0
          - 0
          - 1.8
          - -15.0
    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:
          - -0.5
          - 100.0
          - -13
          - 14.8
          - -156.0
        - paragraph_id: 2
          vector:
          - 0
          - 100.0
          - 0
          - 14.8
          - -156.0
        - paragraph_id: 3
          vector:
          - 0
          - 1.0
          - 0
          - 1.8
          - -15.0
    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"
      size: 3
      body:
        fields:
        - "name"
        query:
          nested:
            path: "nested"
            query:
              knn:
                field: "nested.vector"
                query_vector:
                - -0.5
                - 90.0
                - -10
                - 14.8
                - -156.0
                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
- 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
- do:
    search:
      index: "test"
      size: 5
      body:
        fields:
        - "name"
        query:
          nested:
            path: "nested"
            query:
              knn:
                field: "nested.vector"
                query_vector:
                - -0.5
                - 90.0
                - -10
                - 14.8
                - -156.0
                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: "rabbit.jpg"
- length:
    hits.hits.4.inner_hits.nested.hits.hits: 1
---
nested kNN query search with filter that might match nested docs:
- requires:
    cluster_features: "gte_v8.14.0"
    reason: "bugfix for matching non-nested docs in 8.14"
- do:
    indices.create:
      index: "nested_text"
      body:
        mappings:
          properties:
            range:
              type: "long"
            other_nested_thing:
              type: "nested"
              properties:
                text:
                  type: "text"
            paragraphs:
              type: "nested"
              properties:
                other_nested_thing:
                  type: "nested"
                  properties:
                    text:
                      type: "text"
                    vector:
                      type: "dense_vector"
                      dims: 2
                      index: true
                      similarity: "cosine"
                vector:
                  type: "dense_vector"
                  dims: 2
                  index: true
                  similarity: "cosine"
    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: "nested_text"
      id: "1"
      body:
        publish_date: "1"
        paragraphs:
        - vector:
          - 1
          - 1
          text: "some text"
        - vector:
          - 1
          - 2
          text: "some text"
          other_nested_thing:
          - text: "some text"
            vector:
            - 1
            - 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"
- do:
    index:
      index: "nested_text"
      id: "2"
      body:
        paragraphs:
        - vector:
          - 2
          - 1
          text: "some text"
        - vector:
          - 2
          - 2
          text: "some text"
          other_nested_thing:
          - text: "some text"
            vector:
            - 1
            - 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"
- 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: "nested_text"
      body:
        query:
          nested:
            path: "paragraphs"
            query:
              knn:
                field: "paragraphs.vector"
                query_vector:
                - 1
                - 2
                num_candidates: 10
                filter:
                  bool:
                    must_not:
                      exists:
                        field: "publish_date"
    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: "2"
