How to Count the Number of Documents in an Elasticsearch Index using NodeJS
Introduction
Getting the total number of documents in an Elasticsearch index is one of, if not the most common statistic used in Elasticsearch. You might use this kind of stat to figure out how many users there are for example. This is a very simple but very common task that we wanted to cover in case you’re just getting started with Elasticsearch. What follows is an easy to follow step-by-step tutorial of how to get the total count of documents in an Elasticsearch index using Javascript and NodeJS.
Prerequisites
Before we show you how to count the number of documents with Elasticsearch in Javascript, 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
The elasticsearch npm module installed.
A simple npm install elasticsearch
should work in most cases.
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.
Introduce our Example Data
Let’s look at an example that uses an index called store
, which represents a small grocery store. This store
index contains a type called products
which lists the store’s products. To keep things simple, our example dataset will only contain a handful of products with just the following fields: id, price, quantity, and department. The code below shows the JSON used to create the 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 |
Here is the json we used to define the mapping if our index:
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"} } } } } |
Now we can see that we have 14 items in our dataset. How would we verify that? Getting a count would be a good way to verify. Let’s take a look at the code and then we’ll dissect it afterwards.
File app.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); client.count({index: 'store',type: 'products'}) .then(function(resp) { console.log("Successful query!"); console.log(JSON.stringify(resp, null, 4)); }, function(err) { console.trace(err.message); }); |
We used the count API to get the number of documents in our store
index products
type. As with most API functions there are a variety of options available with this .count
function but the full list of options are outside of the scope of this article but you can consul the Elasticsearch documentation for those. Here is the function declaration:
client.count([params] [, options] [, callback])
We used the most simplistic example and gave it an index store
and the type products
to evaluate the count on.
Here was the result:
1 2 3 4 5 6 7 8 9 10 | Successful query! { "count": 14, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 } } |
You can see from the result that we’ve verified the number of documents (14) in our index.
Conclusion
In this tutorial, you learned how to get a count of the total number of documents in an index type. We used a simplistic but very common example. We hope that you can use this example as a basis for your specific application.
Just the Code
Here’s the code example illustrating how to get a simple count of the number of documents in an index type.
File app.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 | var elasticsearch = require("elasticsearch"); var client = new elasticsearch.Client({ hosts: ["http://localhost:9200"] }); client.count({index: 'store',type: 'products'}) .then(function(resp) { console.log("Successful query!"); console.log(JSON.stringify(resp, null, 4)); }, function(err) { console.trace(err.message); }); |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started