---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features: "gte_v8.14.0"
    reason: "kNN retriever added in 8.14"
- do:
    indices.create:
      index: "index1"
      body:
        settings:
          number_of_shards: 1
          number_of_replicas: 0
        mappings:
          properties:
            name:
              type: "keyword"
            vector:
              type: "dense_vector"
              dims: 5
              index: true
              index_options:
                type: "int8_hnsw"
              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:
    bulk:
      refresh: true
      index: "index1"
      body:
      - "{\"index\": {\"_id\": 1 }}"
      - "{\"name\": \"cow.jpg\", \"vector\": [1, 1, 1, 1, 1]}"
      - "{\"index\": {\"_id\": 2}}"
      - "{\"name\": \"moose.jpg\", \"vector\": [2, 2, 2, 2, 2]}"
      - "{\"index\": {\"_id\": 3 }}"
      - "{\"name\": \"rabbit.jpg\", \"vector\": [3, 3, 3, 3, 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"
---
kNN retriever:
- do:
    search:
      index: "index1"
      body:
        fields:
        - "name"
        retriever:
          knn:
            field: "vector"
            query_vector:
            - 2
            - 2
            - 2
            - 2
            - 3
            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 retriever with filter:
- do:
    search:
      index: "index1"
      body:
        fields:
        - "name"
        retriever:
          knn:
            field: "vector"
            query_vector:
            - 2
            - 2
            - 2
            - 2
            - 3
            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"
---
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: "index1"
      body:
        knn:
          field: "vector"
          query_vector:
          - 2
          - 2
          - 2
          - 2
          - 3
          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
- set:
    hits.hits.0._score: "rescore_score0"
- set:
    hits.hits.1._score: "rescore_score1"
- set:
    hits.hits.2._score: "rescore_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: "index1"
      body:
        query:
          script_score:
            query:
              match_all: {}
            script:
              source: "1.0 / (1.0 + Math.pow(l2norm(params.query_vector, 'vector'),\
                \ 2.0))"
              params:
                query_vector:
                - 2
                - 2
                - 2
                - 2
                - 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
- match:
    hits.hits.0._score: "$rescore_score0"
- match:
    hits.hits.1._score: "$rescore_score1"
- match:
    hits.hits.2._score: "$rescore_score2"
