【ElasticSearch(十一)進階】Mapping的查詢、創建、修改、刪除
- Mapping(映射)是定義文檔及其包含的字段的存儲和索引方式的處理過程。
例如,使用Mapping定義:
哪些字符串字段應視為全文字段。
哪些字段包含數字,日期或地理位置。
日期值 的格式。
自定義規則,用於控制動態添加字段的映射 。
-
字段的數據類型,會在插入第一條數據時 自動識別。
但我們也可以自己 指定和修改
一、查詢Mapping
查詢bank的映射信息
GET /bank/_mapping
返回結果:
{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"employer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
二、創建Mapping
可以利用put方法指定映射關系。
【例子】
創建age為一個integer字段
創建email為一個keyword字段
創建name為一個text字段
PUT /my_index
{
"mappings":{
"properties":{
"age":{
"type": "integer"
},
"email":{
"type": "keyword"
},
"name":{
"type": "text"
}
}
}
}
返回內容
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my_index"
}
三、添加新的字段映射
新增一個字段 employee_id
字段的index
:控制是否對字段值建立索引。默認為true。值為false時,不可以通過這個屬性查詢該數據。
PUT /my_index/_mapping
{
"properties":{
"employee_id":{
"type": "keyword",
"index": false
}
}
}
返回結果:
{
"acknowledged" : true
}
四、修改映射
對於已經存在的映射字段,我們不能更新。想要更新必須創建新的索引,然后進行數據遷移。
1.先創建新的映射關系
PUT /newbank
{
"mappings": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text"
},
"age": {
"type": "integer"
},
"balance": {
"type": "long"
},
"city": {
"type": "keyword"
},
"email": {
"type": "keyword"
},
"employer": {
"type": "keyword"
},
"firstname": {
"type": "text"
},
"gender": {
"type": "keyword"
},
"lastname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "keyword"
}
}
}
}
2.數據遷移
- ES 7.x版本不包含Type屬性,遷移方式:
直接把舊index下的數據 遷移到新index下
POST _reindex
{
"source":{
"index": "twitter"
},
"dest":{
"index": "new_twitter"
}
}
- 舊版本有Type屬性,在index的type下的數據 進行遷移:
把舊index的type下的數據 遷移到新index下,我們希望新的遷移后就不存在type了。
POST _reindex
{
"source":{
"index": "bank",
"type": "account"
},
"dest":{
"index": "newbank"
}
}
返回結果,顯示遷移成功
#! [types removal] Specifying types in reindex requests is deprecated.
{
"took" : 51,
"timed_out" : false,
"total" : 1000,
"updated" : 0,
"created" : 1000,
"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" : [ ]
}
再次查詢確認遷移數據成功
GET /newbank/_search
返回結果:
可以發現_type
的值不是account
了,變成_doc
了(默認類型)。
{
"took" : 836,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "newbank",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"account_number" : 1,
"balance" : 39225,
"firstname" : "Amber",
"lastname" : "Duke",
"age" : 32,
"gender" : "M",
"address" : "880 Holmes Lane",
"employer" : "Pyrami",
"email" : "amberduke@pyrami.com",
"city" : "Brogan",
"state" : "IL"
}
},
{
"_index" : "newbank",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"account_number" : 6,
"balance" : 5686,
"firstname" : "Hattie",
"lastname" : "Bond",
"age" : 36,
"gender" : "M",
"address" : "671 Bristol Street",
"employer" : "Netagy",
"email" : "hattiebond@netagy.com",
"city" : "Dante",
"state" : "TN"
}
},
。。。
]
}
}