Random documents from result set (Giveaway results !)

And now two birds with a single stone – a new article and the Apache Solr 4 Cookbook giveaway results. In this article we would like to show you how to implement random ordering of documents in the resulting using Apache Solr. Our example is real case scenario – we’ve used this to draw two giveaway participants. Those two comment authors that will be of top of the results set will receive the ebook.


Our documents contain information about participants of the competition – their id, name (as the author field) and email.. For example one record looks like that:

Our very big data contains 19 records, maybe we should have used map/reduce ? :).


The schema.xml file describing the structure of the index is also very simple. In our case it contains the following fields:

Additional configuration

Now we need to make sure that the schema.xml file contains the following definition type and field definitions:

In the example schema.xml file provided with the standard Solr distribution package this type and dynamic field is available by default. We will need those to randomize our result set.

Running a query with random sorting

Running a query with a random sorting is a little bit tricky. We build a query like we usually do except for the sorting. For the sort parameter we will use the previously defined dynamic field with the random prefix. For example:

How it works ?

Solr will calculate ordering of the documents basing on the name of the random field and the index version. This means that every time you use the same field name and the same index (which was not changed between queries), you will get results that are ordered exactly the same way. This is disadvantage of this method, but sometimes this may be quite handy, like when doing paging (we don’t want to have different results ordering for each page, right ?). Because of this you have to generate the field name in your application that runs queries to Solr.

And now – Giveaway results !

We’ve used the above-mentioned query. Number used in sort field is absolutely random, that was randomized by saying: “Dad, tell me some random numbers” :). So the whole query we’ve used was:

This above query gave the following results:

And the winners are

  • Rajeev
  • Evgeny

Congratulations ! We will contact you in the very near future with further information about how to receive your awards.  Once again congratulations! Also, to all the other participants, thanks for participations and your comments !

4 thoughts on “Random documents from result set (Giveaway results !)

  • 28 May 2014 at 13:04


    whenever i reindex my solr, random order for a particular key changes.

    is there any way to avoid this, so that for a particular random key, i can always have same records.

    Thanks in advance,

  • 8 July 2014 at 15:30

    A hash would accomplish this, probably. I’m not sure if you would run the risk of collision but if it’s not a problem for a very, very, very small set of your documents to match the same “unique” key, you could do that. For more info: https://wiki.apache.org/solr/UniqueKey

  • 21 October 2014 at 08:01

    The information in this page was very helpful for me.
    I was able to resolve my task with this little but great info.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.