---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features: "search.vectors.k_param_supported"
    reason: "k param for knn as query is required"
    test_runner_features: "close_to"
- do:
    indices.create:
      index: "my_index"
      body:
        settings:
          number_of_shards: 1
        mappings:
          dynamic: false
          properties:
            my_vector:
              type: "dense_vector"
              dims: 4
              index: true
              similarity: "l2_norm"
              index_options:
                type: "hnsw"
                m: 16
                ef_construction: 200
            my_name:
              type: "keyword"
              store: true
    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: "my_index"
      body:
      - "{\"index\": {\"_id\": \"1\"}}"
      - "{\"my_vector\": [1, 1, 1, 1], \"my_name\": \"v1\"}"
      - "{\"index\": {\"_id\": \"2\"}}"
      - "{\"my_vector\": [1, 1, 1, 2], \"my_name\": \"v2\"}"
      - "{\"index\": {\"_id\": \"3\"}}"
      - "{\"my_vector\": [1, 1, 1, 3], \"my_name\": \"v1\"}"
      - "{\"index\": {\"_id\": \"4\"}}"
      - "{\"my_vector\": [1, 1, 1, 4], \"my_name\": \"v2\"}"
      - "{\"index\": {\"_id\": \"5\"}}"
      - "{\"my_vector\": [1, 1, 1, 5], \"my_name\": \"v1\"}"
      - "{\"index\": {\"_id\": \"6\"}}"
      - "{\"my_vector\": [1, 1, 1, 6], \"my_name\": \"v2\"}"
      - "{\"index\": {\"_id\": \"7\"}}"
      - "{\"my_vector\": [1, 1, 1, 7], \"my_name\": \"v1\"}"
      - "{\"index\": {\"_id\": \"8\"}}"
      - "{\"my_vector\": [1, 1, 1, 8], \"my_name\": \"v2\"}"
      - "{\"index\": {\"_id\": \"9\"}}"
      - "{\"my_vector\": [1, 1, 1, 9], \"my_name\": \"v1\"}"
      - "{\"index\": {\"_id\": \"10\"}}"
      - "{\"my_vector\": [1, 1, 1, 10], \"my_name\": \"v2\"}"
    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"
---
Simple knn query with k param:
- do:
    search:
      index: "my_index"
      body:
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            k: 5
    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: 5
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.2._id: "3"
- match:
    hits.hits.3._id: "4"
- match:
    hits.hits.4._id: "5"
- do:
    search:
      index: "my_index"
      body:
        size: 3
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            k: 5
    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: 3
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.2._id: "3"
- do:
    search:
      index: "my_index"
      body:
        size: 3
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            k: 5
            num_candidates: 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:
    hits.total.value: 5
- length:
    hits.hits: 3
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.2._id: "3"
---
Knn query within the standard retriever:
- do:
    search:
      index: "my_index"
      body:
        retriever:
          standard:
            filter:
              bool:
                must:
                  term:
                    my_name: "v1"
            query:
              knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                k: 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:
    hits.total.value: 5
- length:
    hits.hits: 5
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "3"
- match:
    hits.hits.2._id: "5"
- match:
    hits.hits.3._id: "7"
- match:
    hits.hits.4._id: "9"
---
Incorrect k param:
- do:
    catch: "bad_request"
    search:
      index: "my_index"
      body:
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            k: 5
            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:
    status: 400
- match:
    error.type: "x_content_parse_exception"
- match:
    error.caused_by.type: "illegal_argument_exception"
- match:
    error.caused_by.reason: "[num_candidates] cannot be less than [k]"
- do:
    catch: "bad_request"
    search:
      index: "my_index"
      body:
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            k: 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"
- match:
    status: 400
- match:
    error.type: "x_content_parse_exception"
- match:
    error.caused_by.type: "illegal_argument_exception"
- match:
    error.caused_by.reason: "[k] must be greater than 0"
---
Function score query with knn query with k param:
- do:
    search:
      index: "my_index"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          function_score:
            query:
              knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                k: 5
            functions:
            - filter:
                match:
                  my_name: "v1"
              weight: 10
            - filter:
                match:
                  my_name: "v2"
              weight: 100
            boost_mode: "multiply"
    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: 3
- match:
    hits.hits.0._id: "2"
- match:
    hits.hits.0.fields.my_name.0: "v2"
- close_to:
    hits.hits.0._score:
      value: 50.0
      error: 0.001
- match:
    hits.hits.1._id: "1"
- match:
    hits.hits.1.fields.my_name.0: "v1"
- close_to:
    hits.hits.1._score:
      value: 10.0
      error: 0.001
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2.fields.my_name.0: "v2"
- close_to:
    hits.hits.2._score:
      value: 10.0
      error: 0.001
---
dis_max query with knn query:
- do:
    search:
      index: "my_index"
      body:
        size: 10
        fields:
        - "my_name"
        query:
          dis_max:
            queries:
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                k: 5
                num_candidates: 10
            - match:
                my_name: "v2"
            tie_breaker: 0.8
    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: 8
- match:
    hits.hits.0._id: "2"
- match:
    hits.hits.0.fields.my_name.0: "v2"
- match:
    hits.hits.1._id: "1"
- match:
    hits.hits.1.fields.my_name.0: "v1"
- match:
    hits.hits.2._id: "4"
- match:
    hits.hits.2.fields.my_name.0: "v2"
- match:
    hits.hits.3._id: "6"
- match:
    hits.hits.3.fields.my_name.0: "v2"
- match:
    hits.hits.4._id: "8"
- match:
    hits.hits.4.fields.my_name.0: "v2"
- match:
    hits.hits.5._id: "10"
- match:
    hits.hits.5.fields.my_name.0: "v2"
- match:
    hits.hits.6._id: "3"
- match:
    hits.hits.6.fields.my_name.0: "v1"
- match:
    hits.hits.7._id: "5"
- match:
    hits.hits.7.fields.my_name.0: "v1"
---
Aggregations with collected number of docs depends on k param:
- do:
    search:
      index: "my_index"
      body:
        size: 2
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            k: 5
        aggs:
          my_agg:
            terms:
              field: "my_name"
              order:
                _key: "asc"
    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: 2
- match:
    hits.total.value: 5
- match:
    aggregations.my_agg.buckets.0.key: "v1"
- match:
    aggregations.my_agg.buckets.1.key: "v2"
- match:
    aggregations.my_agg.buckets.0.doc_count: 3
- match:
    aggregations.my_agg.buckets.1.doc_count: 2
