Software Engineer, Before Inserting One Iota of Data in an Elasticsearch Index, You Must Do This.

Photo by Isaiah Rustad on Unsplash

Introduction

What was the problem?

  1. I first created and seeded an index with my mangas’ list.
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}

What does mapping really mean?

Why does a good mapping matter?

  1. Bad mapping leads to frustrating and unexpected searching results.
  2. Good mapping prevents you from mapping explosions and saves you from headaches.
  3. It’s not possible to redefine the data type of an existing field when the index has already got data, unfortunately, but it’s so logical.
  4. Bad mapping cuts down on tremendous opportunities we have to use some advanced Elasticsearch features related to data types such as geodata features.

Types of data in Elasticsearch

Common field types

Objects and relational types

"mappings": { 
"properties": {
"age": { "type": "integer" },
"name": {
"properties": {
"first": { "type": "text" },
"last": { "type": "text" }
}
}
}
}

Structured data types

Aggregate data types

Text search types

Document ranking types

Spatial data types

Special field data types

  • array data field type: actually, it’s not really a data type. There is no dedicated array data type. Any field can contain nothing or many values by default, however, all values in the array must be of the same data type. So you can store an array of strings, an array of integers even an array of arrays.
  • multi-fields: let’s assume you need to store a value as text and at the same time as keyword. That’s the purpose of multi-fields in Elasticsearch.

How to define an index mapping the right way?

  1. Analyze your data: What kind of information are you going to store in your field (strings, sentences, numeric values, IP addresses, raw texts)? What kinds of operation will you be performing on the data stored in your field (aggregations, searches, …)? If you want to perform searches against this field, will it be exact terms matching searches (to decide to go for “keyword” instead of “text” for example)?
  2. Choose the right data type for your field: Look at the different data types we went through earlier and choose the best one for your field.
  3. Then define your mapping: There are mainly two ways to define your index’s mapping. Firstly, you can set your index’s mapping when creating it.
PUT /books
{
"mappings": {
"properties": {
"title": { "type": "text" },
"author": { "type": "keyword" },
"published": { "type": "date" }
}
}
}
PUT /books/_mapping
{
"properties": {
"title": { "type": "text" },
"author": { "type": "keyword" },
"published": { "type": "date" }
}
}
}

Hands-on

Conclusion

  • You need to look at your data and ask yourself the right questions.
  • Then you have to choose from the plethora of data types Elasticsearch provides, the right one for your field.
  • And eventually, you have to set the mapping of your index.

--

--

--

Certified AWS Solution Architect, Fullstack Software Engineer & DevOps. I like Solving Challenging Software Engineering Problems & Building Amazing Solutions.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Windows - Kill process by its Port Number

Aggregations in Elasticsearch : 3 Must Known as Software Engineer

uniqar customer care number=.9883774520=.9883774520

My k8s note. create deployment.

Julia Programming Language — From Zero to Expert

Defying the stigma, ZKSwap’s Clarification on Bad Rumours

Writing a README.md Part I: Guides and Resources

Austin Coding Academy Blog #1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akintola L. F. ADJIBAO

Akintola L. F. ADJIBAO

Certified AWS Solution Architect, Fullstack Software Engineer & DevOps. I like Solving Challenging Software Engineering Problems & Building Amazing Solutions.

More from Medium

How to: pgAdmin with Open ID Connect in Docker-Compose

Developing a Flask PostgreSQL application using Github Codespaces

Codespace screen

Docker — A Beginner’s Guide to Understanding and Building Docker Images

Intro to Docker-compose with Express and MongoDB