Mapping類似數(shù)據(jù)庫中的schema的定義,作用如下:
- 定義索引中的字段的名稱
- 定義字段的數(shù)據(jù)類型,例如字符串,數(shù)字,布爾等
- 字段,倒排索引的相關(guān)配置(Analyzed or Not Analyzed,Analyzer)
ES中Mapping映射可以分為動態(tài)映射和靜態(tài)映射。
動態(tài)映射:
????????在關(guān)系數(shù)據(jù)庫中,需要事先創(chuàng)建數(shù)據(jù)庫,然后在該數(shù)據(jù)庫下創(chuàng)建數(shù)據(jù)表,并創(chuàng)建表字段、類型、長度、主鍵等,最后才能基于表插入數(shù)據(jù)。而Elasticpearch中不需要定義Mapping映射(即關(guān)系型數(shù)據(jù)庫的表、字段等),在文檔寫入Elasticsearch時,會根據(jù)文檔字段自動識別類型,這種機(jī)制稱之為動態(tài)映射。
????????動態(tài)映射(Dynamic Mapping)的機(jī)制,使得我們無需手動定義Mappings,Elasticsearch會自動根據(jù)文檔信息,推算出字段的類型。但是有時候會推算的不對,例如地理位置信息。當(dāng)類型如果設(shè)置不對時,會導(dǎo)致一些功能無法正常運(yùn)行,例如Range查詢
靜態(tài)映射:
????????靜態(tài)映射是在Elasticsearch中也可以事先定義好映射,包含文檔的各字段類型、分詞器等,這種方式稱之為靜態(tài)映射。
Dynamic Mapping類型自動識別:
JSON類型 | ElasticSearch類型 |
字符串? |
|
布爾值 | boolean |
浮點數(shù) | float |
整數(shù) | long |
對象 | Object |
數(shù)組 | 由第一個非空數(shù)值的類型所決定 |
空值 | 忽略 |
動態(tài)映射:
創(chuàng)建文檔(es根據(jù)數(shù)據(jù)類型,會自動創(chuàng)建映射)
# 創(chuàng)建文檔(es根據(jù)數(shù)據(jù)類型,會自動創(chuàng)建映射)
PUT /user/_doc/1
{
"name": "張三",
"age": 26,
"address": "北京市朝陽區(qū)"
}
運(yùn)行結(jié)果:
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
獲取文檔映射
# 獲取文檔映射
GET /user/_mapping
運(yùn)行結(jié)果:
{
"user" : {
"mappings" : {
"properties" : {
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
Mapping一旦建立后,那后期是否還可以更新或者修改Mapping字段類型信息呢?
情況一:新增加字段
- dynamic設(shè)為true時,一旦有新增字段的文檔寫入,Mapping 也同時被更新
- dynamic設(shè)為false,Mapping 不會被更新,新增字段的數(shù)據(jù)無法被索引,但是信息會出現(xiàn)在_source中
- dynamic設(shè)置成strict(嚴(yán)格控制策略),文檔寫入失敗,拋出異常
情況二:對于已有字段,一旦已經(jīng)有數(shù)據(jù)寫入,就不再支持修改字段定義
- Lucene實現(xiàn)的倒排索引,—旦生成后,就不允許修改
- 如果希望改變字段類型,必須Reindex API,重建索引
????????如果修改了字段的數(shù)據(jù)類型,會導(dǎo)致已被索引的數(shù)據(jù)無法被搜索;但是如果是增加新的字段,就不會有這樣的影響。
首先新建mapping信息:
PUT user2
{
"mappings": {
"dynamic": "strict",
"properties": {
"name": {
"type": "text"
},
"address": {
"type": "object",
"dynamic": "true"
}
}
}
}
運(yùn)行結(jié)果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "user2"
}
插入新的文檔,原因為age為新增字段報錯,拋出異常,之前mapping中dynamic設(shè)置的為strict
# 插入新的文檔,原因為age為新增字段報錯,拋出異常,之前mapping中dynamic設(shè)置的為strict
PUT user2/_doc/1
{
"name": "張三",
"age": 26,
"address": {
"provienc": "北京市",
"city": "北京市"
}
}
運(yùn)行結(jié)果:
{
"error" : {
"root_cause" : [
{
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [age] within [_doc] is not allowed"
}
],
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [age] within [_doc] is not allowed"
},
"status" : 400
}
接下來修改dynamic信息,更改為true后再次執(zhí)行上面的語句:
# 修改mapping信息
PUT user2/_mapping
{
"dynamic": "true"
}
運(yùn)行結(jié)果:
{
"acknowledged" : true
}
dynamic修改為true后,再次插入一個新的文檔
# dynamic修改為true后,再次插入一個新的文檔
PUT user2/_doc/1
{
"name": "張三",
"age": 26,
"address": {
"provienc": "北京市",
"city": "北京市"
}
}
運(yùn)行結(jié)果:
{
"_index" : "user2",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
添加新字段后,再次查詢mapping信息
# 添加新字段后,再次查詢mapping信息
GET user2/_mapping
運(yùn)行結(jié)果:? 【可以看出新增加了age字段的信息】
{
"user2" : {
"mappings" : {
"dynamic" : "true",
"properties" : {
"address" : {
"dynamic" : "true",
"properties" : {
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"provienc" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"age" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
}
}
}
對已經(jīng)存在的mapping映射進(jìn)行修改:
具體方法:
- 如果要推倒現(xiàn)有的映射,你得重新建立—個靜態(tài)索引
- 然后把之前索引里的數(shù)據(jù)導(dǎo)入到新的索引里
- 刪除原創(chuàng)建的索引
- 為新索引起個別名,為原索引名
注意:通過上述4個步驟實現(xiàn)了索引的平滑過渡,并且是零停機(jī)!
1.重新創(chuàng)建一個靜態(tài)索引,并將數(shù)據(jù)遷移到新索引中
# 重新創(chuàng)建一個靜態(tài)索引
POST _reindex
{
"source": {
"index": "user2"
},
"dest": {
"index": "new_user2"
}
}
運(yùn)行結(jié)果:
{
"took" : 185,
"timed_out" : false,
"total" : 1,
"updated" : 0,
"created" : 1,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
2.查看新索引mapping信息
# 查看新索引mapping信息
GET new_user2/_mapping
運(yùn)行結(jié)果:? ?【新索引和舊索引mapping信息是相同的】
{
"new_user2" : {
"mappings" : {
"properties" : {
"address" : {
"properties" : {
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"provienc" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"age" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
3.查看新索引的數(shù)據(jù)信息
# 查看新索引的數(shù)據(jù)信息
GET new_user2/_search
運(yùn)行結(jié)果:? 【新索引已經(jīng)包含了舊索引所有的數(shù)據(jù)】
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "new_user2",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "張三",
"age" : 26,
"address" : {
"provienc" : "北京市",
"city" : "北京市"
}
}
}
]
}
}
4.刪除之前的舊索引
# 刪除舊索引
DELETE user2
運(yùn)行結(jié)果:
{
"acknowledged" : true
}
5.為新索引起個別名,別名為舊索引的名稱
# 為新索引起別名
PUT new_user2/_alias/user2
運(yùn)行結(jié)果:
{
"acknowledged" : true
}
注意:起別名前要確保別名不是已經(jīng)存在的索引,如果存在則報錯!這也是起別名之前先刪除舊索引的原因!?
6.查詢舊索引(即為新索引的別名)
# 查詢舊索引(即為新索引的別名)
GET user2
# 效果等同于 GETuser2
GET new_user2
運(yùn)行結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-596994.html
{
"new_user2" : {
"aliases" : {
"user2" : { }
},
"mappings" : {
"properties" : {
"address" : {
"properties" : {
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"provienc" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"age" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "new_user2",
"creation_date" : "1667317074691",
"number_of_replicas" : "1",
"uuid" : "rvKsAyTKQeuc-bWklCW-8A",
"version" : {
"created" : "7170699"
}
}
}
}
}
注意:新起的別名是不能DELETE的!拋出異常!文章來源地址http://www.zghlxwxcb.cn/news/detail-596994.html
到了這里,關(guān)于elasticsearch中文檔映射Mapping用法詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!