# Random sorting

**URL:** <https://discuss.elastic.co/t/random-sorting/5795>\
**Category:** Elasticsearch\
**Created:** [November 7, 2011, 1:12pm UTC](https://discuss.elastic.co/t/random-sorting/5795 "2011-11-07T13:12:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![1114](https://avatars.discourse-cdn.com/v4/letter/1/77aa72/32.png) [@1114](https://discuss.elastic.co/u/1114)\
**Post date:** [November 7, 2011, 1:12pm UTC](https://discuss.elastic.co/t/random-sorting/5795/1 "2011-11-07T13:12:02Z")

</div>

Hello!

Is it possible to set up sorting to return items at random based on one of  
the field?

Something like :

{  
"sort" : {  
{ "post\_date" : {"order" : "RANDOM"} },  
"user" : { },  
"\_score" : { }  
},  
"query" : {  
"term" : { "user" : "kimchy" }  
}  
}

Or any other possibilities to get a random responce.

Thanks,  
Andrey.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [November 7, 2011, 3:44pm UTC](https://discuss.elastic.co/t/random-sorting/5795/2 "2011-11-07T15:44:42Z")

</div>

Try a custom score function with a random seed returned

Peter.

On Nov 7, 2:12 pm, Андрей Смолев [andrey.smo...@gmail.com](mailto:andrey.smo...@gmail.com) wrote:

> Hello!
> 
> Is it possible to set up sorting to return items at random based on one of  
> the field?
> 
> Something like :
> 
> {  
> "sort" : {  
> { "post\_date" : {"order" : "RANDOM"} },  
> "user" : { },  
> "\_score" : { }  
> },  
> "query" : {  
> "term" : { "user" : "kimchy" }  
> }
> 
> }
> 
> Or any other possibilities to get a random responce.
> 
> Thanks,  
> Andrey.

---

<div class="post-metadata">

**Author:** ![1114](https://avatars.discourse-cdn.com/v4/letter/1/77aa72/32.png) [@1114](https://discuss.elastic.co/u/1114)\
**Post date:** [November 7, 2011, 8:05pm UTC](https://discuss.elastic.co/t/random-sorting/5795/3 "2011-11-07T20:05:12Z")

</div>

Thank you! Looks like an answer!  
On Nov 7, 2011 6:44 PM, "Karussell" [tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com) wrote:

> Try a custom score function with a random seed returned
> 
> Peter.
> 
> On Nov 7, 2:12 pm, Андрей Смолев [andrey.smo...@gmail.com](mailto:andrey.smo...@gmail.com) wrote:
> 
> > Hello!
> > 
> > Is it possible to set up sorting to return items at random based on one  
> > of  
> > the field?
> > 
> > Something like :
> > 
> > {  
> > "sort" : {  
> > { "post\_date" : {"order" : "RANDOM"} },  
> > "user" : { },  
> > "\_score" : { }  
> > },  
> > "query" : {  
> > "term" : { "user" : "kimchy" }  
> > }
> > 
> > }
> > 
> > Or any other possibilities to get a random responce.
> > 
> > Thanks,  
> > Andrey.

---

<div class="post-metadata">

**Author:** ![Kunal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kunal/32/3079_2.png) [@Kunal](https://discuss.elastic.co/u/Kunal)\
**Post date:** [November 8, 2011, 2:54am UTC](https://discuss.elastic.co/t/random-sorting/5795/4 "2011-11-08T02:54:35Z")

</div>

Hi Andre,

Do you have a code example for this solution? Trying to do the same  
thing, but not entirely sure what it actually looks like...

Thanks!  
-Kunal

On Nov 7, 12:05 pm, Андрей Смолев [andrey.smo...@gmail.com](mailto:andrey.smo...@gmail.com) wrote:

> Thank you! Looks like an answer!  
> On Nov 7, 2011 6:44 PM, "Karussell" [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:
> 
> > Try a custom score function with a random seed returned
> 
> > Peter.
> 
> > On Nov 7, 2:12 pm, Андрей Смолев [andrey.smo...@gmail.com](mailto:andrey.smo...@gmail.com) wrote:
> > 
> > > Hello!
> 
> > > Is it possible to set up sorting to return items at random based on one  
> > > of  
> > > the field?
> 
> > > Something like :
> 
> > > {  
> > > "sort" : {  
> > > { "post\_date" : {"order" : "RANDOM"} },  
> > > "user" : { },  
> > > "\_score" : { }  
> > > },  
> > > "query" : {  
> > > "term" : { "user" : "kimchy" }  
> > > }
> 
> > > }
> 
> > > Or any other possibilities to get a random responce.
> 
> > > Thanks,  
> > > Andrey.

---

<div class="post-metadata">

**Author:** ![1114](https://avatars.discourse-cdn.com/v4/letter/1/77aa72/32.png) [@1114](https://discuss.elastic.co/u/1114)\
**Post date:** [November 9, 2011, 10:56am UTC](https://discuss.elastic.co/t/random-sorting/5795/5 "2011-11-09T10:56:55Z")

</div>

I tried to use custom\_score but i had an syntax error. What do i do wrong?

\_search?source={"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
}}

{  
"error": "SearchPhaseExecutionException[Failed to execute phase [query],  
total failure; shardFailures {[LS6ki3GCS\_yyziipuYI9Og][vitrina][3]:  
RemoteTransportException[[Arlok][inet[/10.10.205.24:9300]][search/phase/query]];  
nested: SearchParseException[[vitrina][3]: from[-1],size[-1]: Parse Failure  
[Failed to parse source  
[{"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
}}]]]; nested: SearchParseException[[vitrina][3]: from[-1],size[-1]: Parse  
Failure [No parser for element [custom\_score]]];  
}{[Cq6VIo58SQCCjpMay6KrhQ][vitrina][0]: SearchParseException[[vitrina][0]:  
from[-1],size[-1]: Parse Failure [Failed to parse source  
[{"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
}}]]]; nested: SearchParseException[[vitrina][0]: from[-1],size[-1]: Parse  
Failure [No parser for element [custom\_score]]];  
}{[Cq6VIo58SQCCjpMay6KrhQ][vitrina][1]: SearchParseException[[vitrina][1]:  
from[-1],size[-1]: Parse Failure [Failed to parse source  
[{"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
}}]]]; nested: SearchParseException[[vitrina][1]: from[-1],size[-1]: Parse  
Failure [No parser for element [custom\_score]]];  
}{[qqcovTfeRg2mk72Yjf0OIA][vitrina][2]: RemoteTransportException[[Devil  
Dinosaur][inet[/10.10.205.25:9300]][search/phase/query]]; nested:  
SearchParseException[[vitrina][2]: from[-1],size[-1]: Parse Failure [Failed  
to parse source  
[{"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
}}]]]; nested: SearchParseException[[vitrina][2]: from[-1],size[-1]: Parse  
Failure [No parser for element [custom\_score]]];  
}{[LS6ki3GCS\_yyziipuYI9Og][vitrina][4]:  
RemoteTransportException[[Arlok][inet[/10.10.205.24:9300]][search/phase/query]];  
nested: SearchParseException[[vitrina][4]: from[-1],size[-1]: Parse Failure  
[Failed to parse source  
[{"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
}}]]]; nested: SearchParseException[[vitrina][4]: from[-1],size[-1]: Parse  
Failure [No parser for element [custom\_score]]]; }]",  
"status": 500  
}

On Mon, Nov 7, 2011 at 7:44 PM, Karussell [tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com)wrote:

> Try a custom score function with a random seed returned
> 
> Peter.
> 
> On Nov 7, 2:12 pm, Андрей Смолев [andrey.smo...@gmail.com](mailto:andrey.smo...@gmail.com) wrote:
> 
> > Hello!
> > 
> > Is it possible to set up sorting to return items at random based on one  
> > of  
> > the field?
> > 
> > Something like :
> > 
> > {  
> > "sort" : {  
> > { "post\_date" : {"order" : "RANDOM"} },  
> > "user" : { },  
> > "\_score" : { }  
> > },  
> > "query" : {  
> > "term" : { "user" : "kimchy" }  
> > }
> > 
> > }
> > 
> > Or any other possibilities to get a random responce.
> > 
> > Thanks,  
> > Andrey.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 9, 2011, 11:01am UTC](https://discuss.elastic.co/t/random-sorting/5795/6 "2011-11-09T11:01:42Z")

</div>

On Wed, 2011-11-09 at 14:56 +0400, ÐÐ½Ð´ÑÐµÐ¹ Ð¡Ð¼Ð¾Ð»ÐµÐ² wrote:

> I tried to use custom\_score but i had an syntax error. What do i do  
> wrong?
> 
> \_search?source={"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all", "default\_operator":"AND","query":"Ð¼Ð°Ð¹ÐºÐ°~0.7"}},"size":10},"script":"\_score" }}

