I’ll continue my series of blog posts today based on “questions I’ve got” with one regarding randomly ordered search results. This can be quite useful if you want to show a subset of a search result and expose the individual result items of the entire result set equally. Say that you for instance have a list campaign products and want to show a limited number of these on the front page but still expose each one of them an equal amount of times to the users. To do this we need to extend the EPiServer Find API with script sorting functionality and add an Order-extension to the fluent API that sorts each result item based on a random number generated by the script. At Random2Find you can fetch your own copy of the code.

How to use Random2Find

Random2Find is a pure query time extension that don’t need to be registered at startup so all you need to do is specifying OrderRandom on your query:

client.Search<Document>()
    .OrderRandom()
    .GetResult();

OrderRandom can of course also be used in conjunction with another OrderBy-clause if you want to randomly order items within a subset of ordered items:

client.Search<Document>()
    .OrderBy(x => x.Name)
    .ThenOrderRandom()
    .GetResult();

I hope you may find this useful when making your site awesome with EPiServer Find.