class Metacrunch::Elasticsearch::Source
Constants
- DEFAULT_OPTIONS
Public Class Methods
new(elasticsearch_client, options = {})
click to toggle source
# File lib/metacrunch/elasticsearch/source.rb, line 15 def initialize(elasticsearch_client, options = {}) @client = elasticsearch_client @options = DEFAULT_OPTIONS.deep_merge(options) end
Public Instance Methods
call_total_hits_callback(result)
click to toggle source
# File lib/metacrunch/elasticsearch/source.rb, line 43 def call_total_hits_callback(result) if @options[:total_hits_callback]&.respond_to?(:call) && total = result.dig("hits", "total", "value") @options[:total_hits_callback].call(total) end end
each(&block)
click to toggle source
# File lib/metacrunch/elasticsearch/source.rb, line 20 def each(&block) return enum_for(__method__) unless block_given? # Perform search request and yield the first results if any search_options = @options[:search_options] result = @client.search(search_options) call_total_hits_callback(result) yield_hits(result, &block) # Scroll over the rest of result set and yield the results until the set is empty. while ( # Note: semantic of 'and' is important here. Do not use '&&'. result = @client.scroll(scroll_id: result["_scroll_id"], scroll: search_options[:scroll]) and result["hits"]["hits"].present? ) do yield_hits(result, &block) end ensure # Clear scroll to free up resources. @client.clear_scroll(scroll_id: result["_scroll_id"]) if result end private def call_total_hits_callback(result) if @options[:total_hits_callback]&.respond_to?(:call) && total = result.dig("hits", "total", "value") @options[:total_hits_callback].call(total) end end def yield_hits(result, &block) result["hits"]["hits"].each do |hit| yield(hit) end end end
yield_hits(result) { |hit| ... }
click to toggle source
# File lib/metacrunch/elasticsearch/source.rb, line 49 def yield_hits(result, &block) result["hits"]["hits"].each do |hit| yield(hit) end end