以下內(nèi)容針對(duì)ES 6以下的版本
簡(jiǎn)介
ES提供了PUT mapping API允許你可以:
- 給已經(jīng)存在的index加上一個(gè)新的type
- 加上一個(gè)新的field給已經(jīng)存在的type
但是請(qǐng)注意,你不能使用這個(gè)API:
- 修改某一個(gè)已經(jīng)存在的字段的類型
會(huì)出現(xiàn)conflic error
{
"type": "illegal_argument_exception",
"reason": "Mapper for [XXX] conflicts with existing mapping in other types:\n[mapper [XXX] has different [analyzer]]"
}
這就說(shuō)明你嘗試修改一個(gè)已經(jīng)存在的字段的類型,出現(xiàn)了沖突。
- 刪除某一個(gè)已經(jīng)存在的字段
你會(huì)發(fā)現(xiàn)永遠(yuǎn)無(wú)法刪除成功,當(dāng)你PUT mapping成功之后,然后再次調(diào)用GET mapping,你會(huì)發(fā)現(xiàn)你刪除的字段依然存在。
因此這個(gè)API所做的其實(shí)是創(chuàng)建 添加而并非修改
例子
創(chuàng)建
我們使用PUT indexName作為創(chuàng)建指令:
PUT indexName
這個(gè)API的body中可以帶很多內(nèi)容作為初始化的setting。
- 在一個(gè)已經(jīng)存在/不存在的index test中創(chuàng)建新的type1以及type2,并且定義mapping
PUT test
{
"mappings" : {
"type1" : {
"_all": {
enable: false
},
"properties" : {
"field1" : { "type" : "text" }
}
},
"type2": {
...
}
}
}
請(qǐng)注意,這個(gè)指令是用來(lái)創(chuàng)建的,因此前提條件是:
- index可以存在或者不存在
- 被定義mapping的type必須不存在
否則就會(huì)報(bào)錯(cuò)說(shuō)test index already exists
添加新的field
我們使用 PUT {indexName}/_mapping/{type}這個(gè)API作為添加的API,千萬(wàn)不要使用上面那個(gè)創(chuàng)建的API,因?yàn)橛锌赡艹霈F(xiàn)exists error
PUT twitter/_mapping/user
{
"properties": {
"name": {
"type": "text"
}
}
}
- 注意
body中通常都只有"properties" section,如果你想要加入其他的meta field,有可能會(huì)報(bào)錯(cuò), 如下就會(huì)報(bào)錯(cuò)
PUT twitter/_mapping/user
{
"_all": {
"enable": false
},
"_parent": {
...
},
"properties": {
"name": {
"type": "text"
}
}
}
如何put那些有meta field的mapping?
比如上面的mapping中包含_all、_parent等meta field,每次直接put就會(huì)說(shuō)_all無(wú)法識(shí)別。
那么將body改成這樣就可以PUT成功
PUT twitter/_mapping/user
{
"user": {
"_all": {
"enable": false
},
"_parent": {
...
},
"properties": {
"name": {
"type": "text"
}
}
}
}
使用type name將所有的內(nèi)容都包在一起