Module: Pagy::ElasticsearchRailsPaginator

Defined in:
lib/pagy/toolbox/paginators/elasticsearch_rails.rb

Class Method Summary collapse

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, options)
  options[:search_method]     ||= ElasticsearchRails::DEFAULT[:search_method]
  options[:max_result_window] ||= ElasticsearchRails::DEFAULT[:max_result_window]

  if search.is_a?(Search::Arguments)  # Active mode

    Searcher.wrap(search, options) do
      model, arguments, search_options = search

      search_options[:size] = options[:limit]
      search_options[:from] = options[:limit] * ((options[:page] || 1) - 1)

      response_object = model.send(options[:search_method], *arguments, **search_options)
      options[:count] = total_count_from(response_object, options)

      [ElasticsearchRails.new(**options), response_object]
    end

  else # Passive mode
    from, size      = pagination_params_from(search)
    options[:limit] = size
    options[:page]  = ((from || 0) / options[:limit]) + 1
    options[:count] = total_count_from(search, options)

    ElasticsearchRails.new(**options)
  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.options
  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, options)
  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
  [options[:max_result_window], value].min
end