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:

  <field name="id">1</id>
  <field name="author">Solr.pl author</field>
  <field name="email">blog(at)solr.pl</field>

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:

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="author" type="text_general" indexed="true" stored="true"/>
<field name="email" type="text_general" indexed="true" stored="true"/>

Additional configuration

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

<fieldType name="random" class="solr.RandomSortField" indexed="true" />
<dynamicField name="random_*" type="random" />

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:

<result name="response" numFound="19" start="0">
    <str name="id">9</str>
    <str name="author">Rajeev Srivastava</str>
    <str name="email">[CENSORED]</str>
    <long name="_version_">1431017731370516481</long></doc>
    <str name="id">8</str>
    <str name="author">Evgeny</str>
    <str name="email">[CENSORED]</str>
    <long name="_version_">1431017731370516480</long></doc>

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 !

This post is also available in: Polish

This entry was posted on Tuesday, April 2nd, 2013 at 08:21 and is filed under About Solr. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Random documents from result set (Giveaway results !)”

  1. Niki Says:


    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,

  2. gr0 Says:

    I don’t think this is possible :(

  3. Craig Says:

    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

  4. sagar Says:

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