# How to compute the average number of indexed documents/sec from an ES query?

**URL:** https://discuss.elastic.co/t/how-to-compute-the-average-number-of-indexed-documents-sec-from-an-es-query/140301
**Category:** Elasticsearch
**Created:** [July 17, 2018, 9:48am UTC](https://discuss.elastic.co/t/how-to-compute-the-average-number-of-indexed-documents-sec-from-an-es-query/140301 "2018-07-17T09:48:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![anon24174512](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@anon24174512](https://discuss.elastic.co/u/anon24174512)
#### Post date: [July 17, 2018, 9:48am UTC](https://discuss.elastic.co/t/how-to-compute-the-average-number-of-indexed-documents-sec-from-an-es-query/140301/1 "2018-07-17T09:48:38Z")

</div>

Hello,

I would like to construct a query to get the @timestamp of the first and last indexed documents and from that to compute the average number of documents indexed per second in this interval.

I know I can use \_stats to compute this metric but I don't get the same number of indexed documents with \_stats than with \_cat/count. The former being the correct value.

How could I correctly compute this value ?

Thanks.

---

<div class="post-metadata">

### Author: ![anon24174512](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@anon24174512](https://discuss.elastic.co/u/anon24174512)
#### Post date: [July 17, 2018, 1:00pm UTC](https://discuss.elastic.co/t/how-to-compute-the-average-number-of-indexed-documents-sec-from-an-es-query/140301/2 "2018-07-17T13:00:59Z")

</div>

[Edit] After some testings this seems to not be reliable. So use with caution.

I am answering myself as in fact it was very simple... Here a bash script to compute the average number of documents indexed :

```
es_url="$1"
index="$2"
t1=$(curl -sX POST "${es_url}/${index}/_search" -H 'Content-Type: application/json' -d'
{
    "from" : 0, "size" : 1,
    "query": {
        "match_all": {}
    },
   "sort" : [
      {"@timestamp" : {"order" : "asc", "mode" : "min"}}
   ]
}
' | jq '.hits.hits[0].sort[0]')

t2=$(curl -sX POST "${es_url}/${index}/_search" -H 'Content-Type: application/json' -d'
{
    "from" : 0, "size" : 1,
    "query": {
        "match_all": {}
    },
   "sort" : [
      {"@timestamp" : {"order" : "desc", "mode" : "max"}}
   ]
}
' | jq '.hits.hits[0].sort[0]')

count=$(curl -sX GET "${es_url}/_cat/count/${index}" | cut -d ' ' -f 3)
time=$(bc <<< "$t2 - $t1")
avg=$(echo "$count / $time * 1000" | bc)

echo $avg

```

If anyone has a simpler or more efficient solution...

---

<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: [August 14, 2018, 1:02pm UTC](https://discuss.elastic.co/t/how-to-compute-the-average-number-of-indexed-documents-sec-from-an-es-query/140301/3 "2018-08-14T13:02:34Z")

</div>

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