LoginSignup
3
4

More than 5 years have passed since last update.

[Elasticsearch]インデックス作成時のシャード数を変更してみる

Posted at

バージョン

  • Elasticsearch 2.3.4

やってみる

Shards & Replicas

By default, each index in Elasticsearch is allocated 5 primary shards and 1 replica which means that if you have at least two nodes in your cluster, your index will have 5 primary shards and another 5 replica shards (1 complete replica) for a total of 10 shards per index.

上記にも記載があるようにデフォルトでは 5プライマリシャード、1レプリカシャードが作られるようになっているようです。
試しに作ってみます。

# インデックス作成
$curl -s -XPOST http://10.66.35.30:9200/test_index | jq
{
  "acknowledged": true
}

# インデックスの設定確認
$curl -s http://10.66.35.30:9200/test_index/_settings | jq
{
  "test_index": {
    "settings": {
      "index": {
        "creation_date": "1469840401660",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "SrrZsLTAQhKSJ5LSgjiEPQ",
        "version": {
          "created": "2030499"
        }
      }
    }
  }
}

# 確認後削除
$curl -s -XDELETE http://10.66.35.30:9200/test_index | jq
{
  "acknowledged": true
}

確かにドキュメント通りの設定になっています。

この設定を変えるために予め Index Template を設定します。

Index Template

今回は setting のみのテンプレートを定義しますが、mappingのテンプレートも行えるようです。
シャード数を1とするテンプレートを作成します。

# テンプレート登録
$curl -XPUT localhost:9200/_template/template_1 -d '
{
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1
    }
}'
{"acknowledged":true}


# テンプレート確認
$curl -s http://10.66.35.30:9200/_template/template_1 |jq
{
  "template_1": {
    "order": 0,
    "template": "te*",
    "settings": {
      "index": {
        "number_of_shards": "1"
      }
    },
    "mappings": {},
    "aliases": {}
  }
}

正しく登録されているようなので再度インデックスを登録してみます。

$curl -s -XPOST http://10.66.35.30:9200/test_index | jq
{
  "acknowledged": true
}

$curl -s http://10.66.35.30:9200/test_index/_settings | jq
{
  "test_index": {
    "settings": {
      "index": {
        "creation_date": "1469842187701",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "Qn8vIieWQ0eNIv9iK6fWQw",
        "version": {
          "created": "2030499"
        }
      }
    }
  }
}

number_of_shards が変わってますね。無事できました!

不要になったのでインデックス、テンプレートも一応削除しておきましょう。

$curl -s -XDELETE http://10.66.35.30:9200/test_index

$curl -s -XDELETE http://10.66.35.30:9200/_template/template_1
3
4
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
3
4