Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drastically speed up imports when bulk_size is specified #606

Merged
merged 3 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/chewy/type/import/bulk_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ def request_base

def request_bodies(body)
if @bulk_size
serializer = ::Elasticsearch::API.serializer
pieces = body.each_with_object(['']) do |piece, result|
operation, meta = piece.to_a.first
data = meta.delete(:data)
piece = [{operation => meta}, data].compact.map(&:to_json).join("\n")
piece = serializer.dump(operation => meta)
piece << "\n" << serializer.dump(data) if data.present?

if result.last.bytesize + piece.bytesize > @bulk_size
result.push(piece)
else
result[-1] = [result[-1], piece].reject(&:blank?).join("\n")
result[-1].blank? ? (result[-1] = piece) : (result[-1] << "\n" << piece)
end
end
pieces.each { |piece| piece << "\n" }
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/type/import/journal_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def entries(action, objects)
index_name: @type.index.derivable_name,
type_name: @type.type_name,
action: action,
references: identify(objects).map(&:to_json).map(&Base64.method(:encode64)),
references: identify(objects).map { |item| Base64.encode64(::Elasticsearch::API.serializer.dump(item)) },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't get how this one is related

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the serialization optimization mentioned above. This uses the serializer Elasticsearch decided to use, instead of the slow default one.

created_at: Time.now.utc
}
end
Expand Down