# Using custom\_score to pull random results yields slow query

**URL:** <https://discuss.elastic.co/t/using-custom-score-to-pull-random-results-yields-slow-query/12832>\
**Category:** Elasticsearch\
**Created:** [July 17, 2013, 9:42pm UTC](https://discuss.elastic.co/t/using-custom-score-to-pull-random-results-yields-slow-query/12832 "2013-07-17T21:42:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)\
**Post date:** [July 17, 2013, 9:42pm UTC](https://discuss.elastic.co/t/using-custom-score-to-pull-random-results-yields-slow-query/12832/1 "2013-07-17T21:42:34Z")

</div>

I am using the following query on a 200 million size index

curl -XPOST "[http://xxxx:9200/leads/household/\_search](http://xxxx:9200/leads/household/_search)" -d'  
{"query":  
{"custom\_score":  
{"script":"round(random()\*100000)",  
"query":{"match\_all": {}}}},  
"sort":{"\_score":{"order":"desc"}},  
"fields":["household\_id"],  
"size":20000}'

To pull 20K records from the index in random, the match\_all represent worse  
case scenario.

The query takes a very long time, over 7 minutes for 1 shard.

I would like to know if there is a better way to approach this problem of  
random results

Thanks

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:25am UTC](https://discuss.elastic.co/t/using-custom-score-to-pull-random-results-yields-slow-query/12832/2 "2017-07-06T02:25:51Z")

</div>


