# Split data into into two sets (test/train)

**URL:** https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236
**Category:** Elasticsearch
**Created:** [May 18, 2021, 6:52am UTC](https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236 "2021-05-18T06:52:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![eliase](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@eliase](https://discuss.elastic.co/u/eliase)
#### Post date: [May 18, 2021, 6:52am UTC](https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236/1 "2021-05-18T06:52:39Z")

</div>

Hi,

for machine learning validation I would like to split the data in elastic search into two sets.  
The [random score](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/query-dsl-function-score-query.html#function-random) in combination with the size parameter would give me one set but how can I obtain all other documents? Is there an easy way to do that?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [May 18, 2021, 7:16am UTC](https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236/2 "2021-05-18T07:16:10Z")

</div>

If you use multiple shards to index the data it’s already randomly distributed across shards and you can use search [routing](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-shard-routing.html#search-routing) to query only one of several shards.

---

<div class="post-metadata">

### Author: ![eliase](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@eliase](https://discuss.elastic.co/u/eliase)
#### Post date: [May 18, 2021, 7:26am UTC](https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236/3 "2021-05-18T07:26:02Z")

</div>

Thanks for the quick reply. So far, it's only in one shard.

Even if there were multiple shards, that doesn't seem to be very flexible solution in regards to getting a specified split ratio like 20% / 80%

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [May 18, 2021, 9:01am UTC](https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236/4 "2021-05-18T09:01:49Z")

</div>

The routing function is a common hash modulo N function where N is the number of shards.  
You could apply the same logic to querying the ID of documents with a script query e.g. something like:

```auto
{
  "query": {
    "script": {
      "script": " doc['_id'].value.hashCode()%2 == 0"
    }
  }
}

```

---

<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: [June 15, 2021, 9:02am UTC](https://discuss.elastic.co/t/split-data-into-into-two-sets-test-train/273236/5 "2021-06-15T09:02:23Z")

</div>

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