1. 程式人生 > 實用技巧 >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 來進行上一篇的所有查詢動作。