Module: Caoutsearch::Search::BatchMethods

Included in:
Base
Defined in:
lib/caoutsearch/search/batch_methods.rb

Instance Method Summary collapse

Instance Method Details

#find_each_hit(**options, &block) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/caoutsearch/search/batch_methods.rb', line 6

def find_each_hit(**options, &block)
  return to_enum(:find_each_hit, **options) { total_count } unless block

  find_hits_in_batches(**options) do |hits|
    hits.each(&block)
  end
end

#find_each_record(**options, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/caoutsearch/search/batch_methods.rb', line 14

def find_each_record(**options, &block)
  return to_enum(:find_each_record, **options) { total_count } unless block

  find_records_in_batches(**options) do |relation|
    relation.each(&block)
  end
end

#find_hits_in_batches(implementation: :search_after, **options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/caoutsearch/search/batch_methods.rb', line 34

def find_hits_in_batches(implementation: :search_after, **options)
  batch_size = options[:batch_size]&.to_i || @current_limit&.to_i || 1000

  unless block_given?
    return to_enum(:find_hits_in_batches, **options) do
      total_count.div(batch_size) + 1
    end
  end

  unless %i[search_after scroll].include?(implementation)
    raise ArgumentError, "unexpected implementation argument: #{implementation.inspect}"
  end

  method(implementation).call(batch_size: batch_size, **options) do |hits|
    yield hits
  end
end

#find_records_in_batches(**options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/caoutsearch/search/batch_methods.rb', line 22

def find_records_in_batches(**options)
  unless block_given?
    return to_enum(:find_records_in_batches, **options) do
      find_hits_in_batches(**options).size
    end
  end

  find_hits_in_batches(**options) do |hits|
    yield records_adapter.call(model, hits, skip_query_cache: true)
  end
end

#scroll_records(**options) ⇒ Object



57
58
59
60
# File 'lib/caoutsearch/search/batch_methods.rb', line 57

def scroll_records(**options)
  ActiveSupport::Deprecation.warn("scroll_records is deprecated, use find_each_record instead")
  find_each_record(implementation: :scroll, **options)
end

#scroll_records_in_batches(**options) ⇒ Object



52
53
54
55
# File 'lib/caoutsearch/search/batch_methods.rb', line 52

def scroll_records_in_batches(**options)
  ActiveSupport::Deprecation.warn("scroll_records_in_batches is deprecated, use find_records_in_batches instead")
  find_records_in_batches(implementation: :scroll, **options)
end