lib/esse/backend/index/update.rb in esse-0.0.5 vs lib/esse/backend/index/update.rb in esse-0.1.1

- old
+ new

@@ -25,11 +25,18 @@ # in case of failure # @return [Hash] the elasticsearch response # # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/ def update_mapping!(suffix: index_version, **options) - client.indices.put_mapping(options.merge(index: index_name(suffix: suffix), body: mappings_hash.fetch(Esse::MAPPING_ROOT_KEY))) + Esse::Events.instrument('elasticsearch.update_mapping') do |payload| + body = mappings_hash.fetch(Esse::MAPPING_ROOT_KEY) + if (type = options[:type]) + body = body[type.to_s] || body[type.to_sym] + end + payload[:request] = opts = options.merge(index: index_name(suffix: suffix), body: body) + payload[:response] = client.indices.put_mapping(**opts) + end end # Create or update a mapping # # @option options [String] :type The name of the document type. This field is required for some elasticsearch versions @@ -45,20 +52,19 @@ # unavailable (missing, closed, etc) # @option options [Boolean] :update_all_types Whether to update the mapping for all fields # with the same name across all types # @option options [Time] :timeout Explicit operation timeout # @option options [Boolean] :master_timeout Timeout for connection to master - # @return [Hash, false] the elasticsearch response, or false in case of failure + # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure # # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/ def update_mapping(suffix: index_version, **options) update_mapping!(suffix: suffix, **options) rescue Elasticsearch::Transport::Transport::ServerError - false + { 'errors' => true } end - # Closes the index for read/write operations, updates the index settings, and open it again # # @option options [String] :expand_wildcards Whether to expand wildcard expression to concrete indices that # are open, closed or both. (options: open, closed) # @option options [String] :ignore_indices When performed on multiple indices, allows to ignore @@ -76,18 +82,37 @@ # # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/ def update_settings!(suffix: index_version, **options) response = nil - close!(suffix: suffix) - begin - body = settings_hash(cluster_settings: false).fetch(Esse::SETTING_ROOT_KEY) - response = client.indices.put_settings(options.merge(index: index_name(suffix: suffix), body: body)) - ensure - open!(suffix: suffix) + settings = settings_hash.fetch(Esse::SETTING_ROOT_KEY).transform_keys(&:to_s) + settings.delete('number_of_shards') # Can't change number of shards for an index + analysis = settings.delete('analysis') + + if settings.any? + # When changing the number of replicas the index needs to be open. Changing the number of replicas on a + # closed index might prevent the index to be opened correctly again. + Esse::Events.instrument('elasticsearch.update_settings') do |payload| + payload[:request] = opts = options.merge(index: index_name(suffix: suffix), body: { index: settings }) + payload[:response] = response = client.indices.put_settings(**opts) + end end + if analysis + # It is also possible to define new analyzers for the index. But it is required to close the + # index first and open it after the changes are made. + close!(suffix: suffix) + begin + Esse::Events.instrument('elasticsearch.update_settings') do |payload| + payload[:request] = opts = options.merge(index: index_name(suffix: suffix), body: { analysis: analysis }) + payload[:response] = response = client.indices.put_settings(**opts) + end + ensure + open!(suffix: suffix) + end + end + response end # Closes the index for read/write operations, updates the index settings, and open it again # @@ -100,16 +125,16 @@ # @option options [Boolean] :include_defaults Whether to return all default clusters setting # @option options [Boolean] :preserve_existing Whether to update existing settings. # If set to `true` existing settings on an index remain unchanged, the default is `false` # @option options [Time] :master_timeout Specify timeout for connection to master # @option options [Boolean] :flat_settings Return settings in flat format (default: false) - # @return [Hash, false] the elasticsearch response, false in case of failure + # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure # # @see http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/ def update_settings(suffix: index_version, **options) update_settings!(suffix: suffix, **options) rescue Elasticsearch::Transport::Transport::ServerError - false + { 'errors' => true } end end include InstanceMethods end