查询原有mapping
登录kibana的Index Management 查看 Mapping
{ "mapping": { "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "create_time": { "type": "long" }, "discuss_num": { "type": "long" }, "hot_num": { "type": "long" }, "hot_time": { "type": "date" }, "id": { "type": "long" }, "is_hot": { "type": "long" }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "topic_sort": { "type": "long" }, "type": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "update_at": { "type": "date" } } } }
设置新的index和mapping (比如修改l_id为text类型,keyword分词,注意修改 mapping为mappings)
注意:我们这里把is_top设置为text keyword 因为看上去是数字,但是其实是精准查询
PUT topic_v1 { "mappings": { "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "create_time": { "type": "long" }, "discuss_num": { "type": "long" }, "hot_num": { "type": "long" }, "hot_time": { "type": "date" }, "id": { "type": "long" }, "is_hot": { "type": "text", "fielddata": true, "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "topic_sort": { "type": "long" }, "type": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "update_at": { "type": "date" } } } }
修改logstash同步配置文件,把mysql可能为空字符串的字段设置为 none
select id,title,is_hot,hot_time,topic_sort,hot_num ,discuss_num,unix_timestamp(created_at) as create_time,update_at,(CASE state WHEN '' THEN 'none' ELSE state END) as es_state from topic wher e update_at >=:sql_last_value order by update_at asc,id asc
创建索引别名,下次需要重建索引的时候,直接把新的索引别名设置为topic_alias就可以不用动代码,平滑切换了
PUT /topic_v1/_alias/topic_alias
查看别名都指向哪些索引
GET /*/_alias/topic_alias
验证新的别名功能都正常后,可以平滑切换索引,删除旧索引的别名,添加新索引的别名
POST /_aliases { "actions": [{ "remove": { "index": "topic_v1", "alias": "topic_alias" } }, { "add": { "index": "topic_v2", "alias": "topic_alias" } }] }