# Custom routing above shard count

**URL:** <https://discuss.elastic.co/t/custom-routing-above-shard-count/34028>\
**Category:** Elasticsearch\
**Created:** [November 6, 2015, 5:58pm UTC](https://discuss.elastic.co/t/custom-routing-above-shard-count/34028 "2015-11-06T17:58:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hwang251](https://avatars.discourse-cdn.com/v4/letter/h/2acd7d/32.png) [@hwang251](https://discuss.elastic.co/u/hwang251)\
**Post date:** [November 6, 2015, 5:58pm UTC](https://discuss.elastic.co/t/custom-routing-above-shard-count/34028/1 "2015-11-06T17:58:40Z")

</div>

I'm developing a multi-tenant system and am planning to use custom routing to separate each tenant's data. I created an index with 5 shards. I was wondering what will happen once add a sixth tenant. If I send a query routing to one of the tenants, will the data returned be only from that tenant or will it contain another tenant's data because there are 6 routes with 5 shards?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [November 6, 2015, 6:13pm UTC](https://discuss.elastic.co/t/custom-routing-above-shard-count/34028/2 "2015-11-06T18:13:27Z")

</div>

Routing is simply `hash(routing_value) % num_shards`. Routing guarantees that all of one tenant's data goes to a single shard, but it doesn't guarantee that other data won't also land there.

In-fact, it's possible that your first two tenants may share the same shard even though there are 5 total shards, just because they happen to hash to the same location.

When custom routing, you will always need some kind of "exclusionary filter" that returns the data you are interested in. So usually, you'll have routing + filter on the user's routing value, group, ID, etc.

```json
GET /tenants/data/_search?routing=userABC
{
  "query" : {
    "filtered" : {
      "query": { ... },
      "filter": {
         "term": {
            "username": "userABC"
         }
      }
    }
  }
}

```

See [https://www.elastic.co/guide/en/elasticsearch/guide/current/routing-value.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/routing-value.html) and [https://www.elastic.co/guide/en/elasticsearch/guide/current/user-based.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/user-based.html) for some more details.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:40pm UTC](https://discuss.elastic.co/t/custom-routing-above-shard-count/34028/3 "2017-07-05T23:40:05Z")

</div>


