4 Mapping

 

This chapter covers

  • Field data types
  • Implicit and explicit mapping
  • Core data types
  • Advanced data types
  • APIs to create/access mappings

Data is like a rainbow—it comes in all sorts of “colors.” Business data has various shapes and forms represented as textual information, dates, numbers, inner objects, Booleans, geolocations, IP addresses, and so on. In Elasticsearch, we model and index data as JSON documents, with each document consisting of a number of fields and every field containing a certain type of data. For example, a movie document may consist of a title and synopsis represented as textual data, a release date as a date, and gross earnings as floating-point data.

In earlier chapters, when we indexed sample documents, we did not bother about the data types of the fields. Elasticsearch derived these types implicitly by looking at each field and the type of information in it. Elasticsearch created a schema without us having to do any up-front work, unlike in a relational database. It is mandatory to have the table schema defined and developed in a database before retrieving or persisting data. But we can prime Elasticsearch with documents without defining a schema for our data model. This schema-free feature helps developers get up and running with the system from day one. However, best practice is to develop a schema up front rather than letting Elasticsearch define it for us, unless our requirements do not need one.

4.1 Overview of mapping

4.1.1 Mapping definition

4.1.2 Indexing a document for the first time

4.2 Dynamic mapping

4.2.1 The mechanism for deducing types

4.2.2 Limitations of dynamic mapping

4.3 Explicit mapping

4.3.1 Mapping using the indexing API

4.3.2 Updating schema using the mapping API

4.3.3 Modifying existing fields is not allowed