以下內容針對ES 6以下的版本
簡介
ES提供了PUT mapping API允許你可以:
- 給已經存在的index加上一個新的type
- 加上一個新的field給已經存在的type
但是請注意愁茁,你不能使用這個API:
- 修改某一個已經存在的字段的類型
會出現conflic error
{
"type": "illegal_argument_exception",
"reason": "Mapper for [XXX] conflicts with existing mapping in other types:\n[mapper [XXX] has different [analyzer]]"
}
這就說明你嘗試修改一個已經存在的字段的類型咒林,出現了沖突堤尾。
- 刪除某一個已經存在的字段
你會發(fā)現永遠無法刪除成功棒坏,當你PUT mapping成功之后阶界,然后再次調用GET mapping摘盆,你會發(fā)現你刪除的字段依然存在挺尿。
因此這個API所做的其實是創(chuàng)建
添加
而并非修改
例子
創(chuàng)建
我們使用PUT indexName
作為創(chuàng)建指令:
PUT indexName
這個API的body中可以帶很多內容作為初始化的setting奏黑。
- 在一個已經存在/不存在的index test中創(chuàng)建新的type1以及type2,并且定義mapping
PUT test
{
"mappings" : {
"type1" : {
"_all": {
enable: false
},
"properties" : {
"field1" : { "type" : "text" }
}
},
"type2": {
...
}
}
}
請注意编矾,這個指令是用來創(chuàng)建的熟史,因此前提條件是:
- index可以存在或者不存在
- 被定義mapping的type必須不存在
否則就會報錯說test index already exists
添加新的field
我們使用 PUT {indexName}/_mapping/{type}
這個API作為添加的API,千萬不要使用上面那個創(chuàng)建的API窄俏,因為有可能出現exists error
PUT twitter/_mapping/user
{
"properties": {
"name": {
"type": "text"
}
}
}
- 注意
body中通常都只有"properties"
section蹂匹,如果你想要加入其他的meta field
,有可能會報錯凹蜈, 如下就會報錯
PUT twitter/_mapping/user
{
"_all": {
"enable": false
},
"_parent": {
...
},
"properties": {
"name": {
"type": "text"
}
}
}
如何put那些有meta field的mapping限寞?
比如上面的mapping中包含_all
、_parent
等meta field仰坦,每次直接put就會說_all無法識別履植。
那么將body改成這樣就可以PUT成功
PUT twitter/_mapping/user
{
"user": {
"_all": {
"enable": false
},
"_parent": {
...
},
"properties": {
"name": {
"type": "text"
}
}
}
}
使用type name
將所有的內容都包在一起