---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features: "gte_v8.13.0"
    reason: "kNN flat index added in 8.13"
- do:
    indices.create:
      index: "flat"
      body:
        mappings:
          properties:
            name:
              type: "keyword"
            vector:
              type: "dense_vector"
              dims: 5
              index: true
              similarity: "l2_norm"
              index_options:
                type: "flat"
            another_vector:
              type: "dense_vector"
              dims: 5
              index: true
              similarity: "l2_norm"
              index_options:
                type: "flat"
    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: "flat"
      id: "1"
      body:
        name: "cow.jpg"
        vector:
        - 230.0
        - 300.33
        - -34.8988
        - 15.555
        - -200.0
        another_vector:
        - 130.0
        - 115.0
        - -1.02
        - 15.555
        - -100.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: "flat"
      id: "2"
      body:
        name: "moose.jpg"
        vector:
        - -0.5
        - 100.0
        - -13
        - 14.8
        - -156.0
        another_vector:
        - -0.5
        - 50.0
        - -1
        - 1
        - 120
    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: "flat"
      id: "3"
      body:
        name: "rabbit.jpg"
        vector:
        - 0.5
        - 111.3
        - -13.0
        - 14.8
        - -156.0
        another_vector:
        - -0.5
        - 11.0
        - 0
        - 12
        - 111.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"
---
kNN search only:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          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: "3"
- match:
    hits.hits.1.fields.name.0: "rabbit.jpg"
---
kNN multi-field search only:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
        - field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          k: 2
          num_candidates: 3
        - field: "another_vector"
          query_vector:
          - -0.5
          - 11.0
          - 0
          - 12
          - 111.0
          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: "3"
- match:
    hits.hits.0.fields.name.0: "rabbit.jpg"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.1.fields.name.0: "moose.jpg"
---
kNN search plus query:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          k: 2
          num_candidates: 3
        query:
          term:
            name: "cow.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.hits.0._id: "1"
- match:
    hits.hits.0.fields.name.0: "cow.jpg"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.1.fields.name.0: "moose.jpg"
- match:
    hits.hits.2._id: "3"
- match:
    hits.hits.2.fields.name.0: "rabbit.jpg"
---
kNN multi-field search with query:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
        - field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          k: 2
          num_candidates: 3
        - field: "another_vector"
          query_vector:
          - -0.5
          - 11.0
          - 0
          - 12
          - 111.0
          k: 2
          num_candidates: 3
        query:
          term:
            name: "cow.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.hits.0._id: "3"
- match:
    hits.hits.0.fields.name.0: "rabbit.jpg"
- match:
    hits.hits.1._id: "1"
- match:
    hits.hits.1.fields.name.0: "cow.jpg"
- match:
    hits.hits.2._id: "2"
- match:
    hits.hits.2.fields.name.0: "moose.jpg"
---
kNN search with filter:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          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: "flat"
      body:
        fields:
        - "name"
        knn:
          field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          k: 2
          num_candidates: 3
          filter:
          - term:
              name: "rabbit.jpg"
          - term:
              _id: 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"
- match:
    hits.total.value: 0
---
KNN Vector similarity search only:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          num_candidates: 3
          k: 3
          field: "vector"
          similarity: 11
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 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"
- length:
    hits.hits: 1
- match:
    hits.hits.0._id: "2"
- match:
    hits.hits.0.fields.name.0: "moose.jpg"
---
Vector similarity with filter only:
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          num_candidates: 3
          k: 3
          field: "vector"
          similarity: 11
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          filter:
            term:
              name: "moose.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"
- length:
    hits.hits: 1
- match:
    hits.hits.0._id: "2"
- match:
    hits.hits.0.fields.name.0: "moose.jpg"
- do:
    search:
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          num_candidates: 3
          k: 3
          field: "vector"
          similarity: 110
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          filter:
            term:
              name: "cow.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"
- length:
    hits.hits: 0
---
Cosine similarity with indexed vector:
- skip:
    features: "headers"
- do:
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      rest_total_hits_as_int: true
      body:
        query:
          script_score:
            query:
              match_all: {}
            script:
              source: "cosineSimilarity(params.query_vector, 'vector')"
              params:
                query_vector:
                - 0.5
                - 111.3
                - -13.0
                - 14.8
                - -156.0
    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: 3
- match:
    hits.hits.0._id: "3"
- gte:
    hits.hits.0._score: 0.999
- lte:
    hits.hits.0._score: 1.001
- match:
    hits.hits.1._id: "2"
- gte:
    hits.hits.1._score: 0.998
- lte:
    hits.hits.1._score: 1.0
- match:
    hits.hits.2._id: "1"
- gte:
    hits.hits.2._score: 0.78
- lte:
    hits.hits.2._score: 0.791
---
Vector rescoring has no effect for non-quantized vectors and provides same results as non-rescored knn:
- requires:
    reason: "Quantized vector rescoring is required"
    test_runner_features:
    - "capabilities"
    capabilities:
    - method: "GET"
      path: "/_search"
      capabilities:
      - "knn_quantized_vector_rescore_oversample"
- skip:
    features: "headers"
- do:
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      rest_total_hits_as_int: true
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          k: 3
          num_candidates: 3
    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: 3
- set:
    hits.hits.0._score: "knn_score0"
- set:
    hits.hits.1._score: "knn_score1"
- set:
    hits.hits.2._score: "knn_score2"
- do:
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      rest_total_hits_as_int: true
      index: "flat"
      body:
        fields:
        - "name"
        knn:
          field: "vector"
          query_vector:
          - -0.5
          - 90.0
          - -10
          - 14.8
          - -156.0
          k: 3
          num_candidates: 3
          rescore_vector:
            oversample: 1.5
    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: 3
- match:
    hits.hits.0._score: "$knn_score0"
- match:
    hits.hits.1._score: "$knn_score1"
- match:
    hits.hits.2._score: "$knn_score2"
---
Test bad parameters:
- do:
    catch: "bad_request"
    indices.create:
      index: "bad_flat"
      body:
        mappings:
          properties:
            vector:
              type: "dense_vector"
              dims: 5
              index: true
              index_options:
                type: "flat"
                m: 42
    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"
