Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Latest commit

 

History

History
94 lines (84 loc) · 2.75 KB

23_extra_metrics.asciidoc

File metadata and controls

94 lines (84 loc) · 2.75 KB

One Final Modification

Just to drive the point home, let’s make one final modification to our example before moving on to new topics. Let’s add two metrics to calculate the min and max price for each make:

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "colors": {
         "terms": {
            "field": "color"
         },
         "aggs": {
            "avg_price": { "avg": { "field": "price" }
            },
            "make" : {
                "terms" : {
                    "field" : "make"
                },
                "aggs" : { (1)
                    "min_price" : { "min": { "field": "price"} }, (2)
                    "max_price" : { "max": { "field": "price"} } (3)
                }
            }
         }
      }
   }
}
  1. We need to add another aggs level for nesting.

  2. Then we include a min metric.

  3. And a max metric.

Which gives us the following output (again, truncated):

{
...
   "aggregations": {
      "colors": {
         "buckets": [
            {
               "key": "red",
               "doc_count": 4,
               "make": {
                  "buckets": [
                     {
                        "key": "honda",
                        "doc_count": 3,
                        "min_price": {
                           "value": 10000 (1)
                        },
                        "max_price": {
                           "value": 20000 (1)
                        }
                     },
                     {
                        "key": "bmw",
                        "doc_count": 1,
                        "min_price": {
                           "value": 80000
                        },
                        "max_price": {
                           "value": 80000
                        }
                     }
                  ]
               },
               "avg_price": {
                  "value": 32500
               }
            },
...
  1. The min and max metrics that we added now appear under each make

With those two buckets, we’ve expanded the information derived from this query to include the following:

  • There are four red cars.

  • The average price of a red car is $32,500.

  • Three of the red cars are made by Honda, and one is a BMW.

  • The cheapest red Honda is $10,000.

  • The most expensive red Honda is $20,000.