---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features: "gte_v8.12.0"
    reason: "knn as query added in 8.12"
    test_runner_features: "close_to"
- do:
    indices.create:
      index: "my_index"
      body:
        settings:
          number_of_shards: 2
        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\"}"
      - "{\"index\": {\"_id\": \"11\"}}"
      - "{\"my_vector\": [1, 1, 1, 11], \"my_name\": \"v1\"}"
      - "{\"index\": {\"_id\": \"12\"}}"
      - "{\"my_vector\": [1, 1, 1, 12], \"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"
---
Search for 2 knn queries combines scores from them:
- do:
    search:
      index: "my_index"
      body:
        size: 6
        query:
          bool:
            should:
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                num_candidates: 100
                boost: 1.1
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 12
                num_candidates: 100
    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: 6
- match:
    hits.total.value: 12
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "12"
- match:
    hits.hits.2._id: "2"
- match:
    hits.hits.3._id: "11"
- match:
    hits.hits.4._id: "3"
- match:
    hits.hits.5._id: "10"
---
Hybrid search combines scores from knn and other queries:
- do:
    search:
      include_named_queries_score: true
      index: "my_index"
      body:
        size: 3
        query:
          bool:
            should:
            - wildcard:
                my_name:
                  value: "v*"
                  _name: "bm25_query"
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                num_candidates: 3
                _name: "knn_query"
    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: 3
- match:
    hits.total.value: 12
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.2._id: "3"
- close_to:
    hits.hits.0._score:
      value: 2.0
      error: 1.0E-5
- close_to:
    hits.hits.0.matched_queries.bm25_query:
      value: 1.0
      error: 1.0E-5
- close_to:
    hits.hits.0.matched_queries.knn_query:
      value: 1.0
      error: 1.0E-5
- close_to:
    hits.hits.1._score:
      value: 1.5
      error: 1.0E-5
- close_to:
    hits.hits.1.matched_queries.bm25_query:
      value: 1.0
      error: 1.0E-5
- close_to:
    hits.hits.1.matched_queries.knn_query:
      value: 0.5
      error: 1.0E-5
- close_to:
    hits.hits.2._score:
      value: 1.2
      error: 1.0E-5
- close_to:
    hits.hits.2.matched_queries.bm25_query:
      value: 1.0
      error: 1.0E-5
- close_to:
    hits.hits.2.matched_queries.knn_query:
      value: 0.2
      error: 1.0E-5
- do:
    search:
      include_named_queries_score: true
      index: "my_index"
      body:
        size: 3
        query:
          bool:
            should:
            - wildcard:
                my_name:
                  value: "v*"
                  boost: 100
                  _name: "bm25_query"
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                num_candidates: 3
                boost: 100
                _name: "knn_query"
    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: 3
- match:
    hits.total.value: 12
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.2._id: "3"
- close_to:
    hits.hits.0._score:
      value: 200.0
      error: 1.0E-5
- close_to:
    hits.hits.0.matched_queries.bm25_query:
      value: 100.0
      error: 1.0E-5
- close_to:
    hits.hits.0.matched_queries.knn_query:
      value: 100.0
      error: 1.0E-5
- close_to:
    hits.hits.1._score:
      value: 150.0
      error: 1.0E-5
- close_to:
    hits.hits.1.matched_queries.bm25_query:
      value: 100.0
      error: 1.0E-5
- close_to:
    hits.hits.1.matched_queries.knn_query:
      value: 50.0
      error: 1.0E-5
- close_to:
    hits.hits.2._score:
      value: 120
      error: 1.0E-5
- close_to:
    hits.hits.2.matched_queries.bm25_query:
      value: 100.0
      error: 1.0E-5
- close_to:
    hits.hits.2.matched_queries.knn_query:
      value: 20.0
      error: 1.0E-5
