Elasticsearch 简明教程
Elasticsearch - Mapping
映射是存储在索引中的文档的概要。它定义了数据类型(如 geo_point 或 string)和文档中存在的字段的格式,以及控制动态添加的字段的映射的规则。
Mapping is the outline of the documents stored in an index. It defines the data type like geo_point or string and format of the fields present in the documents and rules to control the mapping of dynamically added fields.
PUT bankaccountdetails
{
"mappings":{
"properties":{
"name": { "type":"text"}, "date":{ "type":"date"},
"balance":{ "type":"double"}, "liability":{ "type":"double"}
}
}
}
当我们运行上述代码时,会得到如下所示的响应:
When we run the above code, we get the response as shown below −
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "bankaccountdetails"
}
Field Data Types
Elasticsearch 支持为文档中的字段设置不同类型的数据类型。此处详细讨论了用于在 Elasticsearch 中存储字段的数据类型。
Elasticsearch supports a number of different datatypes for the fields in a document. The data types used to store fields in Elasticsearch are discussed in detail here.
Core Data Types
这些数据类型为 text、keyword、date、long、double、boolean 或 ip 等基本数据类型,几乎所有系统都支持。
These are the basic data types such as text, keyword, date, long, double, boolean or ip, which are supported by almost all the systems.
Complex Data Types
这些数据类型组合了核心数据类型。其中包括数组、JSON 对象和嵌套数据类型。嵌套数据类型的示例如下所示:
These data types are a combination of core data types. These include array, JSON object and nested data type. An example of nested data type is shown below &minus
POST /tabletennis/_doc/1
{
"group" : "players",
"user" : [
{
"first" : "dave", "last" : "jones"
},
{
"first" : "kevin", "last" : "morris"
}
]
}
当我们运行上述代码时,会得到如下所示的响应:
When we run the above code, we get the response as shown below −
{
"_index" : "tabletennis",
"_type" : "_doc",
"_id" : "1",
_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
另一个示例代码如下所示:
Another sample code is shown below −
POST /accountdetails/_doc/1
{
"from_acc":"7056443341", "to_acc":"7032460534",
"date":"11/1/2016", "amount":10000
}
当我们运行上述代码时,会得到如下所示的响应:
When we run the above code, we get the response as shown below −
{ "_index" : "accountdetails",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
我们可以使用以下命令检查上述文档:
We can check the above document by using the following command −
GET /accountdetails/_mappings?include_type_name=false
Removal of Mapping Types
在 Elasticsearch 7.0.0 或更高版本中创建的索引不再接受 default 映射。在 Elasticsearch 6.x 中创建的索引将继续在 Elasticsearch 6.x 中像以前一样运行。Elasticsearch 7.0 中不再提供 API 类型的支持。
Indices created in Elasticsearch 7.0.0 or later no longer accept a default mapping. Indices created in 6.x will continue to function as before in Elasticsearch 6.x. Types are deprecated in APIs in 7.0.