# Repeatable randomness in field-collapsed results

**URL:** <https://discuss.elastic.co/t/repeatable-randomness-in-field-collapsed-results/37791>\
**Category:** Elasticsearch\
**Created:** [December 22, 2015, 8:38pm UTC](https://discuss.elastic.co/t/repeatable-randomness-in-field-collapsed-results/37791 "2015-12-22T20:38:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kderusso](https://avatars.discourse-cdn.com/v4/letter/k/f6c823/32.png) [@kderusso](https://discuss.elastic.co/u/kderusso)\
**Post date:** [December 22, 2015, 8:38pm UTC](https://discuss.elastic.co/t/repeatable-randomness-in-field-collapsed-results/37791/1 "2015-12-22T20:38:40Z")

</div>

Hi, I've been trying to understand how to perform field collapsing returning results in random sort order using 2.1.1.

My use case: I want to return a list of documents that match specific restrictions that I specify, field collapsed by a field called supplierId. I want the specific document returned for each supplierId returned in repeatable random order.

When I run the following query (in-lining scripts because I'm still learning how to run queries on my workstation, this isn't a production environment), it behaves in the manner that I'm expecting:

{  
"query": {  
"bool": {  
"must": [  
\<my restrictions\>  
]  
}  
},  
"from": 0,  
"size": 0,  
"aggs": {  
"supplierIds": {  
"terms": {  
"field": "supplierId"  
},  
"aggs": {  
"supplierDocs": {  
"top\_hits": {  
"\_source": [  
\<sourceFields\>  
],  
"size": 1,  
"sort": {  
"\_script": {  
"script": "(new Random()).nextInt()",  
"type": "number",  
"params": {},  
"order": "asc"  
}  
}  
}  
}  
}  
}  
}  
}

Each time I submit this request, a different document is returned, along with a different value of "sort."

If I try to add a seed to Random() by sending something like "script": "(new Random(0)).nextInt()" with everything else being exactly the same, the top result returned is always the same and the sort value is always -1155484576. This seems to make sense, because I want repeatable randomness based on the seed. However, changing the seed from 0 to 1 returns the same part that was returned with a seed of 0, though the sort value is now -1155869325. Any time I specify a seed value into the constructor of Random(), no matter what the value, the same part is returned (though the sort value does change depending on what seed value I enter).

I'm not sure why this works when I don't specify a seed value, but seems to break the moment I enter a seed value, and I was wondering if someone could please let me know if I'm doing something wrong. Thank you in advance!

---

<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:29pm UTC](https://discuss.elastic.co/t/repeatable-randomness-in-field-collapsed-results/37791/2 "2017-07-05T23:29:12Z")

</div>


