---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    indices.create:
      index: "store"
      body:
        mappings:
          properties:
            cost:
              type: "integer"
            time:
              type: "date"
            kind:
              type: "keyword"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    indices.create:
      index: "empty-store"
      body:
        mappings:
          properties:
            cost:
              type: "integer"
            time:
              type: "date"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    bulk:
      index: "store"
      refresh: true
      body: "{\"index\":{}}\n{\"cost\":200,\"time\":1587501233000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":200,\"time\":1587501243000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":200,\"time\":1587501253000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":250,\"time\":1587501263000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":250,\"time\":1587501273000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":380,\"time\":1587501283000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":450,\"time\":1587501293000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":550,\"time\":1587501303000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501313000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501313000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501323000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501333000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501343000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501353000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501363000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501373000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501383000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501393000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501403000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501413000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501423000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501433000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501443000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501453000,\"kind\":\"changed\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501233000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501243000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501253000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501263000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501273000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501283000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501293000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501303000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501313000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501313000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501323000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501333000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501343000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501353000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501363000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501373000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501383000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501393000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501403000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501413000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501423000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501433000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501443000,\"kind\":\"unchanged\"\
        }\n{\"index\":{}}\n{\"cost\":600,\"time\":1587501453000,\"kind\":\"unchanged\"\
        }\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test change_point agg simple:
- do:
    search:
      index: "store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"date\": {\n      \"date_histogram\": {\n    \
        \    \"field\": \"time\",\n        \"fixed_interval\": \"1s\"\n      },\n\
        \      \"aggs\": {\n        \"min\": {\n          \"min\": {\n           \
        \ \"field\": \"cost\"\n          }\n        }\n      }\n    },\n    \"change_point\"\
        : {\n      \"change_point\": {\n        \"buckets_path\": \"date>min\"\n \
        \     }\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "aggregations.change_point.type.trend_change"
- is_true: "aggregations.change_point.type.trend_change.p_value"
- is_true: "aggregations.change_point.type.trend_change.r_value"
---
Test change_point with missing buckets_path:
- do:
    catch: "/Required \\[buckets_path\\]/"
    search:
      index: "store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"date\": {\n      \"date_histogram\": {\n    \
        \    \"field\": \"time\",\n        \"fixed_interval\": \"1s\"\n      },\n\
        \      \"aggs\": {\n        \"avg\": {\n          \"avg\": {\n           \
        \ \"field\": \"cost\"\n          }\n        }\n      }\n    },\n    \"change_point\"\
        : {\n      \"change_point\": {\n      }\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test change_point with bad buckets_path:
- do:
    catch: "/No aggregation found for path \\[foo\\]/"
    search:
      index: "store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"date\": {\n      \"date_histogram\": {\n    \
        \    \"field\": \"time\",\n        \"fixed_interval\": \"1s\"\n      },\n\
        \      \"aggs\": {\n        \"avg\": {\n          \"avg\": {\n           \
        \ \"field\": \"cost\"\n          }\n        }\n      }\n    },\n    \"change_point\"\
        : {\n      \"change_point\": {\n        \"buckets_path\": \"foo\"\n      }\n\
        \    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test change_point with too few buckets:
- do:
    search:
      index: "empty-store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"date\": {\n      \"date_histogram\": {\n    \
        \    \"field\": \"time\",\n        \"fixed_interval\": \"1s\"\n      },\n\
        \      \"aggs\": {\n        \"min\": {\n          \"min\": {\n           \
        \ \"field\": \"cost\"\n          }\n        }\n      }\n    },\n    \"change_point\"\
        : {\n      \"change_point\": {\n        \"buckets_path\": \"date\"\n     \
        \ }\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "aggregations.change_point.type.indeterminable"
---
Test change_point with terms:
- do:
    search:
      index: "store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"terms\": {\n       \"terms\": {\n         \"\
        field\": \"kind\"\n       },\n       \"aggs\": {\n          \"date\": {\n\
        \            \"date_histogram\": {\n              \"field\": \"time\",\n \
        \             \"fixed_interval\": \"1s\"\n            },\n            \"aggs\"\
        : {\n              \"min\": {\n                \"min\": {\n              \
        \    \"field\": \"cost\"\n                }\n              }\n           \
        \ }\n          },\n          \"change_point\": {\n            \"change_point\"\
        : {\n              \"buckets_path\": \"date>min\"\n            }\n       \
        \   }\n       }\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "aggregations.terms.buckets.0.change_point.type.trend_change"
- is_true: "aggregations.terms.buckets.0.change_point.type.trend_change.p_value"
- is_true: "aggregations.terms.buckets.0.change_point.type.trend_change.r_value"
- is_false: "aggregations.terms.buckets.1.change_point.type.trend_change"
- is_false: "aggregations.terms.buckets.1.change_point.type.trend_change.p_value"
- is_false: "aggregations.terms.buckets.1.change_point.type.trend_change.r_value"
- do:
    search:
      index: "store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"terms\": {\n       \"terms\": {\n         \"\
        field\": \"kind\"\n       },\n       \"aggs\": {\n          \"date\": {\n\
        \            \"date_histogram\": {\n              \"field\": \"time\",\n \
        \             \"fixed_interval\": \"1s\"\n            },\n            \"aggs\"\
        : {\n              \"min\": {\n                \"min\": {\n              \
        \    \"field\": \"cost\"\n                }\n              }\n           \
        \ }\n          },\n          \"change_point\": {\n            \"change_point\"\
        : {\n              \"buckets_path\": \"date>min\"\n            }\n       \
        \   },\n          \"select\": {\n            \"bucket_selector\": {\n    \
        \          \"buckets_path\": {\"p_value\": \"change_point.p_value\"},\n  \
        \            \"script\": \"params.p_value < 0.5\"\n            }\n       \
        \   }\n       }\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.terms.buckets: 1
- is_true: "aggregations.terms.buckets.0.change_point.type.trend_change"
- is_true: "aggregations.terms.buckets.0.change_point.type.trend_change.p_value"
- is_true: "aggregations.terms.buckets.0.change_point.type.trend_change.r_value"
- do:
    search:
      index: "store"
      size: 0
      body: "{\n  \"aggs\": {\n    \"terms\": {\n       \"terms\": {\n         \"\
        field\": \"kind\"\n       },\n       \"aggs\": {\n          \"date\": {\n\
        \            \"date_histogram\": {\n              \"field\": \"time\",\n \
        \             \"fixed_interval\": \"1s\"\n            },\n            \"aggs\"\
        : {\n              \"min\": {\n                \"min\": {\n              \
        \    \"field\": \"cost\"\n                }\n              }\n           \
        \ }\n          },\n          \"change_point\": {\n            \"change_point\"\
        : {\n              \"buckets_path\": \"date>min\"\n            }\n       \
        \   },\n          \"sort\": {\n            \"bucket_sort\": {\n          \
        \    \"sort\": [{\"change_point.p_value\": {\"order\": \"desc\"}}]\n     \
        \       }\n          }\n       }\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.terms.buckets: 2
- is_false: "aggregations.terms.buckets.0.change_point.type.trend_change"
- is_false: "aggregations.terms.buckets.0.change_point.type.trend_change.p_value"
- is_false: "aggregations.terms.buckets.0.change_point.type.trend_change.r_value"
- is_true: "aggregations.terms.buckets.1.change_point.type.trend_change"
- is_true: "aggregations.terms.buckets.1.change_point.type.trend_change.p_value"
- is_true: "aggregations.terms.buckets.1.change_point.type.trend_change.r_value"
