---
setup:

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

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

  - do:
      index:
          index:  index_1
          type:   test
          id:     3
          body:   { foo: foo }

  - do:
      index:
          index:  index_2
          type:   test
          id:     1
          body:   { foo: foo }

  - do:
      indices.refresh: {}

---
"Basic multi-search template":

  - do:
      msearch_template:
        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  }


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

  - do:
      msearch_template:
        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"

  - match:  { responses.0.hits.total:     2  }
  - match:  { responses.1.error.root_cause.0.type: json_e_o_f_exception }
  - match:  { responses.1.error.root_cause.0.reason: "/Unexpected.end.of.input/" }
  - match:  { responses.2.hits.total:     1  }
  - match:  { responses.3.error.root_cause.0.type: parsing_exception }
  - match:  { responses.3.error.root_cause.0.reason: "/no.\\[query\\].registered.for.\\[unknown\\]/" }

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

  - do:
      catch: /(.)*action_request_validation_exception(.)*template.is.missing(.)*/
      msearch_template:
        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":

  - 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:     2  }
  - match:  { responses.1.hits.total:     1  }
  - match:  { responses.2.hits.total:     1  }
