How to Query number of Routing Shards?

How can I find out the number of routing shards an index was created with? I tried using the Get Index Settings API, but it doesn't show up there:

# Create the Index
PUT twitter
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_routing_shards": 20
    }
  }
}

# Get the settings
GET twitter/_settings

gives me this result:

{
  "twitter" : {
    "settings" : {
      "index" : {
        "creation_date" : "1578512730838",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "ZQ5h-HjdS3mFojrrpc_3GA",
        "version" : {
          "created" : "7050199"
        },
        "provided_name" : "twitter"
      }
    }
  }
}

which doesn't tell me the number of routing shards. I even tried querying the setting directly, but GET twitter/_settings/index.number_of_routing_shards just returns { }

I'm using elasticearch 7.5.1

try using include_defaults=true as a parameter

That gave me a response, but curiously it returns 1 as the answer.

GET twitter/_settings/index.number_of_routing_shards?include_defaults=true
{
  "twitter" : {
    "settings" : { },
    "defaults" : {
      "index" : {
        "number_of_routing_shards" : "1"
      }
    }
  }
}

Hm, indeed. This always seems to be "1", which is a fallback, but it should actually work with a multipler of the number of shards. Let me check that tomorrow and come back to you.

1 Like

I found https://github.com/elastic/elasticsearch/issues/33036 telling that the setting is not exposed in the index settings. The issue however is still open.

Fair enough, thanks @spinscale!

Looks like a possible workaround mentioned in that issue is to call the _split API with an invalid number of target shards, and the error response will include the number of routing shards.
So in this example you could try

POST twitter/_split/target-index
{
  "settings": {
    "index.number_of_shards": 15
  }
}
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_state_exception",
        "reason": "the number of routing shards [20] must be a multiple of the target shards [15]"
      }
    ],
    "type": "illegal_state_exception",
    "reason": "the number of routing shards [20] must be a multiple of the target shards [15]"
  },
  "status": 500
}

The problem is you could potentially pick a valid number and split the index. To be safer, you could try setting index.number_of_shards to a very high number, making it unlikely that the number of routing shards is a multiple. Probably a better solution is to just use templates and trust the setting in the template is correct.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.