映射是存儲在索引中的文檔的輪廓。它定義了數(shù)據(jù)類型,如geo_point或字符串,以及文檔中顯示的字段的格式和規(guī)則,以控制動態(tài)添加字段的映射。
PUT bankaccountdetails
{
"mappings":{
"properties":{
"name": { "type":"text"}, "date":{ "type":"date"},
"balance":{ "type":"double"}, "liability":{ "type":"double"}
}
}
}當(dāng)我們運行上面的代碼時,我們得到如下所示的響應(yīng)-
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "bankaccountdetails"
}Elasticsearch為文檔中的字段支持多種不同的數(shù)據(jù)類型。這里詳細討論了用于在Elasticsearch中存儲字段的數(shù)據(jù)類型。
這些是基本數(shù)據(jù)類型,例如文本,關(guān)鍵字,日期,長整型,雙精度型,布爾型或ip,幾乎所有系統(tǒng)都支持。
這些數(shù)據(jù)類型是核心數(shù)據(jù)類型的組合。這些包括數(shù)組,JSON對象和嵌套數(shù)據(jù)類型。嵌套數(shù)據(jù)類型的示例如下所示&minus
POST /tabletennis/_doc/1
{
"group" : "players",
"user" : [
{
"first" : "dave", "last" : "jones"
},
{
"first" : "kevin", "last" : "morris"
}
]
}當(dāng)我們運行上面的代碼時,我們得到如下所示的響應(yīng)-
{
"_index" : "tabletennis",
"_type" : "_doc",
"_id" : "1",
_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}另一個示例代碼如下所示-
POST /accountdetails/_doc/1
{
"from_acc":"7056443341", "to_acc":"7032460534",
"date":"11/1/2016", "amount":10000
}當(dāng)我們運行上面的代碼時,我們得到如下所示的響應(yīng)-
{ "_index" : "accountdetails",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}我們可以使用以下命令檢查以上文檔-
GET /accountdetails/_mappings?include_type_name=false
在Elasticsearch 7.0.0或更高版本中創(chuàng)建的索引不再接受_default_映射。中創(chuàng)建的索引6.x將繼續(xù)在Elasticsearch 6.x中像以前一樣起作用。在7.0中的API中已棄用類型。