什么是映射
- 為了能夠把日期字段處理成日期,把數字字段處理成數字,把字符串字段處理成全文本(Full-text)或精確(Exact-value)的字符串值,Elasticsearch需要知道每個字段里面都包含什么數據類型。這些類型和字段的信息存儲在映射中
- 創建索引的時候,可以預先定義字段的類型以及相關屬性,相當於定義數據庫字段的屬性
映射的分類
靜態映射
動態映射
什么是動態映射
文檔中碰到一個以前沒見過的字段時,動態映射可以自動決定該字段的類型,並對該字段添加映射
如何配置動態映射
- 通過dynamic屬性進行控制
- true:默認值,動態添加字段; false:忽略新字段; strict:碰到陌生字段,拋出異常
適用范圍
適用在根對象上或者object類型的任意字段上
字段類型
string | 字符串 |
integer | 數字 |
long | A long value(64bit) |
float | A floating-point number(32bit):such as 1,2,3,4 |
double | A floating-poing number(64bit) |
bool | A bool value,such as True,False |
date | such as:2017-02-20 |
binary | 二進制 |
創建映射
POST /library #給索引為library創建映射關系 { "settings":{ "number_of_shards" : 5, "number_of_replicas" : 1 }, "mappings":{ "books":{ #索引為library的type類型為books "properties":{ #這里往下就是映射關系 "title":{"type":"string"}, "name":{"type":"string","index":"not_analyzed"}, "publish_date":{"type":"date","index":"not_analyzed"}, "price":{"type":"double"}, "number":{ "type":"object", "dynamic":true } } } } }
獲取映射
獲取index為library的映射
GET library/_mapping
{ "library": { "mappings": { "books": { "properties": { "name": { "type": "string", "index": "not_analyzed" }, "number": { "type": "object", "dynamic": "true" }, "price": { "type": "double" }, "publish_date": { "type": "date", "format": "dateOptionalTime" }, "title": { "type": "string" } } } } } }
獲取index為library,type為books的映射
GET /libraryyry/_mapping/books
獲取集群內所有的映射信息
GET /_all/_mapping/
獲取這個集群內某兩個或多個type映射信息(books和bank_account映射信息)
GET /_all/_mapping/books,bank_account
刪除映射
DELETE /libraryry/books DELETE /libraryry/books/_mapping #刪除books的映射 DELETE /libraryry/_mapping/books,bank_acount #刪除多個映射
無法修改已經存在的mapping映射
1.如果要推到現有的映射,你得重新建立一個索引.然后重新定義映射 2.然后把之前索引里的數據導入到新的索引里 -------具體方法------ 1.給現有的索引定義一個別名,並且把現有的索引指向這個別名,運行步驟2 2.運行: PUT /現有索引/_alias/別名A 3.新創建一個索引,定義好最新的映射 4.將別名指向新的索引.並且取消之前索引的執行,運行步驟5 5.運行: POST /_aliases { "actions":[ {"remove" : { "index": "現有索引名". "alias":"別名A" }}. {"add" : { "index": "新建索引名", "alias":"別名A" }} ] } 注意:通過這幾個步驟就實現了索引的平滑過渡,並且是零停機