---
"Default index":
  - do:
      indices.create:
          index:  goodbad
          body:
            settings:
                number_of_shards: "1"
            mappings:
                doc:
                    properties:
                        text:
                            type: text
                            fielddata: true
                        class:
                            type: keyword

  - do:
      index:
          index:  goodbad
          type:   doc
          id:     1
          body:   { text: "good", class: "good" }
  - do:
      index:
          index:  goodbad
          type:   doc
          id:     2
          body:   { text: "good", class: "good" }
  - do:
      index:
          index:  goodbad
          type:   doc
          id:     3
          body:   { text: "bad", class: "bad" }
  - do:
      index:
          index:  goodbad
          type:   doc
          id:     4
          body:   { text: "bad", class: "bad" }
  - do:
      index:
          index:  goodbad
          type:   doc
          id:     5
          body:   { text: "good bad", class: "good" }
  - do:
      index:
          index:  goodbad
          type:   doc
          id:     6
          body:   { text: "good bad", class: "bad" }
  - do:
      index:
          index:  goodbad
          type:   doc
          id:     7
          body:   { text: "bad", class: "bad" }



  - do:
      indices.refresh:
        index: [goodbad]

  - do:
      search:
        index: goodbad
        type:  doc

  - match: {hits.total: 7}
  
  - do:
      search:
        index: goodbad
        type:  doc
        body: {"aggs": {"class": {"terms": {"field": "class"},"aggs": {"sig_terms": {"significant_terms": {"field": "text"}}}}}}

  - match: {aggregations.class.buckets.0.sig_terms.buckets.0.key: "bad"}
  - match: {aggregations.class.buckets.1.sig_terms.buckets.0.key: "good"}
  
---
"IP test":
  - do:
      indices.create:
          index:  ip_index
          body:
            mappings:
                doc:
                    properties:
                        ip:
                            type: ip

  - do:
      index:
          index:  ip_index
          type:   doc
          id:     1
          body:   { ip: "::1" }
  - do:
      index:
          index:  ip_index
          type:   doc
          id:     2
          body:   { }

  - do:
      indices.refresh: {}

  - do:
      search:
        body: { "query" : { "exists" : { "field" : "ip" } }, "aggs" : { "ip_terms" : { "significant_terms" : { "field" : "ip", "min_doc_count" : 1 } } } }

  - match: { hits.total: 1 }

  - length: { aggregations.ip_terms.buckets: 1 }

  - match: { aggregations.ip_terms.buckets.0.key: "::1" }

  - is_false: aggregations.ip_terms.buckets.0.key_as_string

  - match: { aggregations.ip_terms.buckets.0.doc_count: 1 }

  - do:
      search:
        body: { "query" : { "exists" : { "field" : "ip" } }, "aggs" : { "ip_terms" : { "significant_terms" : { "field" : "ip", "min_doc_count" : 1, "include" : [ "::1" ] } } } }

  - match: { hits.total: 1 }

  - length: { aggregations.ip_terms.buckets: 1 }

  - match: { aggregations.ip_terms.buckets.0.key: "::1" }
 
  - do:
      search:
        body: { "query" : { "exists" : { "field" : "ip" } }, "aggs" : { "ip_terms" : { "significant_terms" : { "field" : "ip", "min_doc_count" : 1, "exclude" : [ "::1" ] } } } }

  - match: { hits.total: 1 }

  - length: { aggregations.ip_terms.buckets: 0 }
  
  - do:
      catch: request
      search:
        body: { "size" : 0, "aggs" : { "ip_terms" : { "significant_terms" : { "field" : "ip", "exclude" :  "127.*"  } } } }
  
 
