Skip to content

Commit

Permalink
Support ES5 on elasticsearch rails (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Huang authored Apr 23, 2020
1 parent d58a39d commit a9cee7d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/extras/elasticsearch_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require 'pagy/extras/elasticsearch_rails'
If you have an already paginated `Elasticsearch::Model::Response::Response` object, you can get the `Pagy` object out of it:

```ruby
@response = Model.search('*', from: 0; size: 10, ...)
@response = Model.search('*', from: 0, size: 10, ...)
@pagy = Pagy.new_from_elasticsearch_rails(@response, ...)
```

Expand Down
6 changes: 5 additions & 1 deletion lib/pagy/extras/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class Pagy
def self.new_from_elasticsearch_rails(response, vars={})
vars[:items] = response.search.options[:size] || 10
vars[:page] = (response.search.options[:from] || 0) / vars[:items] + 1
total = response.raw_response['hits']['total']
total = if response.respond_to?(:raw_response)
response.raw_response['hits']['total']
else
response.response['hits']['total']
end
vars[:count] = total.is_a?(Hash) ? total['value'] : total
new(vars)
end
Expand Down
27 changes: 27 additions & 0 deletions test/mock_helpers/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,31 @@ def self.search(*args)
end

end

class ResponseES5

attr_reader :search, :response

def initialize(query, options={})
@search = Search.new(query, options)
@response = {'hits' => {'hits' => @search.results, 'total' => RESULTS[query].size}}
end

def records
@response['hits']['hits'].map{|r| "R-#{r}"}
end

def count
@response['hits']['hits'].size
end

end

class ModelES5 < Model

def self.search(*args)
ResponseES5.new(*args)
end

end
end
18 changes: 18 additions & 0 deletions test/pagy/extras/elasticsearch_rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@
_(pagy.vars[:link_extra]).must_equal 'X'
end

it 'paginates response with defaults on Elasticearch 5' do
response = MockElasticsearchRails::ModelES5.search('a')
pagy = Pagy.new_from_elasticsearch_rails(response)
_(pagy).must_be_instance_of Pagy
_(pagy.count).must_equal 1000
_(pagy.items).must_equal 10
_(pagy.page).must_equal 1
end

it 'paginates response with vars on Elasticsearch 5' do
response = MockElasticsearchRails::ModelES5.search('b', from: 15, size: 15)
pagy = Pagy.new_from_elasticsearch_rails(response, link_extra: 'X')
_(pagy).must_be_instance_of Pagy
_(pagy.count).must_equal 1000
_(pagy.items).must_equal 15
_(pagy.page).must_equal 2
_(pagy.vars[:link_extra]).must_equal 'X'
end
end

end

0 comments on commit a9cee7d

Please sign in to comment.