Jorick918
Jorick918

Reputation: 21

Azure Search -- Want random result set

I'm playing around with replacing an ElasticSearch cluster with an Azure Search service, mainly due to infrastructure hosting/monitoring concerns. So far, things are going decently, but one of the scenarios I need to account for is retrieving 10 random results for a query. In ElasticSearch, we just used the RandomScore function from the Nest library which was good enough.

I've read a bit on Scoring Profiles, but that doesn't seem to fit what I need, as it doesn't have a Random ScoringAggregationFunction.

Is there something I'm missing on how to do this natively?

Upvotes: 2

Views: 974

Answers (2)

Geordie
Geordie

Reputation: 2184

Trying this at the moment. I'm using a two step process:

  1. Generate your query but set $count=true and $top=0. The query result should contain a field named @odata.count.
  2. You can then randomly pick an int between 0 and @odata.count then use $top=1 and $skip=<random number> to return a single random entry. There is one caveat: $skip will only accept numbers less than 100000

Upvotes: 1

ashmaka
ashmaka

Reputation: 81

Unfortunately, there is no way to get randomly sorted results in Azure Search. Please request this feature on our UserVoice to help us prioritize: https://feedback.azure.com/forums/263029-azure-search

A workaround may be to add a field to your index that is populated with some randomly generated integer within some range at index time. When you would call for 10 random documents, your application would instead randomly generate integers within that range to create a filter clause on that field to get randomized results.

Upvotes: 1

Related Questions