Learning Elastic Stack 7.0(Second Edition)
上QQ阅读APP看书,第一时间看更新

Defining the mappings for the type of product

The second step involves defining the mappings for the type of product. This step is executed because the type catalog did not exist before the first document was indexed. Remember the analogy of type with a relational database table. The table needs to exist before any row can be inserted. When a table is created in an RDBMS (Relational Database Management System), we define the fields (columns) and their datatypes in the CREATE TABLE statement.

When the first document is indexed within a type that doesn't exist yet, Elasticsearch tries to infer the datatypes of all the fields. This feature is called the dynamic mapping of types. By default, the dynamic mapping of types is enabled in Elasticsearch.

To see the mappings of the product type in the catalog index, execute the following command in the Kibana Console UI:

GET /catalog/_mapping

This is an example of a GET mapping API (https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html). You can request mappings of a specific type, all the types within an index, or within multiple indexes.

The response should look like the following:

{
"catalog" : {
"mappings" : {
"properties" : {
"ISBN" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"author" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"os" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"price" : {
"type" : "float"
},
"resolution" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"sku" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}

At the top level of the JSON response, catalog is the index for which we requested mappings. The mappings child product signifies the fact that these are mappings for the product type. The actual datatype mappings for each field are under the properties element. 

The actual type mappings that are returned will be slightly different from the ones shown in the preceding code. It has been simplified slightly. As you can see, only price is of the float datatype; the other fields were mapped to the text type. In reality, each text datatype field is mapped as follows:

"field_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}

As you may have noticed, each field that was sent as a string is assigned the text datatype. The text datatype enables full-text search on a field. Additionally, the same field is also stored as a multi-field, and it is also stored as a keyword type. This effectively enables full-text search and analytics (such as sorting, aggregations, and filtering) on the same field. We will look at both search and analytics in the upcoming chapters of this book.