ElasticSearch最佳入門實踐(六十四)索引管理_定製化自己的dynamic mapping
阿新 • • 發佈:2018-11-19
1、定製dynamic策略
true:遇到陌生欄位,就進行dynamic mapping
false:遇到陌生欄位,就忽略
strict:遇到陌生欄位,就報錯
定製
PUT /my_index { "mappings": { "my_type": { "dynamic": "strict", "properties": { "title": { "type": "text" }, "address": { "type": "object", "dynamic": "true" } } } } }
未定義的欄位
刪除之後就可以儲存了
檢視mapping
2、定製dynamic mapping策略
(1)date_detection
預設會按照一定格式識別date,比如yyyy-MM-dd。但是如果某個field先過來一個2017-01-01的值,就會被自動dynamic mapping成date,後面如果再來一個"hello world"之類的值,就會報錯。可以手動關閉某個type的date_detection,如果有需要,自己手動指定某個field為date型別。
PUT /my_index/_mapping/my_type { "date_detection": false }
(2)定製自己的dynamic mapping template(type level)
PUT /my_index { "mappings": { "my_type": { "dynamic_templates": [ { "en": { "match": "*_en", "match_mapping_type": "string", "mapping": { "type": "string", "analyzer": "english" } }} ] }}}
案例
包含 en 的 title 會走 english 的分詞器
PUT /my_index/my_type/1
{
"title": "this is my first article"
}
PUT /my_index/my_type/2
{
"title_en": "this is my first article"
}
title沒有匹配到任何的dynamic模板,預設就是standard分詞器,不會過濾停用詞,is會進入倒排索引,用is來搜尋是可以搜尋到的
title_en匹配到了dynamic模板,就是english分詞器,會過濾停用詞,is這種停用詞就會被過濾掉,用is來搜尋就搜尋不到了
(3)定製自己的default mapping template(index level)
PUT /my_index
{
"mappings": {
"_default_": {
"_all": { "enabled": false }
},
"blog": {
"_all": { "enabled": true }
}
}
}