Creating type mapping in an existing index

With Elasticsearch 7.0, indexes contain strictly one type, and hence it is generally recommended that you create the index and the default type within that index at index creation time. The default type name is _doc.

In the earlier versions of Elasticsearch (6.0 and before), it was possible to define an index and then add multiple types to that index as needed. This is still possible but it is a deprecated feature. A type can be added within an index after the index is created using the following code. The mappings for the type can be specified as follows:

PUT /catalog/_mapping
{
"properties": {
"name": {
"type": "text"
}
}
}

This command creates a type called _doc, with one field of the text type in the existing index catalog. Let's add a couple of documents after creating the new type:

POST /catalog/_doc
{
"name": "books"
}
POST /catalog/_doc
{
"name": "phones"
}

After a few documents are indexed, you realize that you need to add fields in order to store the description of the category. Elasticsearch will assign a type automatically based on the value that you insert for the new field. It only takes into consideration the first value that it sees to guess the type of that field:

POST /catalog/_doc
{
"name": "music",
"description": "On-demand streaming music"
}

When the new document is indexed with fields, the field is assigned a datatype based on its value in the initial document. Let's look at the mapping after this document is indexed:

{
"catalog" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text"
}
}
}
}
}

The field description has been assigned the text datatype, with a field with the name keyword, which is of the keyword type. What this means is that, logically, there are two fields, description and description.keyword. The description field is analyzed at the time of indexing, whereas the description.keyword field is not analyzed and is stored as is without any analysis. By default, fields that are indexed with double quotes for the first time are stored as both text and keyword types.

If you want to take control of the type, you should define the mapping for the field before the first document containing that field is indexed. A field's type cannot be changed after one or more documents are indexed within that field. Let's see how to update the mapping to add a field with the desired type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.45.127