Method: Elastic::Core::Connector#copy_to

Defined in:
lib/elastic/core/connector.rb

#copy_to(_to, batch_size: nil) ⇒ Object

rubocop:disable Metrics/AbcSize



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/elastic/core/connector.rb', line 137

def copy_to(_to, batch_size: nil) # rubocop:disable Metrics/AbcSize
  api.indices.refresh index: index_name

  r = api.search(
    index: index_name,
    body: { sort: ['_doc'] },
    scroll: '5m',
    size: batch_size || default_batch_size
  )

  count = 0
  while !r['hits']['hits'].empty?
    count += r['hits']['hits'].count
    Elastic.logger.info "Copied #{count} docs"

    body = r['hits']['hits'].map { |h| transform_hit_to_create(h) }
    api.bulk(index: _to, body: body)

    r = api.scroll scroll: '5m', scroll_id: r['_scroll_id']
  end
end