---
setup:

  - do:
      index:
          index:  index_1
          id:     "1"
          body:   { foo: bar }

  - do:
      index:
          index:  index_1
          id:     "2"
          body:   { foo: baz }

  - do:
      index:
          index:  index_1
          id:     "3"
          body:   { foo: foo }

  - do:
      index:
          index:  index_2
          id:     "1"
          body:   { foo: foo }

  - do:
      indices.refresh: {}

---
"Basic multi-search template":

  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason:  "status field checks were added in 8.3"

  - do:
      msearch_template:
        rest_total_hits_as_int: true
        body:
          - index: index_*
          - source: '{"query": {"match": {"foo": "{{value}}"} } }'
            params:
              value: "foo"
          - index: index_*
          - source: '{"query": {"match": {"{{field}}": "{{value}}"} } }'
            params:
              field: "foo"
              value: "bar"
          - index: _all
          - source: '{"query": {"{{query_type}}": {{query_content}} } }'
            params:
              query_type: "match_all"
              query_content: "{}"
          - index: _all
          - source:
              query:
                match: {foo: "{{text}}"}
              size: 0
            params:
              text: "baz"

  - match:  { responses.0.hits.total:     2  }
  - match:  { responses.1.hits.total:     1  }
  - match:  { responses.2.hits.total:     4  }
  - match:  { responses.3.hits.total:     1  }
  - length: { responses.3.hits.hits:      0  }
  - match:  { responses.0.status: 200 }
  - match:  { responses.1.status: 200 }
  - match:  { responses.2.status: 200 }
  - match:  { responses.3.status: 200 }


---
"Multi-search template with errors":

  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason:  "status field checks were added in 8.3"

  - do:
      msearch_template:
        rest_total_hits_as_int: true
        body:
          # Search 0 is OK
          - index: index_*
          - source: '{"query": {"match": {"foo": "{{value}}"} } }'
            params:
              value: "foo"
          # Search 1 has an unclosed JSON template
          - index: index_*
          - source: '{"query": {"match": {'
            params:
              field: "foo"
              value: "bar"
          # Search 2 is OK
          - index: _all
          - source:
              query:
                match: {foo: "{{text}}"}
            params:
              text: "baz"
          # Search 3 has an unknown query type
          - index: index_*
          - source: '{"query": {"{{query_type}}": {} }' # Unknown query type
            params:
              query_type: "unknown"
          # Search 4 has an unsupported track_total_hits
          - index: index_*
          - source: '{"query": {"match_all": {} }, "track_total_hits": "{{trackTotalHits}}" }'
            params:
              trackTotalHits: 1

  - match:  { responses.0.hits.total:     2  }
  - match:  { responses.1.error.root_cause.0.type: x_content_e_o_f_exception }
  - match:  { responses.1.error.root_cause.0.reason: "/\\[1:22\\].Unexpected.end.of.file/" }
  - match:  { responses.2.hits.total:     1  }
  - match:  { responses.3.error.root_cause.0.type: parsing_exception }
  - match:  { responses.3.error.root_cause.0.reason: "/unknown.query.\\[unknown\\]/" }
  - match:  { responses.4.error.root_cause.0.type: illegal_argument_exception }
  - match:  { responses.4.error.root_cause.0.reason: "[rest_total_hits_as_int] cannot be used if the tracking of total hits is not accurate, got 1" }
  - match:  { responses.0.status: 200 }
  - match:  { responses.1.status: 400 }
  - match:  { responses.2.status: 200 }
  - match:  { responses.3.status: 400 }


---
"Multi-search template with invalid request":

  - do:
      catch: /(.)*action_request_validation_exception(.)*template.is.missing(.)*/
      msearch_template:
        rest_total_hits_as_int: true
        body:
          # Search 0 is OK
          - index: index_*
          - source: '{"query": {"match": {"foo": "{{value}}"} } }'
            params:
              value: "foo"
          # Search 1 has not template
          - index: _all
          - source: ""
          # Search 2 is OK
          - index: index_*
          - source: '{"query": {"match": {"foo": "{{value}}"} } }'
            params:
              value: "bar"

---
"Basic multi-search using stored template":

  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason:  "status field checks were added in 8.3"

  - do:
      put_script:
        id: stored_template_1
        body: { "script": { "lang" : "mustache", "source": { "query": {"match": {"{{field}}": "{{value}}" }}}}}
  - match: { acknowledged: true }

  - do:
      msearch_template:
        rest_total_hits_as_int: true
        body:
          - index: index_*
          - id: stored_template_1
            params:
              field: "foo"
              value: "foo"
          - index: _all
          - id: stored_template_1
            params:
              field: "foo"
              value: "bar"
          - index: index_2
          - id: stored_template_1
            params:
              field: "foo"
              value: "foo"

  - match:  { responses.0.hits.total:     2  }
  - match:  { responses.1.hits.total:     1  }
  - match:  { responses.2.hits.total:     1  }
  - match:  { responses.0.status: 200 }
  - match:  { responses.1.status: 200 }
  - match:  { responses.2.status: 200 }

  - do:
      put_script:
        id: stored_template_1
        body: { "script": { "lang" : "mustache", "source": { "query": {"match": {"{{field}}": "{{value}}" }}}}}
  - match: { acknowledged: true }

---
"Test with rest_total_hits_as_int":

  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason:  "status field checks were added in 8.3"

  - do:
      put_script:
        id: stored_template_1
        body: { "script": { "lang": "mustache", "source": { "query": {"match": {"{{field}}": "{{value}}" }}}}}
  - match: { acknowledged: true }

  - do:
      msearch_template:
        body:
        - index: index_*
        - id: stored_template_1
          params:
            field: "foo"
            value: "foo"
        - index: _all
        - id: stored_template_1
          params:
            field: "foo"
            value: "bar"
        - index: index_2
        - id: stored_template_1
          params:
            field: "foo"
            value: "foo"

  - match:  { responses.0.hits.total.value:     2  }
  - match:  { responses.0.hits.total.relation:  eq }
  - match:  { responses.1.hits.total.value:     1  }
  - match:  { responses.1.hits.total.relation:  eq }
  - match:  { responses.2.hits.total.value:     1  }
  - match:  { responses.1.hits.total.relation:  eq }
  - match:  { responses.0.status: 200 }
  - match:  { responses.1.status: 200 }
  - match:  { responses.2.status: 200 }
