LoginSignup
5
5

More than 5 years have passed since last update.

Elasticsearch 6.2.2 で インデックス作成時にシャードやレプリカの数を設定する

Posted at

デフォルトの設定でインデックスを作成するとシャード数 5 でレプリカ数 1 となり、単一ノードだとクラスタのステータスが yellow になります。
ステータスが yellow の場合は一部のレプリカが割り当てられていないことを表しています。

$ curl -X PUT 'localhost:9200/shop'
$ curl -X GET 'localhost:9200/_cluster/health'

上記のように実行すると

{
  "cluster_name": "elasticsearch",
  "status": "yellow",
  "timed_out": false,
  "number_of_nodes": 1,
  "number_of_data_nodes": 1,
  "active_primary_shards": 5,
  "active_shards": 5,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 5,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks": 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 50
}

となっており、active_primary_shards が 5 で、unassigned_shards が 5 になっています。

シャードの情報を取得すると、

$ curl -X GET 'localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason'

以下のようにレプリカが設定されていないことが判ります。

shop 3 p STARTED    
shop 3 r UNASSIGNED INDEX_CREATED
shop 1 p STARTED    
shop 1 r UNASSIGNED INDEX_CREATED
shop 2 p STARTED    
shop 2 r UNASSIGNED INDEX_CREATED
shop 4 p STARTED    
shop 4 r UNASSIGNED INDEX_CREATED
shop 0 p STARTED    
shop 0 r UNASSIGNED INDEX_CREATED

このような場合、以下のようにしてシャード数やレプリカ数を設定して調整することができます。(ここではレプリカ数を0 にしています)

$ curl -X PUT 'localhost:9200/shop' -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 1,
            "number_of_replicas" : 0
        }
    }
}
'

これでクラスタのステータスは green になりました。

{
  "cluster_name": "elasticsearch",
  "status": "green",
  "timed_out": false,
  "number_of_nodes": 1,
  "number_of_data_nodes": 1,
  "active_primary_shards": 1,
  "active_shards": 1,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 0,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks": 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 100
}
shop 0 p STARTED 

なお、この設定は elasticsearch.yml には記載できませんでした。
ヘルスチェック結果についての項目は こちら にまとめました。

参考になった記事

Create Index
cat shards

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5