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

1. 建立索引

PUT /lujiahao123
{
  "acknowledged": true,
  "shards_acknowledged": true
}
索引默認的主分片數量是5,每一個主分片的副本數量是1。
複製代碼

建立自定義字段類型的索引:git

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
}
複製代碼

更新分詞器github

POST /order/_close
PUT /order/_settings
{
  "analysis": {
    "analyzer": {
      "content":{
        "type":"customer",
        "tokenizer":"whitespace"
      }
    }
  }
}
POST /order/_open
添加分析器以前必須先關閉索引,添加以後再打開索引。
複製代碼

嘗試修改索引主分片數:bash

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

錯誤提示:app

{
  "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刪除索引。elasticsearch

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"
      }
    }
  }
}
複製代碼

自定義返回結果的屬性:ide

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. 打開/關閉索引

索引關閉只能顯示索引元數據信息,不可以進行讀寫。ui

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。spa

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
}
複製代碼

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

Tips

本文同步發表在公衆號,歡迎你們關注!😁 後續筆記歡迎關注獲取第一時間更新! blog

相關文章
相關標籤/搜索