How to Get the Mapping of an Existing Elasticsearch Index using NodeJS
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:
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 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