Skip to content

Commit 390a7f6

Browse files
authored
Merge pull request #397 from meilisearch/bump-meilisearch-v0.30.0-add-limited-pagination
Add support to finite pagination
2 parents 2dd1681 + 29e8607 commit 390a7f6

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lib/meilisearch/index.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ def search(query, options = {})
191191
parsed_options = Utils.transform_attributes({ q: query.to_s }.merge(options.compact))
192192

193193
response = http_post "/indexes/#{@uid}/search", parsed_options
194-
response['nbHits'] ||= response['estimatedTotalHits']
194+
195+
if !response.key?('totalPages')
196+
response['nbHits'] ||= response['estimatedTotalHits']
197+
end
195198

196199
response
197200
end

spec/meilisearch/index/search/q_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@
6969
expect(response['hits'].first['objectId']).to eq(4)
7070
expect(response['hits'].first).not_to have_key('_formatted')
7171
end
72+
73+
context 'with finite pagination params' do
74+
it 'responds with specialized fields' do
75+
response = index.search('coco', { page: 2, hits_per_page: 2 })
76+
77+
expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
78+
end
79+
80+
it 'responds with specialized fields' do
81+
response = index.search('coco', { page: 2, hitsPerPage: 2 })
82+
83+
expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
84+
end
85+
end
7286
end

spec/spec_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
'nbHits'
4242
].freeze
4343

44+
FINITE_PAGINATED_SEARCH_RESPONSE_KEYS = [
45+
'hits',
46+
'query',
47+
'processingTimeMs',
48+
'hitsPerPage',
49+
'page',
50+
'totalPages',
51+
'totalHits',
52+
].freeze
53+
4454
Dir["#{Dir.pwd}/spec/support/**/*.rb"].sort.each { |file| require file }
4555

4656
RSpec.configure do |config|

0 commit comments

Comments
 (0)