Elasticsearch: Shard & Replica Sayılarını Ayarlama

Neslihan Esra Altınışık
2 min readJan 3, 2019

--

Aşağıda indexlerin, shard veya replica sayılarının değiştirilebileceği durumlar yer almaktadır.

Index oluştururken;

İndex oluşturulduğu zaman shard ve replicaların sayısı belirtilebilir, index oluşturulduktan sonra dinamik olarak replica sayısı değiştirilebilir ama shard sayısı değiştirilememektedir. Eğer bu ayar yapılmazsa varsayılan olarak index oluşturulduğunda 5 primary shard ve her shard için 1 replica shard oluşturulur.

curl -XPUT "http://elastic_ip:elastic_port/index_adi" -H 'Content-Type: application/json' -d'
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
}'

Template oluştururken;

Yeni indexe uygulamak için settings ve mappings işlemlerinden kapsayan template oluşturulurken de shard ve replica sayısı belirtilebilir.

curl -XPUT "http://elastic_ip:elastic_port/_template/template_adi" -H 'Content-Type: application/json' -d'
{
"index_patterns": ["apilogs*", "instancemetrics*"],
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"_doc": {
"_source": {
"enabled": true
}
}
}
}'

Replica Sayısını Güncelleme

İstekte yer alan {index_adi} parametresi yerine bir veya daha fazla index ismi yazılabilir. Eğer parametreye bir değer girilmezse işlem tüm indexlere uygulanır.

curl -XPUT "http://elastic_ip:elastic_port/{index_adi}/_settings" -H 'Content-Type: application/json' -d'
{
"index": {
"number_of_replicas": 1
}
}'

Shrink API

  • Bu API ile, mevcut indexin bulunduğu shard sayısı, yeni bir index oluşturarak azaltılır.
  • Shrinking işleminden önce indexlerin bulunduğu primary ve replica shardların kopyası aynı node da bulunmalıdır.
# adım 1 – Küçültme işlemi yapılmadan önce index sadece okunabilir olmalı, tüm shardlar bir node üzerinde toplanmalı ve cluster’ın durumu yeşil olmalıdır.
curl -XPUT "http://elastic_ip:elastic_port/instancemetrics20181221/_settings" -H 'Content-Type: application/json' -d'
{
"settings": {
"index.routing.allocation.require._name": "node_ismi",
"index.blocks.write":true
}
}'
# adım 2 – shard ve replica sayısını azaltma
curl -XPOST "http://elastic_ip:elastic_port/instancemetrics20181221/_shrink/my_target_index" -H 'Content-Type: application/json' -d'
{
"settings": {
"index.number_of_replicas": 1,
"index.number_of_shards": 3,
"index.codec": "best_compression"
}
}'

Reindex API

Yeniden indexleme yaparken shard ve replica sayısı değiştirilebilir.

# adım 1 – yeni index oluşturma
curl -XPUT "http://elastic_ip:elastic_port/yeni_index_adi" -H 'Content-Type: application/json' -d'
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
}'
# adım 2 – yeniden indexleme
curl -XPOST "http://elastic_ip:elastic_port/_reindex" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "mevcut_index_adi"
},
"dest": {
"index": "yeni_index_adi"
}
}'

--

--