setup:
  - do:
      indices.create:
          index: index_1
          body:
            settings:
              number_of_shards: 1
            mappings:
              doc:
                properties:
                  created_at:
                     type: date
                     format: "yyyy-MM-dd"
  - do:
      indices.create:
          index: index_2
          body:
            settings:
              number_of_shards: 1
            mappings:
              doc:
                properties:
                  created_at:
                     type: date
                     format: "yyyy-MM-dd"
  - do:
      indices.create:
          index: index_3
          body:
            settings:
              number_of_shards: 1
            mappings:
              doc:
                properties:
                  created_at:
                     type: date
                     format: "yyyy-MM-dd"


---
"pre_filter_shard_size with invalid parameter":
  - skip:
      version: " - 5.5.99"
      reason:  this was added in 5.6.0
  - do:
      catch: /preFilterShardSize must be >= 1/
      search:
        index: test_1
        pre_filter_shard_size: 0

---
"pre_filter_shard_size with shards that have no hit":
  - skip:
      version: " - 5.5.99"
      reason:  this was added in 5.6.0
  - do:
      index:
        index: index_1
        type: doc
        id: 1
        body: { "created_at": "2016-01-01"}
  - do:
      index:
        index: index_2
        type: doc
        id: 2
        body: { "created_at": "2017-01-01" }

  - do:
      index:
        index: index_3
        type: doc
        id: 3
        body: { "created_at": "2018-01-01" }
  - do:
      indices.refresh: {}


  - do:
      search:
        body: { "size" : 0, "query" : { "range" : { "created_at" : { "gte" : "2016-02-01", "lt": "2018-02-01"} } } }

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  - match: { _shards.skipped: 0 }
  - match: { _shards.failed: 0 }
  - match: { hits.total: 2 }

  # this is the case where we have an empty body and don't skip anything since it's match_all
  - do:
      search:
        pre_filter_shard_size: 1

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  - match: { _shards.skipped: 0 }
  - match: { _shards.failed: 0 }
  - match: { hits.total: 3 }

  # this is a case where we can actually skip due to rewrite
  - do:
      search:
        pre_filter_shard_size: 1
        body: { "size" : 0, "query" : { "range" : { "created_at" : { "gte" : "2016-02-01", "lt": "2018-02-01"} } } }

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  - match: { _shards.skipped : 1}
  - match: { _shards.failed: 0 }
  - match: { hits.total: 2 }

  # this case we skip all except of one since we need a real result
  - do:
      search:
        pre_filter_shard_size: 1
        body: { "size" : 0, "query" : { "range" : { "created_at" : { "gte" : "2019-02-01", "lt": "2020-02-01"} } } }

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  # skip 2 and execute one to fetch the actual empty result  
  - match: { _shards.skipped : 2} 
  - match: { _shards.failed: 0 }
  - match: { hits.total: 0 }

  - do:
      search:
        pre_filter_shard_size: 1
        body: {"size" : 0, "query" : { "range" : { "created_at" : { "gte" : "2016-02-01", "lt": "2018-02-01"} } }, "aggs" : { "some_agg"  : { "global" : {} }}}

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  - match: { _shards.skipped : 0 }
  - match: { _shards.failed: 0 }
  - match: { hits.total: 2 }
  - match: { aggregations.some_agg.doc_count: 3 }


  - do:
      search:
        pre_filter_shard_size: 1
        body: { "size" : 0, "query" : { "range" : { "created_at" : { "gte" : "2016-02-01", "lt": "2018-02-01"}}}, "aggs" : { "idx_terms" : { "terms" : { "field" : "_index", "min_doc_count" : 0 } } } }

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  - match: { _shards.skipped : 0 }
  - match: { _shards.failed: 0 }
  - match: { hits.total: 2 }
  - length: { aggregations.idx_terms.buckets: 3 }

  - do:
      search:
        pre_filter_shard_size: 1
        body: { "size" : 0, "query" : { "range" : { "created_at" : { "gte" : "2016-02-01", "lt": "2018-02-01"}}}, "aggs" : { "idx_terms" :  { "terms" : { "field" : "_index" } } } }

  - match: { _shards.total: 3 }
  - match: { _shards.successful: 3 }
  - match: { _shards.skipped : 1 }
  - match: { _shards.failed: 0 }
  - match: { hits.total: 2 }
  - length: { aggregations.idx_terms.buckets: 2 }




