diff --git a/elasticsearch-model/lib/elasticsearch/model/response/pagination/kaminari.rb b/elasticsearch-model/lib/elasticsearch/model/response/pagination/kaminari.rb index 66de07cc..b0e9f2ad 100644 --- a/elasticsearch-model/lib/elasticsearch/model/response/pagination/kaminari.rb +++ b/elasticsearch-model/lib/elasticsearch/model/response/pagination/kaminari.rb @@ -106,10 +106,17 @@ def offset(value) self end + # Set the "total_count" value + # + def total(value) + @total_count = value.to_i + self + end + # Returns the total number of results # def total_count - results.total + @total_count || results.total end # Returns the models's `per_page` value or the default diff --git a/elasticsearch-model/lib/elasticsearch/model/response/pagination/will_paginate.rb b/elasticsearch-model/lib/elasticsearch/model/response/pagination/will_paginate.rb index 2d465854..2eef2599 100644 --- a/elasticsearch-model/lib/elasticsearch/model/response/pagination/will_paginate.rb +++ b/elasticsearch-model/lib/elasticsearch/model/response/pagination/will_paginate.rb @@ -55,9 +55,11 @@ def paginate(options) param_name = options[:param_name] || :page page = [options[param_name].to_i, 1].max per_page = (options[:per_page] || __default_per_page).to_i + total = options[:total_entries] search.definition.update size: per_page, from: (page - 1) * per_page + @total_entries = total.to_i if total.present? self end @@ -94,7 +96,7 @@ def per_page(num = nil) # Returns the total number of results # def total_entries - results.total + @total_entries || results.total end # Returns the models's `per_page` value or the default diff --git a/elasticsearch-model/spec/elasticsearch/model/response/pagination/kaminari_spec.rb b/elasticsearch-model/spec/elasticsearch/model/response/pagination/kaminari_spec.rb index 15ee6113..e0bcab99 100644 --- a/elasticsearch-model/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +++ b/elasticsearch-model/spec/elasticsearch/model/response/pagination/kaminari_spec.rb @@ -298,8 +298,20 @@ def self.document_type; 'bar'; end allow(response.results).to receive(:total).and_return(100) end - it 'returns the total number of hits' do - expect(response.total_count).to eq(100) + context 'when a custom value is not set' do + it 'returns the total number of hits' do + expect(response.total_count).to eq(100) + end + end + + context 'when a value is set via the #total method' do + before do + response.total(50) + end + + it 'returns the total number of hits' do + expect(response.total_count).to eq(50) + end end end diff --git a/elasticsearch-model/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb b/elasticsearch-model/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb index 25f1f0d1..8da294a5 100644 --- a/elasticsearch-model/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +++ b/elasticsearch-model/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb @@ -245,8 +245,20 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response allow(response).to receive(:results).and_return(double('results', total: 100)) end - it 'returns the total results' do - expect(response.total_entries).to eq(100) + context 'when a custom value is not set' do + it 'returns the total results' do + expect(response.total_entries).to eq(100) + end + end + + context 'when a value is set via the #paginate method' do + before do + response.paginate(total_entries: 50) + end + + it 'returns the total_entries value' do + expect(response.total_entries).to eq(50) + end end end end