elasticsearch中修改mapping实战

参考:https://www.phpmianshi.com/?id=200

查询原有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"
        }
    }]
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值