1. 程式人生 > >Elasticsearch索引的CRUD操作

Elasticsearch索引的CRUD操作

目錄

1 建立索引

  • 建立語法:

    PUT index
    {
        "settings": { ... any settings ... },
        "mappings": {
            "type1": { ... any mappings ... },
            "type2": { ... any mappings ... },
            ...
        }
    }
  • 建立示例:

    PUT address
    {
        "settings": {
            "number_of_shards": 1,        // 預設分片數為5 (複製時請去掉註釋)
          "number_of_replicas": 0         // 預設副本數為1
        },
        "mappings": {
          "province": {
              "properties": {
                  "description": {
                      "type": "text"
                  }
                  }
          }
          }
    }
  • 響應資訊:

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

2 檢視索引

  • 檢視示例:

    GET address
    
    // 也可同時檢視多個索引, 類似於刪除操作: 
    GET *
    GET _all
    GET index_*
    GET address,shop
    
    // 也可指定返回某些指定項的結果:
    GET address/_settings,_mappings
  • 棄用提示: 檢視索引時, 若通過","分隔要返回的結果, Elasticsearch將丟擲如下錯誤:

    #! Deprecation: Requesting comma-separated features is deprecated and will be removed in 6.0+, retrieve all features instead.

    意為: Elasticsearch不推薦使用逗號分隔功能, 將在6.0+中刪除. 建議不要使用",", 而是直接檢索全部資料, 或檢索某一項的結果.

  • 響應資訊:

    {
      "address": {
        "aliases": {},
        "mappings": {
          "province": {
            "properties": {
              "description": {
                "type": "text"
              }
            }
          }
        },
        "settings": {
          "index": {
            "creation_date": "1542108754899",
            "number_of_shards": "1",
            "number_of_replicas": "0",
            "uuid": "MMpLNHzZR8K1k48rJplWVw",
            "version": {
              "created": "5061099"
            },
            "provided_name": "address"
          }
        }
      }
    }
  • 索引不存在異常:

    如果要檢視的索引不存在, 將丟擲如下異常資訊:

    {
      "error": {
        "root_cause": [
          {
            "type": "index_not_found_exception",
            "reason": "no such index",
            "resource.type": "index_or_alias",
            "resource.id": "addre",
            "index_uuid": "_na_",
            "index": "addre"
          }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index",
        "resource.type": "index_or_alias",
        "resource.id": "addre",
        "index_uuid": "_na_",
        "index": "addre"
      },
      "status": 404
    }

3 修改索引

  • 修改示例:

    PUT address/_settings
    {
        "number_of_replicas": 1       // number_of_shards引數只能在建立索引時設定, 不支援修改
    }

4 刪除索引

刪除索引需要指明索引名稱、別名或萬用字元.

Elasticsearch支援同時刪除多個索引, 或使用 _all萬用字元* 刪除全部索引.

  • 刪除示例:

    DELETE address            // 刪除指定索引
    DELETE index1,index2      // 刪除多個索引
    DELETE index_*            // 按萬用字元刪除以'index_'開頭的索引
    DELETE _all               // 刪除全部索引
  • 為避免誤刪除全部索引, 可在配置檔案elasticsearch.yml中作如下配置:

    action.destructive_requires_name: true  # 要求操作索引時必須指定索引的名稱

5 開啟/關閉索引

只能關閉一個開啟的索引;

只能開啟一個已經關閉的索引;

關閉的索引只能顯示索引元資料, 不能進行讀寫操作.

  • 操作示例:

    POST address/_close
    POST address/_open
    
    // 可以使用_all開啟或關閉全部索引, 也可使用萬用字元(*)配合操作
  • 注意事項:

    • 修改已經關閉了的索引, 將丟擲如下錯誤:

      {
        "error": {
          "root_cause": [
            {
              "type": "illegal_argument_exception",
              "reason": "Can't update [index.number_of_replicas] on closed indices [[address/MMpLNHzZR8K1k48rJplWVw]] - can leave index in an unopenable state"
            }
          ],
          "type": "illegal_argument_exception",
          "reason": "Can't update [index.number_of_replicas] on closed indices [[address/MMpLNHzZR8K1k48rJplWVw]] - can leave index in an unopenable state"
        },
        "status": 400
      }
    • 使用_all或萬用字元標註索引的操作, 也會受到[刪除索引]中 action.destructive_requires_name=true 的限制.

    • 關閉的索引會繼續佔用磁碟空間, 卻又不能使用 —— 造成磁碟空間的浪費.

    • 可以在配置檔案中禁止使用關閉索引的功能: settingscluster.indices.close.enable=true, 預設為true, 即開啟

版權宣告

作者: ma_shoufeng(馬瘦風)

您的支援是對博主的極大鼓勵, 感謝您的閱讀.

本文版權歸博主所有, 歡迎轉載, 但未經博主同意必須保留此段宣告, 且在文章頁面明顯位置給出原文連結, 否則博主保留追究相關人員法律責任的權利.