Elasticsearch 7.x:2、索引管理

程裕強發表於2019-01-07

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/index.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

2.1 索引名規範

索引命名有如下限制:

  • 僅限小寫字母
  • 不能包含\/*?"<>|、#以及空格符等特殊符號
  • 從7.0版本開始不再包含冒號
  • 不能以-_+開頭
  • 不能超過255個位元組(注意它是位元組,因此多位元組字元將計入255個限制)

2.2 新建索引

(1)索引名小寫

PUT test
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test"
}

在這裡插入圖片描述

(2)索引名不能包含大些字母

PUT Blog

相應結果:

{
  "error": {
    "root_cause": [
      {
        "type": "invalid_index_name_exception",
        "reason": "Invalid index name [Blog], must be lowercase",
        "index_uuid": "_na_",
        "index": "Blog"
      }
    ],
    "type": "invalid_index_name_exception",
    "reason": "Invalid index name [Blog], must be lowercase",
    "index_uuid": "_na_",
    "index": "Blog"
  },
  "status": 400
}

(3)重複建立索引

PUT test

相應結果:

{
  "error": {
    "root_cause": [
      {
        "type": "resource_already_exists_exception",
        "reason": "index [test/XBKm9hVxSQGP6KTncA10WA] already exists",
        "index_uuid": "XBKm9hVxSQGP6KTncA10WA",
        "index": "test"
      }
    ],
    "type": "resource_already_exists_exception",
    "reason": "index [test/XBKm9hVxSQGP6KTncA10WA] already exists",
    "index_uuid": "XBKm9hVxSQGP6KTncA10WA",
    "index": "test"
  },
  "status": 400
}

2.3 索引配置

建立索引時,可以制定相關設定,比如設定索引的分片數number_of_shards和副本數number_of_replicas

PUT blog
{
    "settings" : {
        "index" : {
            "number_of_shards" : 2,
            "number_of_replicas" : 2
        }
    }
}

也可以簡化為

PUT blog
{
    "settings" : {
        "number_of_shards" : 2,
        "number_of_replicas" : 2
    }
}

也就是說,不必在settings部分中明確指定索引部分。

相應結果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "blog"
}

2.4 檢視索引

(1)檢視制定索引

GET blog

相應結果:

{
  "blog" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1547012455291",
        "number_of_shards" : "2",
        "number_of_replicas" : "2",
        "uuid" : "RorVlzACQIOpGDCW9LJ1cA",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "blog"
      }
    }
  }
}

(2)檢視索引列表

GET /_cat/indices?v

相應結果如下,可以看到兩個剛剛建立的索引,.kibana_1是Kibana自帶樣例索引。

health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   blog      RorVlzACQIOpGDCW9LJ1cA   2   2          0            0       522b           522b
green  open   .kibana_1 vfaeT7wQSgyxXOlZioCFDQ   1   0          3            0     11.9kb         11.9kb
yellow open   test      k4WAeNOqSpGzQbO3euWy2w   1   1          0            0       230b           230b

需要注意:我們新建的索引,預設分片和副本都是1。

(3)判定索引是否存在

HEAD blog

相應結果:

200 - OK

2.5 更新副本數

PUT blog/_settings
{
  "number_of_replicas": 1
}

相應結果:

{
  "acknowledged" : true
}

2.6 檢視索引配置資訊

GET blog/_settings

相應結果:

{
  "blog" : {
    "settings" : {
      "index" : {
        "creation_date" : "1547012455291",
        "number_of_shards" : "2",
        "number_of_replicas" : "1",
        "uuid" : "RorVlzACQIOpGDCW9LJ1cA",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "blog"
      }
    }
  }
}

2.7 刪除索引

可以直接使用DELETE命令刪除索引

DELETE test

相應結果:

{
  "acknowledged" : true
}

2.8 索引的關閉與開啟

一個關閉的索引幾乎不佔用系統資源。我們可以臨時關閉某個索引,在需要時再重新開啟該索引。
(1)關閉blog

POST blog/_close

相應結果:

{
  "acknowledged" : true
}

(2)檢視索引

GET /_cat/indices?v

相應結果:

health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
       close  blog      RorVlzACQIOpGDCW9LJ1cA                                                          
green  open   .kibana_1 vfaeT7wQSgyxXOlZioCFDQ   1   0          3            0     11.9kb         11.9kb

(3)重新開啟blog

POST blog/_open

相應結果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true
}

2.9 指定type的mapping

創新索引時,允許提供一個type的對映。

創新建立索引test,並制定預設_doc型別的mappings

PUT test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

相應結果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test"
}

2.10 索引別名

索引別名不僅僅可以關聯一個索引,它能聚合多個索引。此外,一個別名也可以與一個過濾器相關聯, 這個過濾器在搜尋和路由的時候被自動應用。

(1)建立多個索引

PUT index1
PUT index2

(2)建立index1的別名alias1

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "index1",
        "alias": "alias1"
      }
    }
  ]
}
{
  "acknowledged" : true
}

說明:此時別名alias1和index1一對一。

(3)新增多個索引的別名

POST _aliases
{
  "actions": [
    {
      "add": {
        "indices": ["index2","test"],
        "alias": "alias1"
      }
    }
  ]
}
{
  "acknowledged" : true
}

說明:我們是不能對alias1進行寫操作,當有多個索引時的別名,不能區分到底操作哪一個索引。

(4)移除別名

POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "test",
        "alias": "alias1"
      }
    }
  ]
}
{
  "acknowledged" : true
}

(5)檢視別名

GET alias1
{
  "index1" : {
    "aliases" : {
      "alias1" : { }
    },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1547013859010",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "sbbArnOvTYqf07rXlTpFtA",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "index1"
      }
    }
  },
  "index2" : {
    "aliases" : {
      "alias1" : { }
    },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1547013863297",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "qqRkUXOcQfuBTf1eBqvMPA",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "index2"
      }
    }
  }
}

相關文章