# Need help deciding how to partition data

**URL:** https://discuss.elastic.co/t/need-help-deciding-how-to-partition-data/350837
**Category:** Elasticsearch
**Created:** [January 11, 2024, 8:34am UTC](https://discuss.elastic.co/t/need-help-deciding-how-to-partition-data/350837 "2024-01-11T08:34:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![favoca](https://avatars.discourse-cdn.com/v4/letter/f/5daacb/32.png) [@favoca](https://discuss.elastic.co/u/favoca)
#### Post date: [January 11, 2024, 8:34am UTC](https://discuss.elastic.co/t/need-help-deciding-how-to-partition-data/350837/1 "2024-01-11T08:34:46Z")

</div>

The document in my RDMS has a schema similar to this:

```auto
{
PatientId: "string",
Date: "date",
IsAvailable: "bool",
_hospitalId: "6-digit number which can be a number or a string"
}

```

The \_hospitalId acts like a partition key. All search queries will take place within the same \_hospitalId. There are about 10,000 hospitals, with each hospital having between 100,000 to millions of patients.

What is the best way to divide the data in Elasticsearch? The ideal scenario would be to have each hospital as a separate index, but this many indexes is not recommended. How should I go about this?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 11, 2024, 8:44am UTC](https://discuss.elastic.co/t/need-help-deciding-how-to-partition-data/350837/2 "2024-01-11T08:44:05Z")

</div>

If you always, or in most cases, are querying with a filter on a specific field and the cardinality of this field is reasonably large and the number of documents per field value is reasonably well distributed (which seems to be the case in your scenario) you can use a single index combined with [index routing](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/search-shard-routing.html).

You set up an index with a reasonable number of primary shards, and we will for this example assume 10 primary shards gives you a good shard size. Whe you index a document you use the hospitalId as raouting key, which means all documents for that hospital will go to the same shard. The shard will contain documents for multiple hospitals though, which makes this approach much more efficient than single index per hospital.

When you query for a single hospital you also provide the hostpitalId as routing key, and this allows Elasticsearch to only query one of the 10 shards as it knows all relevant data is located there.

If you have 10 primary shards only 10% of the data is queried for each request. If you have 20 primary shards only 5% of the data is queries etc.

When you use routing it is important to not have too many primary shards and be aware that the shard size may not be uniform as it is possible some of the larger hospitals may end up in the same shard (which is why you want to consider cardinality and distribution of data set size).

---

<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: [February 8, 2024, 8:44am UTC](https://discuss.elastic.co/t/need-help-deciding-how-to-partition-data/350837/3 "2024-02-08T08:44:16Z")

</div>

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