---
"Template search request":

  - do:
      index:
        index:  test
        id:     "1"
        body:   { "text": "value1" }
  - do:
      index:
        index:  test
        id:     "2"
        body:   { "text": "value2" }
  - do:
      indices.refresh: {}

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "source" : { "query": { "term": { "text": { "value": "{{template}}" } } } }, "params": { "template": "value1" } }

  - match: { hits.total: 1 }

  - do:
      put_script:
        id: "1"
        body: { "script": { "lang": "mustache", "source": { "query": { "term": { "text": "{{template}}" } } } } }
  - match: { acknowledged: true }

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "id" : "1", "params": { "template": "value1" } }
  - match: { hits.total: 1 }

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "source" : { "query": { "match_{{template}}": {} } }, "params" : { "template" : "all" } }

  - match: { hits.total: 2 }

---
"Test with invalid track_total_hits":

  - do:
      catch:              bad_request
      search_template:
        rest_total_hits_as_int: true
        body: { "source" : { "query": { "match_{{template}}": {} }, "track_total_hits": "{{trackTotalHits}}" }, "params" : { "template" : "all", "trackTotalHits" : 1 } }

  - match: { status: 400 }
  - match: { error.type: illegal_argument_exception }
  - match: { error.reason: "[rest_total_hits_as_int] cannot be used if the tracking of total hits is not accurate, got 1" }

---
"Missing template search request":

  - do:
      catch: missing
      search_template:
        rest_total_hits_as_int: true
        body: { "id" : "unknown", "params": { "template": "value1" } }

---
"Test that parameters can be used":

  - do:
      index:
        index:  test
        id:     "1"
        body:   { "theField": "foo" }

  - do:
      index:
        index:  test
        id:     "2"
        body:   { "theField": "foo 2" }

  - do:
      index:
        index:  test
        id:     "3"
        body:   { "theField": "foo 3" }

  - do:
      index:
        index:  test
        id:     "4"
        body:   { "theField": "foo 4" }

  # we use a different index here since we compare the explain description which contains a doc ID and we can only be sure that it's 0
  # if we are the only doc in the shard.
  - do:
      index:
        index:  otherindex
        id:     "5"
        body:   { "otherField": "foo" }
  - do:
      indices.refresh: {}

  - do:
      put_script:
        id: "template_1"
        body: { "script": { "lang": "mustache", "source": { "query": { "match": { "{{field}}": { "query" : "{{value}}", "operator" : "{{operator}}{{^operator}}or{{/operator}}" } } }, "size": "{{size}}" } } }
  - match: { acknowledged: true }

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "id" : "template_1", "params": { "size": "2", "field": "theField", "value": "foo" } }

  - match: { hits.total: 4 }
  - length: { hits.hits: 2 }

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "id" : "template_1", "params": { "size": "2", "field": "otherField", "value": "foo" } }

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

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "id" : "template_1", "params": { "size": "2", "field": "otherField", "value": "foo" }, "explain" : true }

  - match: { hits.total: 1 }
  - length: { hits.hits: 1 }
  - match: { hits.hits.0._explanation.description: "weight(otherField:foo in 0) [PerFieldSimilarity], result of:" }

  - do:
      search_template:
        rest_total_hits_as_int: true
        body: { "id" : "template_1", "params": { "size": "2", "field": "otherField", "value": "foo" }, "profile" : true }

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

---
"Test with new response format":
  - do:
      index:
        index:  test
        id:     "1"
        body:   {}
        refresh: true

  - do:
      put_script:
        id: "template_1"
        body: { "script": { "lang": "mustache", "source": { "query": { "match_all": {} } } } }

  - match: { acknowledged: true }

  - do:
      search_template:
        body: { "id": "template_1", "params": {} }

  - match: { hits.total.value: 1 }
  - match: { hits.total.relation: eq }

---
"Test explain templated search":
  - requires:
      cluster_features: ["gte_v8.8.0"]
      reason: Bug fix for explain in search template added in 8.8.0

  - do:
      index:
        index:  test
        id:     "1"
        body:   { "text": "value1" }
  - do:
      index:
        index:  test
        id:     "2"
        body:   { "text": "value2" }
  - do:
      indices.refresh: {}

  - do:
      search_template:
        explain: true
        body: { "source" : { "query": { "term": { "text": { "value": "{{template}}" } } } }, "params": { "template": "value1" } }

  - is_true: hits.hits.0._explanation
  # Test consistency with _search API, where query parameter takes priority over body
  - do:
      search_template:
        explain: true
        body: { "explain": false, "source" : { "query": { "term": { "text": { "value": "{{template}}" } } } }, "params": { "template": "value1" } }

  - is_true: hits.hits.0._explanation

  - do:
      search_template:
        explain: false
        body: {"explain": true, "source" : { "query": { "term": { "text": { "value": "{{template}}" } } } }, "params": { "template": "value1" } }

  - is_false: hits.hits.0._explanation
