Created
April 7, 2014 09:44
-
-
Save jlecour/10017406 to your computer and use it in GitHub Desktop.
This is a snippet of code that I've ben using in a custom repository implementation, pretty similar to `persistence` from @karmi : https://github.com/elasticsearch/elasticsearch-rails/blob/persistence/elasticsearch-persistence/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_each(options = {}, &block) | |
search_options = {} | |
search_options[:index] = options.fetch(:index) { default_index(options) } | |
search_options[:body] = options.fetch(:body) { | |
{ query: { match_all: {} } } | |
} | |
search_options[:scroll] = options.fetch(:scroll) { "5m" } | |
search_options[:search_type] = "scan" | |
if options.key?(:_source) | |
search_options[:_source] = options[:_source] | |
end | |
response = client.search(search_options) | |
scroll_id = response.fetch("_scroll_id") | |
loop do | |
response = client.scroll({ | |
scroll: '1m', | |
scroll_id: scroll_id | |
}) | |
scroll_id = response.fetch("_scroll_id") | |
hits = response.fetch("hits").fetch("hits") | |
if hits.count > 0 | |
hits.each { |e| yield(e) } | |
else | |
break | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It might not be the perfect implementation, but it works for me.