---
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: "my_index"
      body:
        settings:
          number_of_shards: 1
        mappings:
          dynamic: false
          properties:
            my_vector:
              type: "dense_vector"
              dims: 4
              index: true
              similarity: "l2_norm"
            my_name:
              type: "keyword"
              store: true
        aliases:
          my_alias:
            filter:
              term:
                my_name: "v2"
          my_alias1:
            filter:
              term:
                my_name: "v1"
    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:
- requires:
    cluster_features: "search.vectors.k_param_supported"
    reason: "k param for knn as query is required"
- do:
    search:
      index: "my_index"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            num_candidates: 5
            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.0.fields.my_name.0: "v1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.1.fields.my_name.0: "v2"
- match:
    hits.hits.2._id: "3"
- match:
    hits.hits.2.fields.my_name.0: "v1"
---
'PRE_FILTER: knn query with alias filter as pre-filter':
- do:
    search:
      index: "my_alias"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            num_candidates: 5
            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: "2"
- match:
    hits.hits.0.fields.my_name.0: "v2"
- match:
    hits.hits.1._id: "4"
- match:
    hits.hits.1.fields.my_name.0: "v2"
- match:
    hits.hits.2._id: "6"
- match:
    hits.hits.2.fields.my_name.0: "v2"
- do:
    search:
      index: "my_alias"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            num_candidates: 5
            filter:
              term:
                my_name: "v1"
    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
- length:
    hits.hits: 0
- do:
    search:
      index: "my_alias"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          bool:
            should:
            - wildcard:
                my_name:
                  value: "v*"
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                num_candidates: 5
                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: "2"
- match:
    hits.hits.0.fields.my_name.0: "v2"
- match:
    hits.hits.1._id: "4"
- match:
    hits.hits.1.fields.my_name.0: "v2"
- match:
    hits.hits.2._id: "6"
- match:
    hits.hits.2.fields.my_name.0: "v2"
---
'PRE_FILTER: pre-filter across multiple internal filters':
- do:
    search:
      index: "my_index"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            num_candidates: 5
            filter:
            - term:
                my_name: "v1"
            - term:
                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"
- match:
    hits.total.value: 0
- length:
    hits.hits: 0
---
'PRE_FILTER: pre-filter across multiple aliases':
- do:
    search:
      index: "my_alias,my_alias1"
      body:
        size: 6
        fields:
        - "my_name"
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            num_candidates: 100
            k: 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"
- match:
    hits.total.value: 10
- length:
    hits.hits: 6
- match:
    hits.hits.0._id: "1"
- match:
    hits.hits.0.fields.my_name.0: "v1"
- match:
    hits.hits.1._id: "2"
- match:
    hits.hits.1.fields.my_name.0: "v2"
- match:
    hits.hits.2._id: "3"
- match:
    hits.hits.2.fields.my_name.0: "v1"
- match:
    hits.hits.3._id: "4"
- match:
    hits.hits.3.fields.my_name.0: "v2"
- match:
    hits.hits.4._id: "5"
- match:
    hits.hits.4.fields.my_name.0: "v1"
- match:
    hits.hits.5._id: "6"
- match:
    hits.hits.5.fields.my_name.0: "v2"
---
'PRE_FILTER: knn query with internal filter as pre-filter':
- do:
    search:
      index: "my_index"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          knn:
            field: "my_vector"
            query_vector:
            - 1
            - 1
            - 1
            - 1
            num_candidates: 5
            k: 5
            filter:
              term:
                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"
- match:
    hits.total.value: 5
- length:
    hits.hits: 3
- match:
    hits.hits.0._id: "2"
- match:
    hits.hits.0.fields.my_name.0: "v2"
- match:
    hits.hits.1._id: "4"
- match:
    hits.hits.1.fields.my_name.0: "v2"
- match:
    hits.hits.2._id: "6"
- match:
    hits.hits.2.fields.my_name.0: "v2"
---
'POST_FILTER: knn query with filter from a parent bool query as post-filter':
- do:
    search:
      index: "my_index"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          bool:
            must:
            - term:
                my_name: "v2"
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                num_candidates: 5
                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: 2
- length:
    hits.hits: 2
- match:
    hits.hits.0._id: "2"
- match:
    hits.hits.0.fields.my_name.0: "v2"
- match:
    hits.hits.1._id: "4"
- match:
    hits.hits.1.fields.my_name.0: "v2"
- do:
    search:
      index: "my_index"
      body:
        size: 3
        fields:
        - "my_name"
        query:
          bool:
            must:
            - term:
                my_name: "v2"
            - knn:
                field: "my_vector"
                query_vector:
                - 1
                - 1
                - 1
                - 1
                num_candidates: 5
                filter:
                  term:
                    my_name: "v1"
    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
- length:
    hits.hits: 0
