How to Get the Mapping of an Existing Elasticsearch Index using NodeJS

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

Introduction

If you’re thinking about updating the mapping on an existing index or just analyzing your current index mappings, it would be useful to explicitly get the mapping of an index. Most Elasticsearch users are familiar with creating a mapping but not necessarily how to retrieve the mapping. What follows is an easy to follow step-by-step tutorial of how to retrieve the mapping of an index using NodeJS.

Prerequisites

Before we show you how to retrieve the mapping of an index using 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 maybe it’s been a long time since we created our mapping and we don’t remember whether we created department as a keyword field or a text field. Getting the mapping will answer these types of questions so let’s take a look at the Javascript code we’d use to get the mapping:

File: app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var elasticsearch = require("elasticsearch");

var client = new elasticsearch.Client({
  hosts: ["http://localhost:9200"]
});

/* Get Mapping of Index Type */
client.indices.getMapping({  
    index: 'store',
    type: 'products',
  },
function (error,response) {  
    if (error){
      console.log(error.message);
    }
    else {
      // console.log("Mappings:\n",response.gov.mappings.constituencies.properties);
      console.log("Mappings:\n",JSON.stringify(response, null, 4));
    }
});

We used the getMapping function on the indices API to get the mapping. Here is the function definition from the documentation:

1
client.indices.getMapping([params] [, options] [, callback])

We’re not going over all the options for this function but know that it has the capability to get the mappings for multiple document types over different indices. In this simple example we only provide the index and type parameters (store and products).

The remaining code defines what to do when the getMapping succeeds and what to do if it fails. In our example we print the error if it fails, and if it succeeds we print the mapping using JSON.stringify to make it in a more readable format.

Let’s take a look at the response:

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
26
27
28
29
30
31
32
33
$ node app.js
Mappings:
 {
    "store": {
        "mappings": {
            "products": {
                "properties": {
                    "department": {
                        "type": "keyword"
                    },
                    "id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "name": {
                        "type": "text"
                    },
                    "price": {
                        "type": "double"
                    },
                    "quantity": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

We have gotten back an easy to read mapping for our index and document type.

Conclusion

In this tutorial, you learned how to retrieve the mapping for a specified Elasticsearch index and document type. We hope that you find this example useful if your doing analysis of your current Elasticsearch indeces.

Just the Code

If you’re just looking for the code, here’s all the code we used illustrating how to retrieve the mapping of an index document type in Elasticsearch using Javascript running on NodeJS.

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.