Repeatable randomness in field-collapsed results

(Kathleen DeRusso) #1

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": [
"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!

(system) #2