Elasticsearch 第四篇:索引别名、添加或修改映射规则

项目中经常出现的情景,例如Elasticsearch 服务搭建好了,也建立了索引,但是现有字段不合适或者需要添加字段、修改字段,那就需要根据新的映射规则,重建索引库。最好是项目一开始搭建时,就给索引库一个别名,当需要修改字段时,只需要新增映射,创建新的索引库,然后将别名指向新的索引库,当然需要将之前的索引搬迁到新的索引库当中。

1、获取映射信息(例如索引库是db_student)

GET  http://localhost:9200/db_student/_mapping

这时可以看到返回结果:

{
    "db_student": {
        "mappings": {
            "properties": {
                "chinese": {
                    "type": "integer",
                    "store": true
                },
                "class": {
                    "type": "integer",
                    "store": true
                },
                "english": {
                    "type": "integer",
                    "store": true
                },
                "math": {
                    "type": "integer",
                    "store": true
                },
                "name": {
                    "type": "text",
                    "store": true
                },
                "school": {
                    "type": "text",
                    "store": true,
                    "analyzer": "ik_max_word"
                }
            }
        }
    }
}

这正是上一篇我们设置的映射规则,现在我们想要增加一列 desc ,用来保存学生的自我介绍介绍信息,首先,我们可以为 索引库 db_student 取一个别名  student 。

2、为旧索引库   db_student  起一个别名  student

PUT   http://localhost:9200/db_student/_alias/student

此时,再查一下就索引库有哪些别名:

GET  http://localhost:9200/db_student/_alias

呈现的效果是:

{
    "db_student": {
        "aliases": {
            "student": {}
        }
    }
}

此时,别名已经生效。

3、建立新的索引库  db_student_v1,在原索引库的基础上加上 desc 这一新的字段,并且使用 ik 分词

PUT  http://localhost:9200/db_student_v1
{ 
    "mappings": { 
            "properties": { 
                "class": { 
                    "type": "integer", 
                    "store": true, 
                    "index":true
                },
                "chinese": { 
                    "type": "integer", 
                    "store": true, 
                    "index":true
                },
                "english": { 
                    "type": "integer", 
                    "store": true, 
                    "index":true
                },
                "math": { 
                    "type": "integer", 
                    "store": true, 
                    "index":true
                },
                "name": { 
                    "type": "text", 
                    "store": true, 
                    "index":true
                },
                "school": { 
                    "type": "text", 
                    "store": true, 
                    "index":true, 
                    "analyzer":"ik_max_word" 
                },
                "desc": { 
                    "type": "text", 
                    "store": true, 
                    "index":true, 
                    "analyzer":"ik_max_word" 
                }
            } 
    } 
}

4、数据搬迁,现在将索引库 db_student  的数据搬到新的索引库  db_student_v1

POST  http://localhost:9200/_reindex
{
  "conflicts": "proceed",
  "source": {
    "index": "db_student"
  },
  "dest": {
    "index": "db_student_v1",
    "op_type": "create",
    "version_type": "external"
  }
}

5、更改别名,将旧版索引库 db_student 的别名去除,将别名指向新版的索引库 db_student_v1

去除旧版索引库 db_student 的别名:

POST  http://localhost:9200/_aliases
{
  "actions": [
    {
      "remove": {
        "index": "db_student",
        "alias": "student"
      }  
    }
  ]
}

为新版索引库 db_student_v1 加上别名:

POST  http://localhost:9200/_aliases
{
  "actions": [
    {
     
      "add": {
        "index": "db_student_v1",
        "alias": "student"
      }
    }
  ]
}

此时就可以统一按照别名 student 来进行上一篇的所有查询动作,例如我们新增一个文档(此文档包含desc这个新的字段,插入的索引库是 student ,指向 db_student_v1 ):

PUT  http://localhost:9200/student/_doc/9
{
"chinese":80,
"class":10,
"english":90,
"math":100,
"name":"Jim",
"school":"华南理工大学",
"desc":"来自广东广州,是潮汕人,英国经济学博士"
}

打开Kibana,可以看到新数据已经是插入成功了。

以上的mapping字段的设置,还是比较的简单粗暴,典型的例如字符串,Elasticsearch 字符串的处理可以设置为 type = text 或者是 type = keyword ,区别在于text支持分词、不支持聚合,而keyword不支持分词、支持聚合,

在上面mapping中,school字段的type设置为text,这样和字段desc就没有什么区别,但实际应用中,经常会统计不同学校的各类数据,school 避免不了聚合统计,如果school的type设置为text,那么聚合就会报错,

这是可以将type设置为keyword,再重建索引,方法如上面所介绍。

posted @ 2020-08-10 10:51  vincentfhr方海荣  阅读(2883)  评论(0编辑  收藏  举报