AI Hints
Home›Elasticsearch Tutorial›Facets and Dashboards
Elasticsearch · Lesson 81 of 120

Practice: A Sales Dashboard

One request, every number.

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

The Trailhead team wants a dashboard. Total revenue, revenue for the top countries, and orders per quarter. One request can return all of it.

  • Total revenue
  • Top countries
  • Per quarter

The dashboard request

Three aggregations sit side by side. Revenue is a sum. Countries is terms, with a sum inside. Months is a date histogram, by quarter. Run it. Every number for the dashboard, in one fast request.

Dev Tools Consoletext
GET orders/_search?filter_path=aggregations.revenue,aggregations.countries.buckets.key,aggregations.countries.buckets.rev,aggregations.months.buckets.doc_count
{
  "size": 0,
  "aggs": {
    "revenue":   { "sum": { "field": "total" } },
    "countries": { "terms": { "field": "country", "size": 3 },
                   "aggs": { "rev": { "sum": { "field": "total" } } } },
    "months":    { "date_histogram": { "field": "date", "calendar_interval": "quarter" } }
  }
}
Output
{
  "aggregations": {
    "revenue": {"value": 480787.03},
    "countries": {
      "buckets": [
        {"key": "US", "rev": {"value": 252452.77}},
        {"key": "CA", "rev": {"value": 102309.1}},
        {"key": "AU", "rev": {"value": 46883.16}}
      ]
    },
    "months": {
      "buckets": [
        {"doc_count": 578},
        {"doc_count": 637},
        {"doc_count": 622},
        {"doc_count": 163}
      ]
    }
  }
}
Also uses _load
orders

Dashboard tips

Combine all the panels into one request when you can. Use size zero. Filter by date range in the query, for the period being viewed. And Kibana can build these dashboards for you, with no code.

keyvalue
One requestall panels
size: 0no hits
Date rangein the query
Kibanadashboards, no code

Summary

One aggregation request can feed a whole dashboard. Next, making search results look great with highlighting.

Key takeaways
  • Many aggs
  • One request
  • Dashboard
Next up: Highlighting — show why it matched