How to Find the Min with Aggregations in Elasticsearch
Introduction
No matter what type of data you’re working with in Elasticsearch, you’ll probably need to determine the minimum value of a dataset at some point. Metrics like min and max are often used when compiling statistics about prices, sales, and inventory. Regardless of your intended purpose, finding the min of a field in Elasticsearch is a simple task when you make use of aggregations. In this tutorial, you’ll learn how to find the min with aggregations in Elasticsearch. If you’re already familiar with the concept of aggregation and prefer to dive into the sample code, feel free to jump ahead to Just the Code.
Use the ‘min’ Aggregation
Let’s look at an example of how you can find the min for a field using aggregations. We’ll use a sample index called store
, which represents a small grocery store. Our store
index contains a type called products
which lists all of the products in the store. We’ll keep our dataset simple by including just a handful of products with a small number of fields: id, price, quantity, and department. The JSON shown below can be used to create this dataset:
id | name | price | quantity | department |
---|---|---|---|---|
1 | Multi-Grain Cereal | 4.99 | 4 | Packaged Foods |
2 | 1lb Ground Beef | 3.99 | 29 | Meat and Seafood |
3 | Dozen Apples | 2.49 | 12 | Produce |
4 | Chocolate Bar | 1.29 | 2 | Packaged Foods, Checkout |
5 | 1 Gallon Milk | 3.29 | 16 | Dairy |
6 | 0.5lb Jumbo Shrimp | 5.29 | 12 | Meat and Seafood |
7 | Wheat Bread | 1.29 | 5 | Bakery |
8 | Pepperoni Pizza | 2.99 | 5 | Frozen |
9 | 12 Pack Cola | 5.29 | 6 | Packaged Foods |
10 | Lime Juice | 0.99 | 20 | Produce |
11 | 12 Pack Cherry Cola | 5.59 | 5 | Packaged Foods |
12 | 1 Gallon Soy Milk | 3.39 | 10 | Dairy |
13 | 1 Gallon Vanilla Soy Milk | 3.49 | 9 | Dairy |
14 | 1 Gallon Orange Juice | 3.29 | 4 | Juice |
The following code shows how we created the mapping using curl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/store -d ' { "mappings": {` "products": { "properties" : {` "name": { "type": "text"}, "price": { "type": "double"}, "quantity": { "type": "integer"}, "department": { "type": "keyword"} } } } } ' |
For our example, we’ll look at the store
index and find the product that has the lowest quantity
in stock. Since our sample dataset is small, it’s easy to glance at it and see that “Chocolate Bars” has the lowest quantity
— we’ll use this knowledge later to confirm that our aggregator worked properly. The following code can be used to find the min:
1 2 3 4 5 6 7 8 9 10 11 | curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/store/products/_search?size=0&pretty" -d ' { "aggs": { "min_quantity": { "min": { "field": "quantity" } } } } ' |
Let’s take a closer look at what’s happening in this code. First, we created an aggregator using "aggs"
, and we named it "min_quantity"
. We set the aggregator’s type to "min"
, and we set "field"
to "quantity"
. This tells Elasticsearch that we want to evaluate the "quantity"
field and find the min of it. Note that the URL in our curl
command contains the parameter size=0
. Without this parameter, the query would return information on each individual product, instead of just the aggregate information we want. We also specified pretty
in our URL; this tells Elasticsearch to return our results in a readable, “pretty-printed” format with proper indentation.
Let’s see what the results have to say:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | { "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 14, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "min_quantity" : { "value" : 2.0 } } } |
We can see that the min_quantity
has a value of two, as we expected from examining our dataset earlier.
Finding the min is just one possible use of aggregations in Elasticsearch. You can also use aggregation to calculate max, average, weighted average, and much more. For more information on aggregation in Elasticsearch, see their documentation.
Conclusion
There are many situations where you may want to find the min for a field in Elasticsearch, so it’s important to know how to accomplish the task correctly. This tutorial outlined an easy way to use aggregration to find the min. With these step-by-step instructions, you’ll be able to add this simple computation to your search applications and get the information you need.
Just the Code
If you’re already comfortable with the concept of aggregation, here’s all the code we used to demonstrate how to find the min with aggregations in Elasticsearch:
1 2 3 4 5 6 7 8 9 10 11 | curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/store/products/_search?size=0&pretty" -d ' { "aggs": { "min_quantity": { "min": { "field": "quantity" } } } } ' |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started