AI Hints
Home›Elasticsearch Tutorial›Metrics and Statistics
Elasticsearch · Lesson 71 of 120

stats and Percentiles

The whole picture.

⏱ 2 min read▶ Video at 1:07:17🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

An average can hide a lot. A few huge orders pull it up. Percentiles show the real spread. Half the orders are below the fiftieth percentile, and ninety five percent below the ninety fifth.

  • Averages hide things
  • Percentiles
  • The real spread

stats and percentiles

Stats returns count, min, max, average and sum in one go. Percentiles returns the order totals at fifty, ninety and ninety nine percent. Run it. Half of all orders are under the median, and only one percent are above the last value.

Dev Tools Consoletext
GET orders/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "summary": { "stats": { "field": "total" } },
    "spread":  { "percentiles": { "field": "total", "percents": [50, 90, 99] } }
  }
}
Output
{
  "aggregations": {
    "summary": {
      "count": 2000,
      "min": 20.99,
      "max": 1616.97,
      "avg": 240.3935,
      "sum": 480787.03
    },
    "spread": {
      "values": {
        "50.0": 175.1246,
        "90.0": 527.3891,
        "99.0": 1142.9386
      }
    }
  }
}
Also uses _load
orders

More statistics

Extended stats adds variance and standard deviation. Percentiles are approximate, but very close, and fast. Percentile ranks answer the opposite question. What percent of orders are under one hundred dollars?

keyvalue
statscount, min, max, avg, sum
extended_stats+ std deviation
percentilesvalue at 50%, 90%…
percentile_ranks% under a value

Summary

Stats gives five numbers at once, and percentiles show the spread that averages hide. Next, aggregating only the documents that match a search.

Key takeaways
  • stats
  • percentiles
  • median
Next up: Aggregating a Search — numbers for the matches