setup:
  - requires:
      test_runner_features: close_to

  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            properties:
              text:
                type: text
              integer:
                type: integer
              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"
          integer: 1
          vector: [5]

  - do:
      index:
        index: test
        id: "2"
        body:
          text: "term term"
          integer: 2
          vector: [4]

  - do:
      index:
        index: test
        id: "3"
        body:
          text: "term term term"
          integer: 3
          vector: [3]
  - do:
      index:
        index: test
        id: "4"
        body:
          text: "term term term term"
          integer: 3

  - do:
      index:
        index: test
        id: "5"
        body:
          integer: 1
          vector: [0]

  - do:
      indices.refresh: {}

---
"profile standard and knn query":

  - do:
      search:
        index: test
        body:
          fields: [ "text", "integer" ]
          retriever:
            rrf:
              retrievers: [
                {
                  standard: {
                    query: {
                      term: {
                        text: "term"
                      }
                    }
                  }
                },
                {
                  standard: {
                    query: {
                      knn: {
                        field: "vector",
                        query_vector: [ 3 ],
                        num_candidates: 5
                      }
                    }
                  }
                }
              ]
              rank_window_size: 5
              rank_constant: 1
          size: 3
          profile: true

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

  - not_exists: profile.shards.0.dfs
  - match: { profile.shards.0.searches.0.query.0.type: RankDocsQuery }
  - length: { profile.shards.0.searches.0.query.0.children: 2 }
  - match: { profile.shards.0.searches.0.query.0.children.0.type: TopQuery }
  - match: { profile.shards.0.searches.0.query.0.children.1.type: BooleanQuery }
  - length: { profile.shards.0.searches.0.query.0.children.1.children: 2 }
  - match: { profile.shards.0.searches.0.query.0.children.1.children.0.type: TermQuery }
  - match: { profile.shards.0.searches.0.query.0.children.1.children.1.type: DocAndScoreQuery }

---
"profile standard and knn dfs retrievers":

  - do:
      search:
        index: test
        body:
          fields: [ "text", "integer" ]
          retriever:
            rrf:
              retrievers: [
                {
                  standard: {
                    query: {
                      term: {
                        text: "term"
                      }
                    }
                  }
                },
                {
                  knn: {
                      field: "vector",
                      query_vector: [ 3 ],
                      num_candidates: 5,
                      k: 5
                  }
                }
              ]
              rank_window_size: 5
              rank_constant: 1
          size: 3
          profile: true

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

  - not_exists: profile.shards.0.dfs
  - match: { profile.shards.0.searches.0.query.0.type: RankDocsQuery }
  - length: { profile.shards.0.searches.0.query.0.children: 2 }
  - match: { profile.shards.0.searches.0.query.0.children.0.type: TopQuery }
  - match: { profile.shards.0.searches.0.query.0.children.1.type: BooleanQuery }
  - length: { profile.shards.0.searches.0.query.0.children.1.children: 2 }
  - match: { profile.shards.0.searches.0.query.0.children.1.children.0.type: TermQuery }
  - match: { profile.shards.0.searches.0.query.0.children.1.children.1.type: TopQuery }

---
"using query and dfs knn search":

  - 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]"
      search:
        index: test
        body:
          fields: [ "text", "integer" ]
          query: {
            term: {
              text: {
                value: "term"
              }
            }
          }
          knn: {
            field: "vector",
            query_vector: [ 3 ],
            num_candidates: 5,
            k: 5
          }
          rank: {
            rrf: {
              rank_window_size: 5,
              rank_constant: 1
            }
          }
          size: 3
          profile: true

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

  - not_exists: profile.shards.0.dfs

  - match: { profile.shards.0.searches.0.query.0.type: RankDocsQuery }
  - length: { profile.shards.0.searches.0.query.0.children: 2 }
  - match: { profile.shards.0.searches.0.query.0.children.0.type: TopQuery }
  - match: { profile.shards.0.searches.0.query.0.children.1.type: BooleanQuery }
  - length: { profile.shards.0.searches.0.query.0.children.1.children: 2 }
  - match: { profile.shards.0.searches.0.query.0.children.1.children.0.type: TermQuery }
  - match: { profile.shards.0.searches.0.query.0.children.1.children.1.type: TopQuery }
