How to Count the Number of Documents in an Elasticsearch Index using NodeJS

Have a Database Problem? Speak with an Expert for Free
Get Started >>

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:

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

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

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.