# Sorting a random set of documents

**URL:** <https://discuss.elastic.co/t/sorting-a-random-set-of-documents/19916>\
**Category:** Elasticsearch\
**Created:** [September 22, 2014, 2:38pm UTC](https://discuss.elastic.co/t/sorting-a-random-set-of-documents/19916 "2014-09-22T14:38:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Moss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/james_moss/32/1265_2.png) [@James\_Moss](https://discuss.elastic.co/u/James_Moss)\
**Post date:** [September 22, 2014, 2:38pm UTC](https://discuss.elastic.co/t/sorting-a-random-set-of-documents/19916/1 "2014-09-22T14:38:14Z")

</div>

Hi,

I need to pull out a random selection of documents (based on a seed) and  
then make that set sortable but I'm having difficulties:

Here's what I've got at the moment, I'm trying to pull out a random  
selection of document (real estate properties) and then sort them by price  
desc:

GET \_search  
{  
"from": 0,  
"size": 30,  
"query": {  
"function\_score": {  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"and": [  
{  
"term": {  
"property.rental": 0  
}  
},  
{  
"term": {  
"property.status": 100  
}  
}  
]  
}  
}  
},  
"random\_score": {  
"seed": 124567  
}  
}  
},  
"sort": [  
{  
"property.price": "desc"  
}  
],  
"filter": {  
"range": {  
"property.price": {  
"gte": 500000  
}  
}  
}  
}

It seems the sort is being applied to all the documents in the index, not  
just those returned from the query.

Is this even possible with elasticsearch?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b82ba776-854b-4649-bd71-2719cf095061%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b82ba776-854b-4649-bd71-2719cf095061%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [September 22, 2014, 2:57pm UTC](https://discuss.elastic.co/t/sorting-a-random-set-of-documents/19916/2 "2014-09-22T14:57:46Z")

</div>

No, it is just that sort is overriding the random\_score. I would say just  
do the sorting on the client side - while it is possible to do still on ES  
(using scripted fields for example) it will just get too complicated.

--

Itamar Syn-Hershko  
[http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
Freelance Developer & Consultant  
Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)

On Mon, Sep 22, 2014 at 5:38 PM, James Moss [email@jamesmoss.co.uk](mailto:email@jamesmoss.co.uk) wrote:

> Hi,
> 
> I need to pull out a random selection of documents (based on a seed) and  
> then make that set sortable but I'm having difficulties:
> 
> Here's what I've got at the moment, I'm trying to pull out a random  
> selection of document (real estate properties) and then sort them by price  
> desc:
> 
> GET \_search  
> {  
> "from": 0,  
> "size": 30,  
> "query": {  
> "function\_score": {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "and": [  
> {  
> "term": {  
> "property.rental": 0  
> }  
> },  
> {  
> "term": {  
> "property.status": 100  
> }  
> }  
> ]  
> }  
> }  
> },  
> "random\_score": {  
> "seed": 124567  
> }  
> }  
> },  
> "sort": [  
> {  
> "property.price": "desc"  
> }  
> ],  
> "filter": {  
> "range": {  
> "property.price": {  
> "gte": 500000  
> }  
> }  
> }  
> }
> 
> It seems the sort is being applied to all the documents in the index, not  
> just those returned from the query.
> 
> Is this even possible with elasticsearch?
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/b82ba776-854b-4649-bd71-2719cf095061%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b82ba776-854b-4649-bd71-2719cf095061%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/b82ba776-854b-4649-bd71-2719cf095061%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b82ba776-854b-4649-bd71-2719cf095061%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHTr4Zu%2BmVGBznf%3Dcst0C%2B8RWRSDRrmPnbE-dg4Awno5EB84Wg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4Zu%2BmVGBznf%3Dcst0C%2B8RWRSDRrmPnbE-dg4Awno5EB84Wg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:00am UTC](https://discuss.elastic.co/t/sorting-a-random-set-of-documents/19916/3 "2017-07-06T01:00:38Z")

</div>


