Reputation: 4894
I'm using Searchkick to search through user records. I'm getting the same order for the results every time. I have gone through the documentation but could not find anything. Following is the implementation I'm using.
search_params = {}
search_params[:where] = where #where holds all the conditions for the search
search_params[:order] = {user_id: :desc}
user_matches = User.search "*", search_params
Upvotes: 0
Views: 570
Reputation: 1
I recently ran into similar requirement - I used -
order_params = {
"_script" => {
"script" => "(doc['created_at'].value + doc['user_id'].value + _score).hashCode()",
"type" => "string",
"order" => "desc"
}
}
Model.search "*", {order: order_params}
Upvotes: 0
Reputation: 3282
A rails way:
user_matches.results.shuffle
A Searchkick way:
seed = current_user.id
# setting seed to the user_id will make the random results consistent for that user...
# ... if you always want random results than you should make seed = Time.zone.now.to_i
User.search( "*", body: {query: {function_score: {random_score: {seed: seed}}}} )
For the searchkick way here is the elasticsearch reference
Here is another example in Searchkick issues
Upvotes: 1