3

我正在使用SearchKick搜索数据。我正在执行分页。在这种情况下,我如何获得总结果数:

我的搜索结果:

       #<Searchkick::Results:0x007faeeef8ed88
   @facets=nil,
   @max_score=0.0,
   @options=
    {:load=>true,
     :payload=>
      {:query=>{:match_all=>{}},
       :size=>20,
       :from=>0,
       :sort=>{"created_at"=>:desc},
       :filter=>{:and=>[{:term=>{"sub_category_id"=>2}}]},
       :fields=>[]},
     :size=>20,
     :from=>0,
     :term=>"*"},
   @response=
    {"took"=>4,
     "timed_out"=>false,
     "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0},
     "hits"=>
      {"total"=>1925,
       "max_score"=>nil,
       "hits"=>
        [{"_index"=>"products_..", "_type"=>"product", "_id"=>"..", "_score"=>nil, "sort"=>[..]},....]}},
   @results=
    [#<Product>,...]....

我不能做的另一件事是:

SearchKickResult.response

它返回我错误说:

undefined method回应'`

4

3 回答 3

6

Searchkick 提供了#total_count一种计算响应总命中数的方法:

https://github.com/ankane/searchkick/blob/7a24684bb470abd5ceca7a40b21d28584b910a4c/lib/searchkick/results.rb#L89

def total_count
  response["hits"]["total"]
end
alias_method :total_entries, :total_count
于 2015-04-28T13:30:21.027 回答
2

在 searchkick (0.9.0) 中它是: response.total_count

于 2015-06-17T11:53:32.223 回答
0

试试这个:-

数据=模型名称搜索“*”

数据.结果.计数

于 2021-10-29T12:58:36.643 回答