setup:
  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 5
            number_of_replicas: 0
          mappings:
            properties:
              text:
                type: text
              keyword:
                type: keyword
              vector:
                type: dense_vector
                dims: 1
                index: true
                similarity: l2_norm
                index_options:
                  type: hnsw
                  ef_construction: 100
                  m: 16

  - do:
      index:
        index: test
        id: "1"
        body:
          text: "term term"
          keyword: "other"
          vector: [0.0]

  - do:
      index:
        index: test
        id: "2"
        body:
          text: "other"
          keyword: "other"
          vector: [1.0]

  - do:
      index:
        index: test
        id: "3"
        body:
          text: "term"
          keyword: "keyword"
          vector: [2.0]

  - do:
      indices.refresh: {}

---
"rrf retriever with a standard retriever and a knn retriever":

  - do:
      search:
        index: test
        body:
          track_total_hits: false
          fields: [ "text", "keyword" ]
          retriever:
            rrf:
              retrievers: [
                {
                  knn: {
                    field: vector,
                    query_vector: [ 0.0 ],
                    k: 3,
                    num_candidates: 3
                  }
                },
                {
                  standard: {
                    query: {
                      term: {
                        text: term
                      }
                    }
                  }
                }
              ]
              rank_window_size: 100
              rank_constant: 1
          size: 10

  - match: { hits.hits.0._id: "1" }
  - match: { hits.hits.0.fields.text.0: "term term" }
  - match: { hits.hits.0.fields.keyword.0: "other" }

  - match: { hits.hits.1._id: "3" }
  - match: { hits.hits.1.fields.text.0: "term" }
  - match: { hits.hits.1.fields.keyword.0: "keyword" }

  - match: { hits.hits.2._id: "2" }
  - match: { hits.hits.2.fields.text.0: "other" }
  - match: { hits.hits.2.fields.keyword.0: "other" }

---
"rrf retriever with multiple standard retrievers":

  - do:
      search:
        index: test
        body:
          track_total_hits: true
          fields: [ "text", "keyword" ]
          retriever:
            rrf:
              retrievers: [
                {
                  "standard": {
                    "query": {
                      "term": {
                        "text": "term"
                      }
                    }
                  }
                },
                {
                  "standard": {
                    "query": {
                      "match": {
                        "keyword": "keyword"
                      }
                    }
                  }
                }
              ]
              rank_window_size: 100
              rank_constant: 1
          size: 10

  - match: { hits.total.value : 2 }

  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.0.fields.text.0: "term" }
  - match: { hits.hits.0.fields.keyword.0: "keyword" }

  - match: { hits.hits.1._id: "1" }
  - match: { hits.hits.1.fields.text.0: "term term" }
  - match: { hits.hits.1.fields.keyword.0: "other" }

---
"rrf retriever with multiple standard retrievers and a knn retriever":

  - do:
      search:
        index: test
        body:
          track_total_hits: true
          fields: [ "text", "keyword" ]
          retriever:
            rrf:
              retrievers: [
                {
                  knn: {
                    field: vector,
                      query_vector: [ 0.0 ],
                      k: 3,
                      num_candidates: 3
                  }
                },
                {
                  "standard": {
                    "query": {
                      "term": {
                        "text": "term"
                      }
                    }
                  }
                },
                {
                  "standard": {
                    "query": {
                      "match": {
                        "keyword": "keyword"
                      }
                    }
                  }
                }
              ]
              rank_window_size: 100
              rank_constant: 1
          size: 10

  - match: { hits.total.value : 3 }

  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.0.fields.text.0: "term" }
  - match: { hits.hits.0.fields.keyword.0: "keyword" }

  - match: { hits.hits.1._id: "1" }
  - match: { hits.hits.1.fields.text.0: "term term" }
  - match: { hits.hits.1.fields.keyword.0: "other" }

  - match: { hits.hits.2._id: "2" }
  - match: { hits.hits.2.fields.text.0: "other" }
  - match: { hits.hits.2.fields.keyword.0: "other" }

