Skip to content

Instantly share code, notes, and snippets.

@jlecour
Created April 7, 2014 09:44
Show Gist options
  • Select an option

  • Save jlecour/10017406 to your computer and use it in GitHub Desktop.

Select an option

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
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
@jlecour
Copy link
Copy Markdown
Author

jlecour commented Apr 7, 2014

It might not be the perfect implementation, but it works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment