How To Create Mapping For Elasticsearch With Kibana

Introduction

When importing data into Elasticsearch it will infer the types it uses based on the data that it is categorizing. It might be necessary to be explicit with details in Kibana if the working data is ambiguous or Elasticsearch is inferring the wrong type. Explicitly creating an object mapping is also good practice because it requires the user to think about how they want to store data. This tutorial will show how to create an index with an appropriate mapping of the details. Click here to see Just The Code.

Prerequisites

  • Elasticsearch Installed & Running.
  • Kibana will be used to interact with Elasticsearch, and so to follow along it is required to have Kibana installed and running. However, the user can follow along if they understand how to interact with Elasticsearch through curl or some other manner.

Create a Mapping for Elasticsearch with Kibana

STEP ONE – Analyze the Data

Elasticsearch’s own account example dataset will be used to demonstrate. That data can be obtained here.

Here is an example of one record in the data to get an idea of what is being worked with:

{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}

The dataset is set of data commonly associated with an account like firstname, lastname, and balance.

STEP TWO – Break the Data Fields

First, the data must be broken up into fields. For this example, these are the fields:

account_number
balance
firstname
lastname
age
gender
address
employer
email
city
state

STEP THREE – Associate Each Field with an Elasticsearch Data Type

Now map each field to an Elasticsearch data type by the data it will store. For example, if the field stored a date, then the Elasticsearch Date datatype would be used. Here is a list of Elasticsearch’s Core Datatypes for reference. (There are more, but these are the most common.)

Core Datatypes:

  • string (text, keyword)
  • numeric (long, integer, short, byte, double, float, half_float, scaled_float)
  • date
  • boolean
  • binary
  • range (integer_range, float_range, long_range, double_range, date_range)
  • array
  • object

Note: Elasticsearch supports geo data and data types for a range of specialized applications.

For the accounts example, the following data types have been chosen. Most of them are pretty straightforward text or integers used to cover the finer points.

The most challenging component here is choosing between what type of string some of these fields should be, text or keyword. Lengthy text fields like product descriptions or user comments are what text fields are typically used for. This is because these are fields that the user should be able to find by searching a single word in that field. For instance, searching for “soft” should find any product with “soft” in the description. Strings for keyword on the other hand are used for string fields that are searched for by the entire string. For example, city is mapped as a keyword string because people would not search for “North” and expect to get “North Carolina.” They would instead search for the entire string “North Carolina”.

Account number is mapped as an integer because it is never a decimal.

The balance, surprisingly, is also mapped as an integer because in the data it was never a decimal. It is likely that the balance being stored is the total cents in the bank account.

The final mapping chosen is below:

account_number->integer
balance->integer
firstname->text
lastname->text
age->integer
gender->text ("M" or "F")
address->text
employer->text
email->keyword
city->keyword
state->string

STEP FOUR – Create the Index, Type, and Mapping

Next, it is shown how to create the actual index, type, and mapping. This index will be called financial and the type accounts. Remember to think of the index as a database and the type as a table.

The PUT command is used with json for how the index is to be structured in the body of the command. The mappings object describes the accounts type and all the properties within it. It also describes the datatype of each property.

PUT /financial
{
    "mappings": {
        "properties": {
            "account_number": {
                "type": "integer"
            },
            "balance": {
                "type": "integer"
            },
            "firstname": {
                "type": "text"
            },
            "lastname": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "gender": {
                "type": "text"
            },
            "address": {
                "type": "text"
            },
            "employer": {
                "type": "text"
            },
            "email": {
                "type": "keyword"
            },
            "city": {
                "type": "keyword"
            },
            "state": {
                "type": "keyword"
            }
        }
    }
}

NOTE: As of Elasticsearch v6.0 the _doc type has been deprecated and is not supported at all in version 7.

Conclusion

Thanks for following along with how to create a mapping for Elasticsearch using Kibana and hopefully there are no difficulties applying this knowledge to the specified application. Data imports should go much more smoothly with a thoughtful mapping. The new mapping will also enable a more efficient retrieval of the desired info. This is a simple process that will complete the task without errors but feel free to leave any feedback about the accuracy and effectiveness of this walkthrough. Below is a detailed list of the commands showing only the code needed to complete it. If you need help managing the database for your project please don’t hesitate to reach an expert at Object Rocket.

Just The Code

...
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
...
PUT /financial
{
    "mappings": {
        "properties": {
            "account_number": {
                "type": "integer"
            },
            "balance": {
                "type": "integer"
            },
            "firstname": {
                "type": "text"
            },
            "lastname": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "gender": {
                "type": "text"
            },
            "address": {
                "type": "text"
            },
            "employer": {
                "type": "text"
            },
            "email": {
                "type": "keyword"
            },
            "city": {
                "type": "keyword"
            },
            "state": {
                "type": "keyword"
            }
        }
    }
}

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.