Module: Pagy::ElasticsearchRailsPaginator
- Defined in:
- lib/pagy/toolbox/paginators/elasticsearch_rails.rb
Class Method Summary collapse
- .paginate(search, options) ⇒ Object
-
.pagination_params_from(response_object) ⇒ Object
Support different versions of ElasticsearchRails.
-
.total_count_from(response_object, options) ⇒ Object
Support different versions of ElasticsearchRails.
Class Method Details
.paginate(search, options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pagy/toolbox/paginators/elasticsearch_rails.rb', line 9 def paginate(search, ) [:search_method] ||= ElasticsearchRails::DEFAULT[:search_method] [:max_result_window] ||= ElasticsearchRails::DEFAULT[:max_result_window] if search.is_a?(Search::Arguments) # Active mode Searcher.wrap(search, ) do model, arguments, = search [:size] = [:limit] [:from] = [:limit] * (([:page] || 1) - 1) response_object = model.send([:search_method], *arguments, **) [:count] = total_count_from(response_object, ) [ElasticsearchRails.new(**), response_object] end else # Passive mode from, size = pagination_params_from(search) [:limit] = size [:page] = ((from || 0) / [:limit]) + 1 [:count] = total_count_from(search, ) ElasticsearchRails.new(**) end end |
.pagination_params_from(response_object) ⇒ Object
Support different versions of ElasticsearchRails
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pagy/toolbox/paginators/elasticsearch_rails.rb', line 38 def pagination_params_from(response_object) definition = response_object.search.definition definition = definition.to_hash if definition.respond_to?(:to_hash) container = (definition.is_a?(Hash) && (definition[:body] || definition)) || response_object.search. from = (container[:from] || container['from']).to_i size = (container[:size] || container['size']).to_i size = 10 if size.zero? [from, size] end |
.total_count_from(response_object, options) ⇒ Object
Support different versions of ElasticsearchRails
50 51 52 53 54 55 56 57 |
# File 'lib/pagy/toolbox/paginators/elasticsearch_rails.rb', line 50 def total_count_from(response_object, ) total = response_object.instance_eval do respond_to?(:response) ? response['hits']['total'] : raw_response['hits']['total'] end value = total.is_a?(Hash) ? total['value'] : total [[:max_result_window], value].min end |