Elasticsearch第四篇:索引別名、添加或修改映射規則

項目中常常出現的情景,例如Elasticsearch 服務搭建好了,也創建了索引,可是現有字段不合適或者須要添加字段、修改字段,那就須要根據新的映射規則,重建索引庫。最好是項目一開始搭建時,就給索引庫一個別名,當須要修改字段時,只須要新增映射,建立新的索引庫,而後將別名指向新的索引庫,固然須要將以前的索引搬遷到新的索引庫當中。app

一、獲取映射信息(例如索引庫是db_student)spa

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

這時能夠看到返回結果:code

{
    "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 。blog

二、爲舊索引庫   db_student  起一個別名  student索引

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

此時,再查一下就索引庫有哪些別名:rem

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

呈現的效果是:文檔

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

此時,別名已經生效。io

三、創建新的索引庫  db_student_v1,在原索引庫的基礎上加上 desc 這一新的字段,而且使用 ik 分詞ast

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" 
                }
            } 
    } 
}

四、數據搬遷,如今將索引庫 db_student  的數據搬到新的索引庫  db_student_v1class

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

五、更改別名,將舊版索引庫 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,能夠看到新數據已是插入成功了。

未完,待續。 

相關文章
相關標籤/搜索