
Elasticsearch 8.x Cookbook
By :

If we consider the index as a database in the SQL world, mapping is similar to the create table definition.
Elasticsearch can understand the structure of the document that you are indexing (reflection) and create the mapping definition automatically. This is called explicit mapping creation.
To execute the code in this recipe, you will need an up-and-running Elasticsearch installation, as described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute these commands, you can use any HTTP client, such as curl
(https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar platforms. I suggest using the Kibana console to provide code completion and better character escaping for Elasticsearch.
To understand the examples and code in this recipe, basic knowledge of JSON is required.
You can explicitly create a mapping by adding a new document to Elasticsearch. For this, perform the following steps:
PUT test
The output will be as follows:
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "test" }
PUT test/_doc/1 {"name":"Paul", "age":35}
The output will be as follows:
{ "_index" : "test", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : {"total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
GET test/_mapping
{ "test" : { "mappings" : { "properties" : { "age" : { "type" : "long" }, "name" : { "type" : "text", "fields" : { "keyword" : {"type" : "keyword", "ignore_above" : 256 } } } } } } }
DELETE test
The output will be as follows:
{ "acknowledged" : true }
The first command line (Step 1) creates an index where we can configure the mappings in the future, if required, and store documents in it.
The second command (Step 2) inserts a document in the index (we'll learn how to create the index in the Creating an index recipe of Chapter 3, Basic Operations, and record indexing in the Indexing a document recipe of Chapter 3, Basic Operations).
Elasticsearch reads all the default properties for the field of the mapping and starts to process them as follows:
In Elasticsearch, every document has a unique identifier, called an ID for a single index, which is stored in the special _id
field of the document.
The _id
field can be provided at index time or can be assigned automatically by Elasticsearch if it is missing.
When a mapping type is created or changed, Elasticsearch automatically propagates mapping changes to all the nodes in the cluster so that all the shards are aligned to process that particular type.
In Elasticsearch 7.x, there was a default type (_doc
): it was removed in Elasticsearch 8.x and above.
Please refer to the following recipes in Chapter 3, Basic Operations: