setup:
  - skip:
      features:
        - close_to
        - contains

  - requires:
      capabilities:
        - method: POST
          path: /_search
          capabilities: [ transform_rank_rrf_to_retriever ]
      test_runner_features: capabilities
      reason: "Support for transforming deprecated rank_rrf queries to the corresponding rrf retriever is required"

  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            properties:
              number_val:
                type: keyword
              char_val:
                type: keyword

  - do:
      index:
        index: test
        id: 1
        body:
          number_val: "1"
          char_val: "A"

  - do:
      index:
        index: test
        id: 2
        body:
          number_val: "2"
          char_val: "B"

  - do:
      index:
        index: test
        id: 3
        body:
          number_val: "3"
          char_val: "C"

  - do:
      index:
        index: test
        id: 4
        body:
          number_val: "4"
          char_val: "D"

  - do:
      index:
        index: test
        id: 5
        body:
          number_val: "5"
          char_val: "E"

  - do:
      indices.refresh: {}

---
"Standard pagination within rank_window_size":
  # this test retrieves the same results from two queries, and applies a simple pagination skipping the first result
  - requires:
      cluster_features: [ "gte_v8.16.0" ]
      reason: "deprecation added in 8.16"
      test_runner_features: warnings

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "2",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "3",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "4",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 10
              rank_constant: 10
          from : 1
          size : 10

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 3 }
  - match: { hits.hits.0._id: "2" }
  - match: { hits.hits.1._id: "3" }
  - match: { hits.hits.2._id: "4" }

---
"Standard pagination outside rank_window_size":
  # in this example, from starts *after* rank_window_size so, we expect 0 results to be returned
  - requires:
      cluster_features: [ "gte_v8.16.0" ]
      reason: "deprecation added in 8.16"
      test_runner_features: warnings

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "2",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "3",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "4",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 2
              rank_constant: 10
          from : 10
          size : 2

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 0 }

---
"Standard pagination partially outside rank_window_size":
  # in this example we have that from starts *within* rank_window_size, but "from + size" goes over
  - requires:
      cluster_features: [ "gte_v8.16.0" ]
      reason: "deprecation added in 8.16"
      test_runner_features: warnings

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "2",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "3",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "4",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 3
              rank_constant: 10
          from : 2
          size : 2

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 1 }
  - match: { hits.hits.0._id: "3" }


---
"Pagination within interleaved results":
  # perform two searches with different "from" parameter, ensuring that results are consistent
  # rank_window_size covers the entire result set for both queries, so pagination should be consistent
  # queryA has a result set of [1, 2, 3, 4] and
  # queryB has a result set of [4, 3, 1, 2]
  # so for rank_constant=10, the expected order is [1, 4, 3, 2]
  - requires:
      cluster_features: ["gte_v8.16.0"]
      reason: "deprecation added in 8.16"
      test_runner_features: warnings

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "2",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "3",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "4",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 10
              rank_constant: 10
          from : 0
          size : 2

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 2 }
  - match: { hits.hits.0._id: "1" }
  - match: { hits.hits.1._id: "4" }

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1, 2, 3, 4]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "2",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "3",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "4",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 10
              rank_constant: 10
          from : 2
          size : 2

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 2 }
  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.1._id: "2" }

---
"Pagination within interleaved results, different result set sizes, rank_window_size covering all results":
  # perform multiple searches with different "from" parameter, ensuring that results are consistent
  # rank_window_size covers the entire result set for both queries, so pagination should be consistent
  # queryA has a result set of [1] and
  # queryB has a result set of [4, 3, 1, 2]
  # so for rank_constant=10, the expected order is [1, 5, 4, 3, 2]
  - requires:
      cluster_features: ["gte_v8.16.0"]
      reason: "deprecation added in 8.16"
      test_runner_features: warnings
  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1]
              "query": {
                term: {
                  number_val: {
                    value: "1",
                    boost: 9.0
                  }
                }
              }
            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 10
              rank_constant: 10
          from : 0
          size : 2

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 2 }
  - match: { hits.hits.0._id: "1" }
    # score for doc 1 is (1/12 + 1/13)
  - close_to: {hits.hits.0._score: {value:  0.1678, error: 0.001}}
  - match: { hits.hits.1._id: "4" }
    # score for doc 4 is (1/11)
  - close_to: {hits.hits.1._score: {value:  0.0909, error: 0.001}}

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1]
              "query": {
                term: {
                  number_val: {
                    value: "1",
                    boost: 9.0
                  }
                }
              }
            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 10
              rank_constant: 10
          from : 2
          size : 2

  - match: { hits.total.value : 4 }
  - length: { hits.hits : 2 }
  - match: { hits.hits.0._id: "3" }
    # score for doc 3 is (1/12)
  - close_to: {hits.hits.0._score: {value:  0.0833, error: 0.001}}
  - match: { hits.hits.1._id: "2" }
    # score for doc 2 is (1/14)
  - close_to: {hits.hits.1._score: {value:  0.0714, error: 0.001}}

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [1]
              "query": {
                term: {
                  number_val: {
                    value: "1",
                    boost: 9.0
                  }
                }
              }
            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
          rank:
            rrf:
              rank_window_size: 10
              rank_constant: 10
          from: 4
          size: 2

  - match: { hits.total.value: 4 }
  - length: { hits.hits: 0 }

---
"Pagination within interleaved results, different result set sizes, rank_window_size not covering all results":
  # perform multiple searches with different "from" parameter, ensuring that results are consistent
  # rank_window_size does not cover the entire result set for both queries, so the results should be different
  # from the test above. More specifically, we'd get to collect 2 results from each query, so we'd have:
  # queryA has a result set of [5, 1] and
  # queryB has a result set of [4, 3]
  # so for rank_constant=10, the expected order is [5, 4, 1, 3],
  # and the rank_window_size-sized result set that we'd paginate over is [5, 4]
  - requires:
      cluster_features: ["gte_v8.16.0"]
      reason: "deprecation added in 8.16"
      test_runner_features: warnings

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]
      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [5, 1]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "5",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 9.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 2
              rank_constant: 10
          from : 0
          size : 2

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

  - contains: { hits.hits: { _id: "4" } }
  - contains: { hits.hits: { _id: "5" } }

    # both docs have the same score (1/11)
  - close_to: {hits.hits.0._score: {value:  0.0909, error: 0.001}}
  - close_to: {hits.hits.1._score: {value:  0.0909, error: 0.001}}

  - do:
      warnings:
        - "Deprecated field [rank] used, replaced by [retriever]"
        - Deprecated field [sub_searches] used, replaced by [retriever]

      search:
        index: test
        body:
          track_total_hits: true
          sub_searches: [
            {
              # this should clause would generate the result set [5, 1]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        number_val: {
                          value: "5",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        number_val: {
                          value: "1",
                          boost: 9.0
                        }
                      }
                    }
                  ]
                }
              }

            },
            {
              # this should clause would generate the result set [4, 3, 1, 2]
              "query": {
                bool: {
                  should: [
                    {
                      term: {
                        char_val: {
                          value: "D",
                          boost: 10.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "C",
                          boost: 9.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "A",
                          boost: 8.0
                        }
                      }
                    },
                    {
                      term: {
                        char_val: {
                          value: "B",
                          boost: 7.0
                        }
                      }
                    }
                  ]
                }
              }

            }
          ]
          rank:
            rrf:
              rank_window_size: 2
              rank_constant: 10
          from : 2
          size : 2

  - match: { hits.total.value : 5 }
  - length: { hits.hits : 0 }
