我們本來(lái)沒有某個(gè)type,或者沒有某個(gè)field,但是希望在插入數(shù)據(jù)的時(shí)候,es自動(dòng)為我們做一個(gè)識(shí)別,動(dòng)態(tài)映射出這個(gè)type的mapping,包括每個(gè)field的數(shù)據(jù)類型,一般用的動(dòng)態(tài)映射,dynamic mapping
dynamic mapping這里有個(gè)問題,如果說,我們其實(shí)對(duì)dynamic mapping有一些自己獨(dú)特的需求,比如說,es默認(rèn)來(lái)說,如經(jīng)過識(shí)別到一個(gè)數(shù)字,field: 10,默認(rèn)是搞成這個(gè)field的數(shù)據(jù)類型是long,再比如說,如果我們弄了一個(gè)field : "10",默認(rèn)就是text,還會(huì)帶一個(gè)keyword的內(nèi)置field。我們沒法改變。但是我們現(xiàn)在就是希望動(dòng)態(tài)映射的時(shí)候,根據(jù)我們的需求去映射,而不是讓es自己按照默認(rèn)的規(guī)則去玩兒
dyanmic mapping template,動(dòng)態(tài)映射模板
我們自己預(yù)先定義一個(gè)模板,然后插入數(shù)據(jù)的時(shí)候,相關(guān)的field,如果能夠根據(jù)我們預(yù)先定義的規(guī)則,匹配上某個(gè)我們預(yù)定義的模板,那么就會(huì)根據(jù)我們的模板來(lái)進(jìn)行mapping,決定這個(gè)Field的數(shù)據(jù)類型
默認(rèn)的動(dòng)態(tài)映射的效果咋樣
PUT /my_index/my_type/1
{
"test_string":"hello world",
"test_number":10
}
GET /my_index/_mapping/my_type
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"test_number": {
"type": "long"
},
"test_string": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
這個(gè)就是es的默認(rèn)的動(dòng)態(tài)映射規(guī)則,可能就不是我們想要的。
test_number:如果是個(gè)數(shù)字,我們希望默認(rèn)就是integer類型的
test_string:如果是字符串,我們希望默認(rèn)是個(gè)text,這個(gè)沒問題,但是內(nèi)置的field名字,叫做raw,不叫做keyword,類型還是keyword,然后保留500個(gè)字符
1、根據(jù)類型匹配映射模板
動(dòng)態(tài)映射模板,有兩種方式:
第一種,是根據(jù)新加入的field的默認(rèn)的數(shù)據(jù)類型,來(lái)進(jìn)行匹配,匹配上某個(gè)預(yù)定義的模板;
第二種,是根據(jù)新加入的field的名字,去匹配預(yù)定義的名字,或者qui匹配一個(gè)預(yù)定義的通配符,然后匹配上某個(gè)預(yù)定義的模板
PUT /my_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"my_type": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"string": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 500
}
}
}
}
}
]
}
}
}
PUT /my_index/my_type/1
{
"test_number": 10,
"test_string": "hello world"
}
GET /my_index/_mapping/my_type
{
"my_index": {
"mappings": {
"my_type": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"string": {
"match_mapping_type": "string",
"mapping": {
"fields": {
"raw": {
"ignore_above": 500,
"type": "keyword"
}
},
"type": "text"
}
}
}
],
"properties": {
"test_number": {
"type": "integer"
},
"test_string": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 500
}
}
}
}
}
}
}
}
2、根據(jù)字段名稱匹配映射模板
PUT /my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"string_as_integer": {
"match_mapping_type": "string",
"match": "long_*",
"unmatch": "*_text",
"mapping": {
"type": "integer"
}
}
}
]
}
}
}
舉個(gè)例子,field : "10",把類似這種field,弄成long型
PUT /my_index/my_type/1
{
"long_field":"10",
"long_field_text":"20"
}
GET /my_index/_mapping/my_type
{
"my_index": {
"mappings": {
"my_type": {
"dynamic_templates": [
{
"string_as_integer": {
"match": "long_*",
"unmatch": "*_text",
"match_mapping_type": "string",
"mapping": {
"type": "integer"
}
}
}
],
"properties": {
"long_field": {
"type": "integer"
},
"long_field_text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
場(chǎng)景,有些時(shí)候,dynamic mapping + template,每天有一堆日志,每天有一堆數(shù)據(jù)
這些數(shù)據(jù),每天的數(shù)據(jù)都放一個(gè)新的type中,每天的數(shù)據(jù)都會(huì)嘩嘩的往新的tye中寫入,此時(shí)你就可以定義一個(gè)模板,搞一個(gè)腳本,每天都預(yù)先生成一個(gè)新type的模板,里面講你的各個(gè)Field都匹配到一個(gè)你預(yù)定義的模板中去,就好了