---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    indices.create:
      index: "aggregate_metric_test"
      body:
        mappings:
          properties:
            metric:
              type: "aggregate_metric_double"
              metrics:
              - "sum"
              - "value_count"
              default_metric: "value_count"
    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:
    bulk:
      index: "aggregate_metric_test"
      refresh: true
      body:
      - "{\"index\": {}}"
      - "{\"metric\": {\"sum\": 10000.45, \"value_count\": 100} }"
      - "{\"index\": {}}"
      - "{\"metric\": {\"sum\": 60, \"value_count\": 20} }"
      - "{\"index\": {}}"
      - "{\"metric\": {\"sum\": -400.50, \"value_count\": 1000} }"
      - "{\"index\": {}}"
      - "{\"metric\": {\"sum\": 1, \"value_count\": 20} }"
      - "{\"index\": {}}"
      - "{\"metric\": {\"sum\": -100, \"value_count\": 40} }"
    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 sum agg:
- requires:
    cluster_features:
    - "gte_v7.11.0"
    reason: "Aggregate metric fields have been added in 7.11"
- do:
    search:
      index: "aggregate_metric_test"
      size: 0
      body:
        aggs:
          sum_agg:
            sum:
              field: "metric"
    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"
- match:
    hits.total.value: 5
- match:
    aggregations.sum_agg.value: 9560.95
---
Test sum agg with query:
- skip:
    awaits_fix: "Default metric cannot be defined, so the query is empty"
- requires:
    cluster_features:
    - "gte_v7.11.0"
    reason: "Aggregate metric fields have been added in 7.11"
- do:
    search:
      index: "aggregate_metric_test"
      size: 0
      body:
        query:
          term:
            metric:
              value: 20
        aggs:
          sum_agg:
            sum:
              field: "metric"
    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"
- match:
    hits.total.value: 2
- match:
    aggregations.sum_agg.value: 61
