---
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:
            product:
              type: "keyword"
            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{ \"product\": \"TV\", \"cost\": 200, \"time\": 1587501233000\
        \ }\n{ \"index\": {} }\n{ \"product\": \"TV\", \"cost\": 400, \"time\": 1587501233000}\n\
        { \"index\": {} }\n{ \"product\": \"TV\", \"cost\": 600, \"time\": 1587501233000}\n\
        { \"index\": {} }\n{ \"product\": \"VCR\", \"cost\": 150, \"time\": 1587501233000\
        \ }\n{ \"index\": {} }\n{ \"product\": \"VCR\", \"cost\": 350, \"time\": 1587501233000\
        \ }\n{ \"index\": {} }\n{ \"product\": \"VCR\", \"cost\": 580, \"time\": 1587501233000\
        \ }\n{ \"index\": {} }\n{ \"product\": \"Laptop\", \"cost\": 100, \"time\"\
        : 1587501233000 }\n{ \"index\": {} }\n{ \"product\": \"Laptop\", \"cost\"\
        : 300, \"time\": 1587501233000 }\n{ \"index\": {} }\n{ \"product\": \"Laptop\"\
        , \"cost\": 500, \"time\": 1587501233000 }\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 correlation bucket agg simple:
- do:
    search:
      index: "store"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"good\": {\n      \"terms\": {\n\
        \        \"field\": \"product\",\n        \"size\": 10\n      },\n      \"\
        aggs\": {\n        \"ranged_cost\": {\n          \"range\": {\n          \
        \  \"field\": \"cost\",\n            \"ranges\": [\n              {\"from\"\
        : 200},\n              {\"from\": 200, \"to\": 300},\n              {\"from\"\
        : 300}\n            ]\n          }\n        },\n        \"bucket_correlation\"\
        : {\n          \"bucket_correlation\": {\n            \"buckets_path\": \"\
        ranged_cost>_count\",\n            \"function\": { \"count_correlation\":\
        \ {\n                \"indicator\": {\n                   \"expectations\"\
        : [3, 4, 2],\n                   \"doc_count\": 9\n                }\n   \
        \           }\n            }\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.good.buckets.0.bucket_correlation.value"
- is_true: "aggregations.good.buckets.1.bucket_correlation.value"
- is_true: "aggregations.good.buckets.2.bucket_correlation.value"
---
Test correlation with missing buckets_path:
- do:
    catch: "/Required \\[buckets_path\\]/"
    search:
      index: "store"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"good\": {\n      \"terms\": {\n\
        \        \"field\": \"product\",\n        \"size\": 10\n      },\n      \"\
        aggs\": {\n        \"ranged_cost\": {\n          \"range\": {\n          \
        \  \"field\": \"cost\",\n            \"ranges\": [\n              {\"from\"\
        : 200},\n              {\"from\": 200, \"to\": 300},\n              {\"from\"\
        : 300}\n            ]\n          }\n        },\n        \"bucket_correlation\"\
        : {\n          \"bucket_correlation\": {\n            \"function\": { \"count_correlation\"\
        : {\n                \"indicator\": {\n                   \"expectations\"\
        : [3, 4, 2],\n                   \"doc_count\": 9\n                }\n   \
        \           }\n            }\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"
---
Test correlation with missing function:
- do:
    catch: "/Required \\[function\\]/"
    search:
      index: "store"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"good\": {\n      \"terms\": {\n\
        \        \"field\": \"product\",\n        \"size\": 10\n      },\n      \"\
        aggs\": {\n        \"ranged_cost\": {\n          \"range\": {\n          \
        \  \"field\": \"cost\",\n            \"ranges\": [\n              {\"from\"\
        : 200},\n              {\"from\": 200, \"to\": 300},\n              {\"from\"\
        : 300}\n            ]\n          }\n        },\n        \"bucket_correlation\"\
        : {\n          \"bucket_correlation\": {\n            \"buckets_path\": \"\
        ranged_cost>_count\"\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"
---
Test correlation with pointing to missing agg:
- do:
    catch: "/No aggregation found for path \\[missing>_count\\]/"
    search:
      index: "store"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"good\": {\n      \"terms\": {\n\
        \        \"field\": \"product\",\n        \"size\": 10\n      },\n      \"\
        aggs\": {\n        \"ranged_cost\": {\n          \"range\": {\n          \
        \  \"field\": \"cost\",\n            \"ranges\": [\n              {\"from\"\
        : 200},\n              {\"from\": 200, \"to\": 300},\n              {\"from\"\
        : 300}\n            ]\n          }\n        },\n        \"bucket_correlation\"\
        : {\n          \"bucket_correlation\": {\n            \"buckets_path\": \"\
        missing>_count\",\n            \"function\": { \"count_correlation\": {\n\
        \                \"indicator\": {\n                  \"expectations\": [3,\
        \ 4, 2],\n                  \"doc_count\": 9\n                }\n        \
        \      }\n            }\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"
---
Test correlation with pointing to mismatched lengths:
- do:
    catch: "/value lengths do not match; indicator.expectations \\[4\\] and number\
      \ of buckets \\[3\\]. Unable to calculate correlation/"
    search:
      index: "store"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"good\": {\n      \"terms\": {\n\
        \        \"field\": \"product\",\n        \"size\": 10\n      },\n      \"\
        aggs\": {\n        \"ranged_cost\": {\n          \"range\": {\n          \
        \  \"field\": \"cost\",\n            \"ranges\": [\n              {\"from\"\
        : 200},\n              {\"from\": 200, \"to\": 300},\n              {\"from\"\
        : 300}\n            ]\n          }\n        },\n        \"bucket_correlation\"\
        : {\n          \"bucket_correlation\": {\n            \"buckets_path\": \"\
        ranged_cost>_count\",\n            \"function\": { \"count_correlation\":\
        \ {\n                \"indicator\": {\n                  \"expectations\"\
        : [3, 4, 2, 10],\n                  \"doc_count\": 9\n                }\n\
        \              }\n            }\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"
- do:
    catch: "/value lengths do not match; indicator.expectations \\[3\\] and number\
      \ of buckets \\[4\\]. Unable to calculate correlation/"
    search:
      index: "store"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"good\": {\n      \"terms\": {\n\
        \        \"field\": \"product\",\n        \"size\": 10\n      },\n      \"\
        aggs\": {\n        \"ranged_cost\": {\n          \"range\": {\n          \
        \  \"field\": \"cost\",\n            \"ranges\": [\n              {\"from\"\
        : 200},\n              {\"from\": 200, \"to\": 300},\n              {\"from\"\
        : 300, \"to\": 400},\n              {\"from\": 400}\n            ]\n     \
        \     }\n        },\n        \"bucket_correlation\": {\n          \"bucket_correlation\"\
        : {\n            \"buckets_path\": \"ranged_cost>_count\",\n            \"\
        function\": { \"count_correlation\": {\n                \"indicator\": {\n\
        \                  \"expectations\": [3, 4, 2],\n                  \"doc_count\"\
        : 9\n                }\n              }\n            }\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"
