# Elastic Search - How to make stats bucket over calculated bucket using bucket script and bucket path

**URL:** <https://discuss.elastic.co/t/elastic-search-how-to-make-stats-bucket-over-calculated-bucket-using-bucket-script-and-bucket-path/368791>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 14, 2024, 2:53pm UTC](https://discuss.elastic.co/t/elastic-search-how-to-make-stats-bucket-over-calculated-bucket-using-bucket-script-and-bucket-path/368791 "2024-10-14T14:53:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitor.rubio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vitor.rubio/32/138350_2.png) [@vitor.rubio](https://discuss.elastic.co/u/vitor.rubio)\
**Post date:** [October 14, 2024, 2:53pm UTC](https://discuss.elastic.co/t/elastic-search-how-to-make-stats-bucket-over-calculated-bucket-using-bucket-script-and-bucket-path/368791/1 "2024-10-14T14:53:21Z")

</div>

Lets start with a simple query in an Elastic Search netflow database. I need to get the sum of network bytes aggregated by network.direction, so I do:

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-30m",
              "lte": "now"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "table": {
      "terms": {
        "field": "network.direction",
        "order": {
          "agg_query_order": "desc"
        },
        "size": 10
      },
      "aggs": {
        "sum_network_bytes": {
          "sum": {
            "field": "network.bytes"
          }
        },
				
        "agg_query_order": {
          "sum": {
            "field": "network.bytes"
          }
        }				
				
      }
    }
  },
  "size": 0
}

```

Here I have an aggregation called sum\_network\_bytes to get sum "network.bytes" per "network.direction" and another aggregation called agg\_query\_order just to order the results.

Now i need to add a stats for "network.bytes" by "network.direction", and I do this adding the code below:

```auto
		"stats_network_bytes": {
          "stats": {
            "field": "network.bytes"
          }
        }

```

resulting in:

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-30m",
              "lte": "now"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "table": {
      "terms": {
        "field": "network.direction",
        "order": {
          "agg_query_order": "desc"
        },
        "size": 10
      },
      "aggs": {
        "sum_network_bytes": {
          "sum": {
            "field": "network.bytes"
          }
        },
				
        "agg_query_order": {
          "sum": {
            "field": "network.bytes"
          }
        },
		
		"stats_network_bytes": {
          "stats": {
            "field": "network.bytes"
          }
        }					
      }
    }
  },
  "size": 0
}

```

This added another aggregation called stats\_network\_bytes with "network.bytes" stats.

My sample rate is 1:1000 (one netflow of each 1000) and I need to multiply the results by 1000 to get accurate results. I need also to multiply by 8 to get results in bits instead of bytes, so I will add the code below

```auto
        "sum_network_bits": {
          "bucket_script": {
            "buckets_path": {
              "total_value": "sum_network_bytes"
            },
            "script": "params.total_value * 1000 * 8"
          }
        }

```

And by whole code becomes:

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-30m",
              "lte": "now"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "table": {
      "terms": {
        "field": "network.direction",
        "order": {
          "agg_query_order": "desc"
        },
        "size": 10
      },
      "aggs": {
        "sum_network_bytes": {
          "sum": {
            "field": "network.bytes"
          }
        },
				
        "agg_query_order": {
          "sum": {
            "field": "network.bytes"
          }
        },
		
				"stats_network_bytes": {
          "stats": {
            "field": "network.bytes"
          }
        },
				
        "sum_network_bits": {
          "bucket_script": {
            "buckets_path": {
              "total_value": "sum_network_bytes"
            },
            "script": "params.total_value * 1000 * 8"
          }
        }				

      }
    }
  },
  "size": 0
}

```

Now I have a new bucket called sum\_network\_bits with the total bits per "network.direction".

How can I add a stats bucket for sum\_network\_bits metric ?

I need also to get the speed (in bits per second) but without get date\_histogram aggregation.

---

<div class="post-metadata">

**Author:** ![nfeekery](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nfeekery/32/117349_2.png) [@nfeekery](https://discuss.elastic.co/u/nfeekery)\
**Post date:** [October 15, 2024, 3:11pm UTC](https://discuss.elastic.co/t/elastic-search-how-to-make-stats-bucket-over-calculated-bucket-using-bucket-script-and-bucket-path/368791/2 "2024-10-15T15:11:11Z")

</div>

From #Elastic Search to #Elasticsearch

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 12, 2024, 3:11pm UTC](https://discuss.elastic.co/t/elastic-search-how-to-make-stats-bucket-over-calculated-bucket-using-bucket-script-and-bucket-path/368791/3 "2024-11-12T15:11:15Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
