# Calculating indexing rate programmatically

**URL:** https://discuss.elastic.co/t/calculating-indexing-rate-programmatically/261701
**Category:** Elasticsearch
**Tags:** elastic-stack-monitoring
**Created:** [January 20, 2021, 8:26pm UTC](https://discuss.elastic.co/t/calculating-indexing-rate-programmatically/261701 "2021-01-20T20:26:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sandeepkanabar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeepkanabar/32/79399_2.png) [@sandeepkanabar](https://discuss.elastic.co/u/sandeepkanabar)
#### Post date: [January 20, 2021, 8:26pm UTC](https://discuss.elastic.co/t/calculating-indexing-rate-programmatically/261701/1 "2021-01-20T20:26:43Z")

</div>

The `indices` tab in Kibana Monitoring UI shows the indexing rate:

 ![Screenshot 2021-01-20 at 11.56.03 AM](https://us1.discourse-cdn.com/elastic/original/3X/1/b/1b43e7baa34a70a313289679e04f3896d621ca16.png)

Can anyone guide me on how can I get that programatically using API? Based on [this thread](https://discuss.elastic.co/t/api-to-get-indexing-rate/144159/2) as well as [this](https://discuss.elastic.co/t/elasticsearch-indexing-rate-api/209312/2), it seems I can do the following:

```
GET /.monitoring-es-6-2021.01.20/_search?sort=timestamp:desc&filter_path=**.primaries.indexing&size=2
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "index_stats.index": "my_index"
          }
        }
      ]
    }
  }
}

```

This will output something like:

```
{
  "hits" : {
   ...
              "indexing" : {
                "index_total" : 54527554,
                "index_time_in_millis" : 6592199,
                ...
      },
      {
       ...
              "indexing" : {
                "index_total" : 54315000,
                "index_time_in_millis" : 6564685,
                ...
}

```

I suppose I can calculate the indexing rate as `(index_total_1 - index_total_2) / (index_time_in_seconds_1-index_time_in_seconds_2)` i.e `( 54527554-54315000)/(6592.199-6564.685)` = `7725.30/s`

Is this the correct way or a better / shorter way exists?

Thanks.

---

<div class="post-metadata">

### Author: ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)
#### Post date: [January 21, 2021, 7:14pm UTC](https://discuss.elastic.co/t/calculating-indexing-rate-programmatically/261701/2 "2021-01-21T19:14:45Z")

</div>

Yup, this is basically all we do in the UI as well.

We look at the first document in the timespan and look at the last document in the timespan and calculate the rate based on those values in the same fashion as you described

---

<div class="post-metadata">

### Author: ![sandeepkanabar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeepkanabar/32/79399_2.png) [@sandeepkanabar](https://discuss.elastic.co/u/sandeepkanabar)
#### Post date: [February 9, 2021, 2:59am UTC](https://discuss.elastic.co/t/calculating-indexing-rate-programmatically/261701/3 "2021-02-09T02:59:59Z")

</div>

Thank you Chris. That helps. Btw,

> [@chrisronline](#):
>
> look at the last document in the timespan

How would do that? Mind sharing? I mean how do you get the `last` document in the timespan? You use a specific `size`?

---

<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: [March 9, 2021, 3:00am UTC](https://discuss.elastic.co/t/calculating-indexing-rate-programmatically/261701/4 "2021-03-09T03:00:56Z")

</div>

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