《Elasticsearch技術解析與實戰》Chapter 2.1 Elasticsearch索引增刪改查

莊裡程式猿發表於2019-04-17

1. 建立索引

PUT /lujiahao123
{
  "acknowledged": true,
  "shards_acknowledged": true
}
索引預設的主分片數量是5,每個主分片的副本數量是1。
複製程式碼

建立自定義欄位型別的索引:

PUT /order
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "carpoolType":{
      "properties": {
        "orderNo":{
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}
#! Deprecation: The [string] field is deprecated, please use [text] or [keyword] instead on [orderNo]
{
  "acknowledged": true,
  "shards_acknowledged": true
}
ES5.0版本中string型別被拆分為text和keyword型別: https://www.elastic.co/blog/strings-are-dead-long-live-strings
複製程式碼

2. 修改索引

PUT /order/_settings
{
  "number_of_replicas": 2
}
複製程式碼

更新分詞器

POST /order/_close
PUT /order/_settings
{
  "analysis": {
    "analyzer": {
      "content":{
        "type":"customer",
        "tokenizer":"whitespace"
      }
    }
  }
}
POST /order/_open
新增分析器之前必須先關閉索引,新增之後再開啟索引。
複製程式碼

嘗試修改索引主分片數:

PUT /order/_settings
{
  "number_of_shards": 2
}
複製程式碼

錯誤提示:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[order/2cOJ6Ga7THCyW10idoPPig]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[order/2cOJ6Ga7THCyW10idoPPig]]"
  },
  "status": 400
}

主分片是無法修改的,主分片個數一旦建立就無法修改,但是看錯誤原因裡面說因為這個是open的索引,那是不是關閉的索引就可以修改主分片個數了呢?這個我們後面再解答。
複製程式碼

3. 刪除索引

刪除單個索引:
DELETE /order
刪除索引需要指定索引名稱,別名或者萬用字元
複製程式碼
刪除多個索引:
DELETE order,order1,order2
DELETE order*
複製程式碼
刪除全部索引:
DELETE _all
DELETE *
複製程式碼

使用_all 和萬用字元刪除全部索引 為了防止誤刪除,設定config/elasticsearch.yml屬性action.destructive_requires_name=true,以禁用萬用字元或_all刪除索引。

4. 查詢索引

GET /order
{
  "order": {
    "aliases": {},
    "mappings": {
      "carpoolType": {
        "properties": {
          "orderNo": {
            "type": "keyword"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1555054886098",
        "number_of_shards": "3",
        "number_of_replicas": "2",
        "uuid": "uSh9K26CS_q1uZKaos7NRQ",
        "version": {
          "created": "5050099"
        },
        "provided_name": "order"
      }
    }
  }
}
複製程式碼

自定義返回結果的屬性:

GET /order/_settings,_aliases
{
  "order": {
    "settings": {
      "index": {
        "creation_date": "1555054886098",
        "number_of_shards": "3",
        "number_of_replicas": "2",
        "uuid": "uSh9K26CS_q1uZKaos7NRQ",
        "version": {
          "created": "5050099"
        },
        "provided_name": "order"
      }
    },
    "aliases": {}
  }
}
複製程式碼

5. 開啟/關閉索引

索引關閉只能顯示索引後設資料資訊,不能夠進行讀寫。

POST /order/_close
POST /order/_open
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana 8n5wnGEjRJ-aVa54l_jTjA   1   1          1            0      3.1kb          3.1kb
yellow open   order   uSh9K26CS_q1uZKaos7NRQ   3   2          0            0       486b           486b
複製程式碼

可以同時開啟或關閉多個索引。如果指向不存在的索引會丟擲錯誤,可通過配置ignore_unavailable=true 關閉異常提示。 禁止使用關閉索引功能,配置settingscluster.indices.close.enable為false,預設值是ture。

6. 關閉索引後嘗試修改主分片個數

PUT /order/_settings
{
  "number_of_shards": 2
}
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "final order setting [index.number_of_shards], not updateable"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "final order setting [index.number_of_shards], not updateable"
  },
  "status": 400
}
複製程式碼

解答了之前的問題,索引一旦建立,主分片數量不可改變。

Tips

本文同步發表在公眾號,歡迎大家關注!? 後續筆記歡迎關注獲取第一時間更新!

《Elasticsearch技術解析與實戰》Chapter 2.1 Elasticsearch索引增刪改查

相關文章