ruby-on-rails 随机搜索结果

yvgpqqbh  于 2023-02-14  发布在  Ruby
关注(0)|答案(2)|浏览(142)

我正在使用Searchkick来搜索用户记录。我每次都得到相同的结果顺序。我已经看过了文档,但什么也没找到。下面是我正在使用的实现。

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
bqf10yzr

bqf10yzr1#

我最近遇到了类似的要求-我用了-

order_params = {
  "_script" => { 
    "script" => "(doc['created_at'].value + doc['user_id'].value + _score).hashCode()",
    "type" => "string",
    "order" => "desc"
   }
}
Model.search "*", {order: order_params}
zwghvu4y

zwghvu4y2#

一种轨道:

user_matches.results.shuffle

一个搜索踢道:

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}}}} )

对于searchkick方式,这里是elasticsearch reference
这是另一个关于搜索踢问题的例子

相关问题