背景:
Elasticsearch具有動(dòng)態(tài)mapping的功能,如果沒(méi)有設(shè)置字段的映射,Elasticsearch將會(huì)對(duì)字段進(jìn)行動(dòng)態(tài)映射。但是有時(shí)候動(dòng)態(tài)映射會(huì)出現(xiàn)不符合業(yè)務(wù)需求的字段映射,比如時(shí)間戳的映射,elasticsearch將其自動(dòng)映射為long類型,但是在業(yè)務(wù)使用中需要date類型。
更改索引blog中的動(dòng)態(tài)映射字段priority數(shù)據(jù)類型:由long更改為date
一、新建動(dòng)態(tài)索引模板,此時(shí)time字段為long類型
put http://localhost:9200/_template/test
{
"template": "test_*",
"settings": {
"index.refresh_interval": "5s",
"index.translog.durability": "async",
"index.translog.flush_threshold_size": "512m",
"index.number_of_replicas": 0,
"index.routing.allocation.total_shards_per_node": 1,
"index.number_of_shards": 1,
"index.unassigned.node_left.delayed_timeout": "30m"
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"regix_long_string": {
"match_pattern": "regex",
"mapping": {
"type": "long"
},
"match_mapping_type": "string",
"match": ".*(?i)(num|cnt|count)$"
}
},
{
"regix_double_string": {
"match_pattern": "regex",
"mapping": {
"type": "double"
},
"match_mapping_type": "string",
"match": ".*(?i)(amount|price)$"
}
},
{
"longs_as_strings": {
"unmatch": "*_text",
"mapping": {
"type": "long"
},
"match_mapping_type": "string",
"match": "long_*"
}
},
{
"double_as_strings": {
"unmatch": "*_text",
"mapping": {
"type": "double"
},
"match_mapping_type": "string",
"match": "double_*"
}
},
{
"date_as_strings": {
"unmatch": "*_text",
"mapping": {
"type": "date"
},
"match_mapping_type": "string",
"match": "date_*"
}
},
{
"strings": {
"mapping": {
"type": "string",
"fields": {
"raw": {
"ignore_above": 256,
"index": "not_analyzed",
"type": "string"
},
"keyword": {
"type": "keyword"
}
}
},
"match_mapping_type": "string"
}
}
],
"_all": {
"enabled": false
},
"properties": {
"time": {
"index": true,
"store": true,
"type": "long"
}
}
}
}
}
2、存入樣本數(shù)據(jù)
post http://localhost:9200/test_001/test
{
"name":"forest",
"age":26,
"time":1573701009309
}
3、更改動(dòng)態(tài)模板中time的字段類型為date,并新建索引模板,同步驟1
put http://localhost:9200/_template/test
4、將test_001數(shù)據(jù)reindex到test_001_new
post http://localhost:9200/_reindex
{
"source": {
"index": "test_001"
},
"dest": {
"index": "test_001_new"
}
}
5、刪除舊索引 test_001
delete http://localhost:9200/test_001
6、將test_001_new數(shù)據(jù)reindex到test_001
post http://localhost:9200/_reindex
{
"source": {
"index": "test_001_new"
},
"dest": {
"index": "test_001"
}
}