1

We have a requirement to add a random number to a field and sort it .

eg; we have rank which is integer in each document. we need to add some random number between 1-10 to rank field in each document and finally sort it in ascending.

by doing this we can rearrange the product each time we call in the solr response.

Gopi
  • 25
  • 1
  • 10

2 Answers2

0

One of the possibility to achieve this is to have RandomSortField, which could be used as a second field for sorting.

schema.xml:

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

then, you would need to do sorting during query time like this:

sort=rank DESC,random_1 DESC

So, you will be able to achieve what you want, the only caveat is, that values of RandomSortField wouldn't be from 1 to 10, but rather truly random.

Mysterion
  • 9,050
  • 3
  • 30
  • 52
0

I was able to achieve this using the combination of sum and scale. Using the scale function i was able to scale the random_ to min and max values in my case 0 to 10.

then use the sum function to add that value to the integer field.

Gopi
  • 25
  • 1
  • 10