From faa4690bd220daee44ba9552157ef2f8642f6f61 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Fri, 2 Aug 2024 13:23:13 -0300 Subject: [PATCH] feat: Add reindex action to transport api --- lib/esse/events.rb | 1 + lib/esse/transport/indices.rb | 25 +++++++++ .../elasticsearch-5/transport/reindex_spec.rb | 8 +++ .../elasticsearch-6/transport/reindex_spec.rb | 8 +++ .../elasticsearch-7/transport/reindex_spec.rb | 8 +++ .../elasticsearch-8/transport/reindex_spec.rb | 8 +++ .../shared_examples/transport_reindex.rb | 54 +++++++++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 spec/esse/integrations/elasticsearch-5/transport/reindex_spec.rb create mode 100644 spec/esse/integrations/elasticsearch-6/transport/reindex_spec.rb create mode 100644 spec/esse/integrations/elasticsearch-7/transport/reindex_spec.rb create mode 100644 spec/esse/integrations/elasticsearch-8/transport/reindex_spec.rb create mode 100644 spec/support/shared_examples/transport_reindex.rb diff --git a/lib/esse/events.rb b/lib/esse/events.rb index fbc1ab5..83e6574 100644 --- a/lib/esse/events.rb +++ b/lib/esse/events.rb @@ -56,5 +56,6 @@ module Events register_event 'elasticsearch.exist' register_event 'elasticsearch.count' register_event 'elasticsearch.get' + register_event 'elasticsearch.reindex' end end diff --git a/lib/esse/transport/indices.rb b/lib/esse/transport/indices.rb index 4e77aa8..58034d7 100644 --- a/lib/esse/transport/indices.rb +++ b/lib/esse/transport/indices.rb @@ -185,6 +185,31 @@ def update_settings(index:, body:, **options) payload[:response] = coerce_exception { client.indices.put_settings(**opts) } end end + + # Allows to copy documents from one index to another, optionally filtering the source + # documents by a query, changing the destination index settings, or fetching the + # documents from a remote cluster. + # + # @option arguments [Boolean] :refresh Should the affected indexes be refreshed? + # @option arguments [Time] :timeout Time each individual bulk request should wait for shards that are unavailable. + # @option arguments [String] :wait_for_active_shards Sets the number of shard copies that must be active before proceeding with the reindex operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1) + # @option arguments [Boolean] :wait_for_completion Should the request should block until the reindex is complete. + # @option arguments [Number] :requests_per_second The throttle to set on this request in sub-requests per second. -1 means no throttle. + # @option arguments [Time] :scroll Control how long to keep the search context alive + # @option arguments [Number|string] :slices The number of slices this task should be divided into. Defaults to 1, meaning the task isn't sliced into subtasks. Can be set to `auto`. + # @option arguments [Number] :max_docs Maximum number of documents to process (default: all documents) + # @option arguments [Hash] :headers Custom HTTP headers + # @option arguments [Hash] :body The search definition using the Query DSL and the prototype for the index request. (*Required*) + # + # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html + def reindex(body:, **options) + throw_error_when_readonly! + + Esse::Events.instrument('elasticsearch.reindex') do |payload| + payload[:request] = opts = options.merge(body: body) + payload[:response] = coerce_exception { client.reindex(**opts) } + end + end end include InstanceMethods diff --git a/spec/esse/integrations/elasticsearch-5/transport/reindex_spec.rb b/spec/esse/integrations/elasticsearch-5/transport/reindex_spec.rb new file mode 100644 index 0000000..d8f7bea --- /dev/null +++ b/spec/esse/integrations/elasticsearch-5/transport/reindex_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'support/shared_examples/transport_reindex' + +stack_describe 'elasticsearch', '5.x', Esse::Transport, '#reindex' do + include_examples 'transport#reindex' +end diff --git a/spec/esse/integrations/elasticsearch-6/transport/reindex_spec.rb b/spec/esse/integrations/elasticsearch-6/transport/reindex_spec.rb new file mode 100644 index 0000000..6bc0409 --- /dev/null +++ b/spec/esse/integrations/elasticsearch-6/transport/reindex_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'support/shared_examples/transport_reindex' + +stack_describe 'elasticsearch', '6.x', Esse::Transport, '#reindex' do + include_examples 'transport#reindex' +end diff --git a/spec/esse/integrations/elasticsearch-7/transport/reindex_spec.rb b/spec/esse/integrations/elasticsearch-7/transport/reindex_spec.rb new file mode 100644 index 0000000..d2900d5 --- /dev/null +++ b/spec/esse/integrations/elasticsearch-7/transport/reindex_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'support/shared_examples/transport_reindex' + +stack_describe 'elasticsearch', '7.x', Esse::Transport, '#reindex' do + include_examples 'transport#reindex' +end diff --git a/spec/esse/integrations/elasticsearch-8/transport/reindex_spec.rb b/spec/esse/integrations/elasticsearch-8/transport/reindex_spec.rb new file mode 100644 index 0000000..4f4d76c --- /dev/null +++ b/spec/esse/integrations/elasticsearch-8/transport/reindex_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'support/shared_examples/transport_reindex' + +stack_describe 'elasticsearch', '8.x', Esse::Transport, '#reindex' do + include_examples 'transport#reindex' +end diff --git a/spec/support/shared_examples/transport_reindex.rb b/spec/support/shared_examples/transport_reindex.rb new file mode 100644 index 0000000..2beeb2c --- /dev/null +++ b/spec/support/shared_examples/transport_reindex.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'transport#reindex' do + let(:body) do + { + settings: { + index: { + number_of_shards: 1, + number_of_replicas: 0 + } + } + } + end + + it 'raises an Esse::Transport::ReadonlyClusterError exception when the cluster is readonly' do + es_client do |client, _conf, cluster| + cluster.warm_up! + expect(client).not_to receive(:perform_request) + cluster.readonly = true + expect { + cluster.api.reindex(body: { source: { index: "#{cluster.index_prefix}_ro_from" }, dest: { index: "#{cluster.index_prefix}_ro_to" } }) + }.to raise_error(Esse::Transport::ReadonlyClusterError) + end + end + + it 'raises an #