Solr - Return random results (Sort by Random)

前端 未结 2 966
无人及你
无人及你 2020-12-07 02:03

In Solr, Whats the best approach to order results by random?, basically needs to pick some records at random.

相关标签:
2条回答
  • 2020-12-07 02:14

    In latest Solr, nothing need to do with schema. Just add below sorting and it will give random result set.

    ...
    ...
    
    //Below is to get rendom string
    $randString = mt_rand();        
    $query->addSort('random_'.$randString, $query::SORT_DESC);
    
    ...
    ...
    
    0 讨论(0)
  • 2020-12-07 02:22

    Use a RandomSortField. There's an example in the bundled schema.xml in example directory (under solr):

    <dynamicField name="random_*" type="random" />
    

    Sort your result list by the field random_<seed>, where you replace <seed> with a randomly generated value (but use the same value if you're paginating through the result, as it will give you a stable sort based on the field name).

    0 讨论(0)
提交回复
热议问题