天天看點

Elasticsearch(009):es中index(索引)的新增、修改、删除、關閉等操作

Elasticsearch(009):es中index(索引)的新增、修改、删除、關閉等操作

展開

文章目錄

索引(Index)

1. 添加索引

2. 擷取索引

3. 修改索引

4. 删除索引

5. 打開/關閉索引

6. 擷取所有索引清單

本篇文章主要學習索引的相關操作。

PUT example

{

    "settings" : {

        "index" : {

            "number_of_shards" : 2, #設定分片的數量,在叢集中通常設定多個分片,表示一個索引庫将拆分成多片分别存儲不同的結點,提高了ES的處理能力和高可用性,這裡設定為2。

            "number_of_replicas" : 1 #設定副本的數量,設定副本是為了提高ES的高可靠性,這裡設定成設定為1

        }

    }

}

傳回值

  "acknowledged" : true, #表示建立成功

  "shards_acknowledged" : true,

  "index" : "example"

當然還有不止一個參數針對Index,更多的可以參考這裡。 todo

GET example

  "example" : {

    "aliases" : { },

    "mappings" : { },

      "index" : {

        "creation_date" : "1573387465030",

        "number_of_shards" : "2",

        "number_of_replicas" : "1",

        "uuid" : "yw-ZmC4ATjeukZb6N-ub8A",

        "version" : {

          "created" : "6050499"

        },

        "provided_name" : "example"

      }

  }

上面的示例擷取名為的索引的資訊example。需要指定索引,别名或通配符表達式。

通過使用_all或*作為索引,get index API也可以應用于多個索引,或者應用于所有索引。

修改example索引的max_result_window的值,調大一些。預設是10000。使用ES的人肯定知道其分頁在超過10000條資料後會報錯。

Result window is too large, from + size must be less than or equal to: [10000] but was [78020]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.

1

是以這裡我們以此為例來動态修改其值,來解決這個問題。

PUT example/_settings

  "index.max_result_window": 1000000000

傳回結果

  "acknowledged" : true

我們通過剛剛的查詢方法,擷取索引,來檢視我們的修改操作是否已經生效。

DELETE example

這時我們通過查詢索引方法就會報錯。如下

  "error" : {

    "root_cause" : [

      {

        "type" : "index_not_found_exception",

        "reason" : "no such index",

        "index_uuid" : "_na_",

        "resource.type" : "index_or_alias",

        "resource.id" : "example",

        "index" : "example"

    ],

    "type" : "index_not_found_exception",

    "reason" : "no such index",

    "index_uuid" : "_na_",

    "resource.type" : "index_or_alias",

    "resource.id" : "example",

    "index" : "example"

  },

  "status" : 404

打開和關閉索引API允許先關閉索引,然後再打開索引。封閉索引幾乎沒有叢集開銷(除了維護其中繼資料),并且被禁止進行讀/寫操作。可以打開一個封閉的索引,然後将通過正常的恢複過程。

REST端點為/{index}/_close和/{index}/_open。例如

#關閉索引

POST /example/_close

2

#打開索引

POST /example/_open

  "acknowledged" : true,

  "shards_acknowledged" : true

#擷取所有索引清單

GET _all

其實建立索引時也可以同時建立映射,也可以後面添加建立。我們下一小節将研究映射的問題。

————————————————

版權聲明:本文為部落客「瘦子沒有夏天」的原創文章,遵循 CC 4.0 BY-SA 版權協定,轉載請附上原文出處連結及本聲明。