---
"rrf retriever with multiple standard retrievers and multiple knn retriever":

  - do:
      search:
        size: 1
        index: test
        body:
          track_total_hits: true
          fields: [ "text", "keyword" ]
          retriever:
            rrf:
              retrievers: [
                {
                  knn: {
                    field: vector,
                    query_vector: [ 0.0 ],
                    k: 3,
                    num_candidates: 3
                  }
                },
                {
                  knn: {
                    field: vector,
                    query_vector: [ 1.0 ],
                    k: 3,
                    num_candidates: 3
                  }
                },
                {
                  "standard": {
                    "query": {
                      "term": {
                        "text": "term"
                      }
                    }
                  }
                },
                {
                  "standard": {
                    "query": {
                      "match": {
                        "keyword": "keyword"
                      }
                    }
                  }
                }
              ]
              rank_window_size: 2
              rank_constant: 1

  - match: { hits.total.value : 3 }
  - length: { hits.hits: 1 }

  - match: { hits.hits.0._id: "1" }
  - match: { hits.hits.0.fields.text.0: "term term" }
  - match: { hits.hits.0.fields.keyword.0: "other" }

---
"rrf retriever with multiple standard retrievers and multiple knn retriever and a filter":

  - do:
      search:
        index: test
        body:
          track_total_hits: true
          fields: [ "text", "keyword" ]
          retriever:
            rrf:
              filter: [
                {
                  term: {
                    keyword: "keyword"
                  }
                }
              ]
              retrievers: [
                {
                  knn: {
                    field: vector,
                    query_vector: [ 0.0 ],
                    k: 3,
                    num_candidates: 3
                  }
                },
                {
                  knn: {
                    field: vector,
                    query_vector: [ 1.0 ],
                    k: 3,
                    num_candidates: 3
                  }
                },
                {
                  "standard": {
                    "query": {
                      "term": {
                        "text": "term"
                      }
                    }
                  }
                },
                {
                  "standard": {
                    "query": {
                      "match": {
                        "keyword": "keyword"
                      }
                    }
                  }
                }
              ]

  - match: { hits.total.value : 1 }
  - length: { hits.hits: 1 }

  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.0.fields.text.0: "term" }
  - match: { hits.hits.0.fields.keyword.0: "keyword" }

---
"rrf retriever with nested rrf retriever and pagination":

  - do:
      search:
        index: test
        body:
          track_total_hits: true
          fields: [ "text", "keyword" ]
          retriever:
            rrf:
              retrievers: [
                {
                  "rrf":
                    {
                      "retrievers": [
                        {
                          "knn": {
                            "field": "vector",
                            "query_vector": [ 0.0 ],
                            "k": 3,
                            "num_candidates": 3
                          }
                        },
                        {
                          "standard":
                            {
                              "query":
                                {
                                  "term":
                                    {
                                      "text": "term"
                                    }
                                }
                            }
                        },
                        {
                          "standard":
                            {
                              "query":
                                {
                                  "match":
                                    {
                                      "keyword": "keyword"
                                    }
                                }
                            }
                        }
                      ],
                      "rank_window_size": 100,
                      "rank_constant": 1
                    }
                },
                {
                  "knn": {
                    "field": vector,
                    "query_vector": [ 0.0 ],
                    "k": 2,
                    "num_candidates": 2
                  }
                }
              ]
              "rank_window_size": 10
              "rank_constant": 1
          size: 10
          from: 1

  - match: { hits.total.value : 3 }

  - match: { hits.hits.0._id: "2" }
  - match: { hits.hits.0.fields.text.0: "other" }
  - match: { hits.hits.0.fields.keyword.0: "other" }

  - match: { hits.hits.1._id: "3" }
  - match: { hits.hits.1.fields.text.0: "term" }
  - match: { hits.hits.1.fields.keyword.0: "keyword" }