the 'custom\_score' should be wrapped by an outer {query: {...}}

clint

---

<div class="post-metadata">

**Author:** ![1114](https://avatars.discourse-cdn.com/v4/letter/1/77aa72/32.png) [@1114](https://discuss.elastic.co/u/1114)\
**Post date:** [November 9, 2011, 11:17am UTC](https://discuss.elastic.co/t/random-sorting/5795/7 "2011-11-09T11:17:54Z")

</div>

Ohh, i see!

Here is a working example if anyone is interested:

source={"query":{"custom\_score":{"script":"random()\*20","query":{"query\_string":{"default\_field":"\_all",  
"default\_operator":"AND","query":"майка~0.7"}},"size":10}},"sort":{"\_score":{"order":"desc"}}}

On Wed, Nov 9, 2011 at 3:01 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> On Wed, 2011-11-09 at 14:56 +0400, Андрей Смолев wrote:
> 
> > I tried to use custom\_score but i had an syntax error. What do i do  
> > wrong?
> 
> \_search?source={"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
> "default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
> }}
> 
> the 'custom\_score' should be wrapped by an outer {query: {...}}
> 
> clint

---

<div class="post-metadata">

**Author:** ![icenine18](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@icenine18](https://discuss.elastic.co/u/icenine18)\
**Post date:** [December 14, 2011, 5:24pm UTC](https://discuss.elastic.co/t/random-sorting/5795/8 "2011-12-14T17:24:14Z")

</div>

I'm trying to do something like this with a match all query...I want a  
random sampling of my entire record set. Using this custom score  
script approach to return a subset of 100,000 random records yields a  
very slow query (minutes) against the data set (~100 million records  
total). So it must be scanning through all the records before  
returning the random results... Does anyone know a more efficient way  
to accomplish this? Thanks.

On Nov 9, 6:17 am, Андрей Смолев [andrey.smo...@gmail.com](mailto:andrey.smo...@gmail.com) wrote:

> Ohh, i see!
> 
> Here is a working example if anyone is interested:
> 
> source={"query":{"custom\_score":{"script":"random()\*20","query":{"query\_string":{"default\_field":"\_all",  
> "default\_operator":"AND","query":"майка~0.7"}},"size":10}},"sort":{"\_score":{"order":"desc"}}}
> 
> On Wed, Nov 9, 2011 at 3:01 PM, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com)wrote:
> 
> > On Wed, 2011-11-09 at 14:56 +0400, Андрей Смолев wrote:
> > 
> > > I tried to use custom\_score but i had an syntax error. What do i do  
> > > wrong?
> 
> > \_search?source={"custom\_score":{{"query":{"query\_string":{"default\_field":"\_all",  
> > "default\_operator":"AND","query":"майка~0.7"}},"size":10},"script":"\_score"  
> > }}
> 
> > the 'custom\_score' should be wrapped by an outer {query: {...}}
> 
> > clint

---

<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 6, 2017, 3:45am UTC](https://discuss.elastic.co/t/random-sorting/5795/9 "2017-07-06T03:45:23Z")

</div>


