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:

idnamepricequantitydepartment
1Multi-Grain Cereal4.994Packaged Foods
21lb Ground Beef3.9929Meat and Seafood
3Dozen Apples2.4912Produce
4Chocolate Bar1.292Packaged Foods, Checkout
51 Gallon Milk3.2916Dairy
60.5lb Jumbo Shrimp5.2912Meat and Seafood
7Wheat Bread1.295Bakery
8Pepperoni Pizza2.995Frozen
912 Pack Cola5.296Packaged Foods
10Lime Juice0.9920Produce
1112 Pack Cherry Cola5.595Packaged Foods
121 Gallon Soy Milk3.3910Dairy
131 Gallon Vanilla Soy Milk3.499Dairy
141 Gallon Orange Juice3.294Juice

The following code shows how we created the mapping using curl:

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:

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:

{
"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: `js 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

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.