SearchKick获得总结果数

时间:2015-04-28 13:28:16

标签: ruby-on-rails searchkick

我正在使用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响应&#39;`

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

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