How to Find the Max with Aggregations in Elasticsearch using NodeJS
Introduction
Aggregation is a very powerful functionality in Elasticsearch. In this article we’ll be showing you how to use aggregations with NodeJS to find the maximum value for a field in a dataset. Aggregations can do many things like histograms, sums, or averages but finding the max and min are a good way to get your feet wet with aggregations. We’ll show you how step-by-step. But if you’d just rather see the example code, click here to jump to Just the Code.
Prerequisites
Before we show you how to find the maximum value using Elasticsearch, it’s important to make sure a few prerequisites are in place. There are only a few of system requirements for this task:
NodeJS needs to be installed
Elasticsearch also needs to be installed and running.
In our example, we have Elasticsearch installed locally using the default port of 9200. If your Elasticsearch installation is running on a different server, you’ll need to modify your javascript syntax accordingly.
The elasticsearch npm module installed.
* A simple npm install elasticsearch
should work in most cases.
Use the max
Aggregation
Here is our 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 |
And here is the json we used to make the mapping:
1 2 3 4 5 6 7 8 9 10 11 12 | { "mappings": {` "products": { "properties" : {` "name": { "type": "text"}, "price": { "type": "double"}, "quantity": { "type": "integer"}, "department": { "type": "keyword"} } } } } |
To demonstrate how to find the maximum, we will take our store
index and find the product with the highest price
. If you look at our data, the highest price is this product ($5.59):
1 | 11| 12 Pack Cherry Cola | 5.59 | 5 | Packaged Foods |
Let’s look at the code of how we’d determine the highest price of a product and then we’ll explain it:
File index.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); /* Aggregate Max */ client.search({ index: 'store', type: 'products', body: { aggs: { max_price: { max: { field: "price" } } } } }).then(function(resp) { console.log("Successful query! Here is the response:", resp); }, function(err) { console.trace(err.message); }); |
There’s a few steps to dissect so let’s dive in step-by-step:
First we required the elasticsearch library because it gives us the library of functions that make it easy to access Elasticsearch.
Next we created a variable var client
which creates and stores our connection to Elasticsearch. From this point on we’ll use this client to do all our interactions with Elasticsearch.
We then use the search function on client
to create a query with an aggregator.
Of course we specify the index and type to perform the search on. The important part comes in the body where we define the aggregator by using the aggs
keyword. We gave our aggregator a name max_price
that can be anything but choose something that makes sense. We specify the type of aggregator as max
.
* Lastly we specify what field the aggregator should evaluate, price
.
Now let’s see the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $ node index.js Successful query! Here is the response: { took: 76, timed_out: false, _shards: { total: 5, successful: 5, skipped: 0, failed: 0 }, hits: { total: 13, max_score: 1, hits: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ] }, aggregations: { max_price: { value: 5.99 } } } |
As you can see our max_price
returned a value of $5.59 just as we expected.
This is just as easily done for finding the minimum by replacing the max
with min
.
Conclusion
In this tutorial we demonstrated how to use Elasticsearch aggregations to get the minimum or maximum value for a field in a given dataset. Remember that these are the most basic of aggregators and the to explore the myriad of other options and combinations available. Consult the documentation for more information on aggregators and specific syntax. Their documentation is full of great examples.
Just the Code
If you’re already comfortable with NodeJS and aggregations here’s all the code we used to demonstrate how to find the max for a field in our dataset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); /* Aggregate Max */ client.search({ index: 'store', type: 'products', body: { aggs: { max_price: { max: { field: "price" } } } } }).then(function(resp) { console.log("Successful query! Here is the response:", resp); }, function(err) { console.trace(err.message); }); |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started