From 20fd54e9b29640905b69f6213b95ba9ab09bb033 Mon Sep 17 00:00:00 2001 From: pyromaniac Date: Sat, 24 Jun 2017 19:23:50 +0700 Subject: [PATCH 1/4] Rename derivable_index_name -> derivable_name --- CHANGELOG.md | 7 ++++++- lib/chewy.rb | 2 +- lib/chewy/index.rb | 9 +++++++-- lib/chewy/index/specification.rb | 4 ++-- lib/chewy/journal.rb | 10 +++++----- lib/chewy/journal/query.rb | 2 +- lib/chewy/rake_helper.rb | 6 +++--- lib/chewy/search/request.rb | 4 ++-- lib/chewy/type.rb | 6 +++--- spec/chewy/index_spec.rb | 6 ++++++ spec/chewy/journal/entry_spec.rb | 2 +- spec/chewy/type_spec.rb | 21 +++++++++++++++------ spec/chewy_spec.rb | 2 -- 13 files changed, 52 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7617be735..8fa81d446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Changes + * Add `Chewy::Type.derivable_name` for consistency + + * Rename `Chewy::Index.derivable_index_name` to `Chewy::Index.derivable_name`. + `Chewy::Index.derivable_index_name` and `Chewy::Type.derivable_index_name` are deprecated. + * Use normal YAML loading, for the config, we don't need the safe one. * Consistency checks and synchronization: `rake chewy:sync`. @@ -14,7 +19,7 @@ * Brand new request DSL. Supports ElasticSearch 2 and 5, better usability, architecture and docs. - * Kaminari 1.0 support. + * Add Kaminari 1.0 support. * `skip_index_creation_on_import` option (@sergey-kintsel, #483) diff --git a/lib/chewy.rb b/lib/chewy.rb index 43c26613e..201a51c5d 100644 --- a/lib/chewy.rb +++ b/lib/chewy.rb @@ -96,7 +96,7 @@ def derive_type(name) return name if name.is_a?(Class) && name < Chewy::Type types = derive_types(name) - raise Chewy::UnderivableType, "Index `#{types.first.index}` has more than one type, please specify type via `#{types.first.index.derivable_index_name}#type_name`" unless types.one? + raise Chewy::UnderivableType, "Index `#{types.first.index}` has more than one type, please specify type via `#{types.first.index.derivable_name}#type_name`" unless types.one? types.first end diff --git a/lib/chewy/index.rb b/lib/chewy/index.rb index 5bcb1a4d4..8ab8364eb 100644 --- a/lib/chewy/index.rb +++ b/lib/chewy/index.rb @@ -44,13 +44,18 @@ def _index_name(suggest = nil) if suggest @_index_name = suggest elsif name - @_index_name ||= name.sub(/Index\Z/, '').demodulize.underscore + @_index_name ||= name.sub(/Index\z/, '').demodulize.underscore end @_index_name end + def derivable_name + @derivable_name ||= name.sub(/Index\z/, '').underscore if name + end + def derivable_index_name - @_derivable_index_name ||= name.sub(/Index\Z/, '').underscore + ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead' + derivable_name end # Setups or returns pure Elasticsearch index name diff --git a/lib/chewy/index/specification.rb b/lib/chewy/index/specification.rb index 9c5cb8b92..476a48b47 100644 --- a/lib/chewy/index/specification.rb +++ b/lib/chewy/index/specification.rb @@ -23,7 +23,7 @@ def initialize(index) # @return [true] if everything is fine def lock! Chewy::Stash::Specification.import!([ - id: @index.derivable_index_name, + id: @index.derivable_name, value: current.to_json ], journal: false) end @@ -33,7 +33,7 @@ def lock! # # @return [Hash] hash produced with JSON parser def locked - filter = {ids: {values: [@index.derivable_index_name]}} + filter = {ids: {values: [@index.derivable_name]}} JSON.parse(Chewy::Stash::Specification.filter(filter).first.try!(:value) || '{}') end diff --git a/lib/chewy/journal.rb b/lib/chewy/journal.rb index a328d4430..f79f203d5 100644 --- a/lib/chewy/journal.rb +++ b/lib/chewy/journal.rb @@ -17,9 +17,9 @@ class Journal } }.freeze - def initialize(index) + def initialize(type) @entries = [] - @index = index + @type = type end def add(action_objects) @@ -27,8 +27,8 @@ def add(action_objects) next if objects.blank? { - index_name: @index.derivable_index_name, - type_name: @index.type_name, + index_name: @type.index.derivable_name, + type_name: @type.type_name, action: action, object_ids: identify(objects), created_at: Time.now.to_i @@ -51,7 +51,7 @@ def bulk_body private def identify(objects) - @index.adapter.identify(objects) + @type.adapter.identify(objects) end class << self diff --git a/lib/chewy/journal/query.rb b/lib/chewy/journal/query.rb index c17b121b2..a2e0984c2 100644 --- a/lib/chewy/journal/query.rb +++ b/lib/chewy/journal/query.rb @@ -44,7 +44,7 @@ def index_filter return if @indices.blank? { terms: { - index_name: @indices.map(&:derivable_index_name) + index_name: @indices.map(&:derivable_name) } } end diff --git a/lib/chewy/rake_helper.rb b/lib/chewy/rake_helper.rb index fa0b97d57..45edae532 100644 --- a/lib/chewy/rake_helper.rb +++ b/lib/chewy/rake_helper.rb @@ -96,7 +96,7 @@ def update(only: nil, except: nil, output: STDOUT) types.each(&:import) update_types.concat(types) else - output.puts "Skipping #{index}, it does not exists (use rake chewy:reset[#{index.derivable_index_name}] to create and update it)" + output.puts "Skipping #{index}, it does not exists (use rake chewy:reset[#{index.derivable_name}] to create and update it)" end end end @@ -195,7 +195,7 @@ def indexes_from(only: nil, except: nil) indexes end - indexes.sort_by(&:derivable_index_name) + indexes.sort_by(&:derivable_name) end def types_from(only: nil, except: nil) @@ -212,7 +212,7 @@ def types_from(only: nil, except: nil) end types.sort_by do |type| - [type.index.derivable_index_name, type.type_name] + [type.index.derivable_name, type.type_name] end.group_by(&:index) end diff --git a/lib/chewy/search/request.rb b/lib/chewy/search/request.rb index da4a2d966..02592f950 100644 --- a/lib/chewy/search/request.rb +++ b/lib/chewy/search/request.rb @@ -933,8 +933,8 @@ def initialize_clone(origin) private def compare_internals(other) - _indexes.map(&:index_name).sort == other._indexes.map(&:index_name).sort && - _types.map(&:full_name).sort == other._types.map(&:full_name).sort && + _indexes.sort_by(&:derivable_name) == other._indexes.sort_by(&:derivable_name) && + _types.sort_by(&:derivable_name) == other._types.sort_by(&:derivable_name) && parameters == other.parameters end diff --git a/lib/chewy/type.rb b/lib/chewy/type.rb index b5cf86b6e..c0fafb0f5 100644 --- a/lib/chewy/type.rb +++ b/lib/chewy/type.rb @@ -34,7 +34,7 @@ class << self # Chewy index current type belongs to. Defined inside `Chewy.create_type` # def index - raise NotImplementedError + raise NotImplementedError, 'Looks like this type ws defined outside the index scope and `.index` method is undefined for it' end # Current type adapter. Defined inside `Chewy.create_type`, derived from @@ -52,8 +52,8 @@ def type_name # Returns index and type names as a string identifier # - def full_name - @full_name ||= [index_name, type_name].join('#') + def derivable_name + @derivable_name ||= [index.derivable_name, type_name].join('#') if index && index.derivable_name end # Returns list of public class methods defined in current type diff --git a/spec/chewy/index_spec.rb b/spec/chewy/index_spec.rb index 709bb3adb..1319fd5d0 100644 --- a/spec/chewy/index_spec.rb +++ b/spec/chewy/index_spec.rb @@ -68,6 +68,12 @@ end end + describe '.derivable_name' do + specify { expect(Class.new(Chewy::Index).derivable_name).to be_nil } + specify { expect(stub_index(:places).derivable_name).to eq('places') } + specify { expect(stub_index('namespace/places').derivable_name).to eq('namespace/places') } + end + describe '.default_prefix' do before { allow(Chewy).to receive_messages(configuration: {prefix: 'testing'}) } specify { expect(Class.new(Chewy::Index).default_prefix).to eq('testing') } diff --git a/spec/chewy/journal/entry_spec.rb b/spec/chewy/journal/entry_spec.rb index 95e56fe05..a3c235c36 100644 --- a/spec/chewy/journal/entry_spec.rb +++ b/spec/chewy/journal/entry_spec.rb @@ -33,7 +33,7 @@ end context do - let(:indices) { [CityIndex::City2] } + let(:indices) { [CityIndex] } before do stub_index('city') do define_type :city2 do diff --git a/spec/chewy/type_spec.rb b/spec/chewy/type_spec.rb index b003c0927..ada806d68 100644 --- a/spec/chewy/type_spec.rb +++ b/spec/chewy/type_spec.rb @@ -1,14 +1,23 @@ require 'spec_helper' describe Chewy::Type do - describe '.full_name' do - before do - stub_index(:places) do - define_type :city - end + describe '.derivable_name' do + specify { expect { Class.new(Chewy::Type).derivable_name }.to raise_error NotImplementedError } + + specify do + index = Class.new(Chewy::Index) { define_type :city } + expect(index::City.derivable_name).to be_nil end - specify { expect(PlacesIndex::City.full_name).to eq('places#city') } + specify do + stub_index(:places) { define_type :city } + expect(PlacesIndex::City.derivable_name).to eq('places#city') + end + + specify do + stub_index('namespace/places') { define_type :city } + expect(Namespace::PlacesIndex::City.derivable_name).to eq('namespace/places#city') + end end describe '.scopes' do diff --git a/spec/chewy_spec.rb b/spec/chewy_spec.rb index 63aa7d345..47b2021c5 100644 --- a/spec/chewy_spec.rb +++ b/spec/chewy_spec.rb @@ -90,7 +90,6 @@ it { is_expected.to be < Chewy::Type } its(:name) { should == 'CitiesIndex::City' } its(:index) { should == CitiesIndex } - its(:derivable_index_name) { should == 'cities' } its(:type_name) { should == 'city' } end @@ -104,7 +103,6 @@ it { is_expected.to be < Chewy::Type } its(:name) { should == 'Namespace::CitiesIndex::City' } its(:index) { should == Namespace::CitiesIndex } - its(:derivable_index_name) { should == 'namespace/cities' } its(:type_name) { should == 'city' } end From ccadb4ff6dcd744df65b41e23e30e38b6b7345dd Mon Sep 17 00:00:00 2001 From: pyromaniac Date: Sat, 24 Jun 2017 22:02:23 +0700 Subject: [PATCH 2/4] Rename default_prefix -> prefix --- CHANGELOG.md | 2 ++ lib/chewy/index.rb | 33 ++++++++++++++++++++++++++------- spec/chewy/index_spec.rb | 26 ++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fa81d446..67e7b62f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Changes + * Rename `Chewy::Index.default_prefix` to `Chewy::Index.prefix`. The old one is deprecated. + * Add `Chewy::Type.derivable_name` for consistency * Rename `Chewy::Index.derivable_index_name` to `Chewy::Index.derivable_name`. diff --git a/lib/chewy/index.rb b/lib/chewy/index.rb index 8ab8364eb..0989703d3 100644 --- a/lib/chewy/index.rb +++ b/lib/chewy/index.rb @@ -36,7 +36,7 @@ def index_name(suggest = nil) @index_name = nil _index_name(suggest) else - @index_name ||= build_index_name(_index_name, prefix: default_prefix) + @index_name ||= build_index_name(_index_name, prefix: prefix_with_deprecation) end end @@ -53,14 +53,9 @@ def derivable_name @derivable_name ||= name.sub(/Index\z/, '').underscore if name end - def derivable_index_name - ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead' - derivable_name - end - # Setups or returns pure Elasticsearch index name # without any prefixes/suffixes - def default_prefix + def prefix Chewy.configuration[:prefix] end @@ -204,6 +199,30 @@ def index_params def specification @specification ||= Specification.new(self) end + + def derivable_index_name + ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead' + derivable_name + end + + # Handling old default_prefix if it is not defined. + def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing + if name == :default_prefix + ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, use `Chewy::Index.prefix` instead' + prefix + else + super + end + end + + def prefix_with_deprecation + if respond_to?(:default_prefix) + ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, define `Chewy::Index.prefix` method instead' + default_prefix + else + prefix + end + end end end end diff --git a/spec/chewy/index_spec.rb b/spec/chewy/index_spec.rb index 1319fd5d0..79be58170 100644 --- a/spec/chewy/index_spec.rb +++ b/spec/chewy/index_spec.rb @@ -74,9 +74,9 @@ specify { expect(stub_index('namespace/places').derivable_name).to eq('namespace/places') } end - describe '.default_prefix' do + describe '.prefix' do before { allow(Chewy).to receive_messages(configuration: {prefix: 'testing'}) } - specify { expect(Class.new(Chewy::Index).default_prefix).to eq('testing') } + specify { expect(Class.new(Chewy::Index).prefix).to eq('testing') } end describe '.define_type' do @@ -298,4 +298,26 @@ def self.by_id; end specify { expect(index.journal?).to eq true } end end + + describe '.default_prefix' do + before { allow(Chewy).to receive_messages(configuration: {prefix: 'testing'}) } + + context do + before { expect(ActiveSupport::Deprecation).to receive(:warn).once } + specify { expect(DummiesIndex.default_prefix).to eq('testing') } + end + + context do + before do + DummiesIndex.class_eval do + def self.default_prefix + 'borogoves' + end + end + end + + before { expect(ActiveSupport::Deprecation).to receive(:warn).once } + specify { expect(DummiesIndex.index_name).to eq('borogoves_dummies') } + end + end end From 063509beb8e0273954e76b4435db4e7414487c70 Mon Sep 17 00:00:00 2001 From: pyromaniac Date: Sun, 25 Jun 2017 12:28:18 +0700 Subject: [PATCH 3/4] Rework index_name to be more dynamic --- CHANGELOG.md | 28 ++++--- lib/chewy/index.rb | 106 +++++++++++++++++++------- lib/chewy/index/actions.rb | 8 +- lib/chewy/type.rb | 12 ++- lib/chewy/type/import/bulk_request.rb | 2 +- lib/chewy/type/syncer.rb | 2 +- spec/chewy/index/actions_spec.rb | 4 +- spec/chewy/index_spec.rb | 14 ++-- spec/chewy_spec.rb | 2 + 9 files changed, 122 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67e7b62f5..a815de837 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,16 @@ # master +## Breaking changes + + * Changed behavior of `Chewy::Index.index_name`, it doesn't cache the values anymore. + ## Changes + * Deprecate `Chewy::Index.build_index_name`. + * Rename `Chewy::Index.default_prefix` to `Chewy::Index.prefix`. The old one is deprecated. - * Add `Chewy::Type.derivable_name` for consistency + * Add `Chewy::Type.derivable_name` for consistency. * Rename `Chewy::Index.derivable_index_name` to `Chewy::Index.derivable_name`. `Chewy::Index.derivable_index_name` and `Chewy::Type.derivable_index_name` are deprecated. @@ -115,6 +121,12 @@ # Version 0.8.3 +## Breaking changes: + + * `Chewy.atomic` and `Chewy.urgent_update=` methods was removed from the codebase, use `Chewy.strategy` block instead. + + * `delete_from_index?` hook is removed from the codebase. + ## Changes * Sequel support completely reworked to use common ORM implementations + better sequel specs covarage. @@ -131,12 +143,6 @@ * Correct custom assets path silencer (@davekaro) -## Incompatible changes: - - * `Chewy.atomic` and `Chewy.urgent_update=` methods was removed from the codebase, use `Chewy.strategy` block instead. - - * `delete_from_index?` hook is removed from the codebase. - # Version 0.8.2 ## Changes @@ -172,7 +178,7 @@ # Version 0.8.0 -## Incompatible changes: +## Breaking changes: * `:atomic` and `:urgent` strategies are using `import!` method raising exceptions @@ -198,7 +204,7 @@ # Version 0.7.0 -## Incompatible changes: +## Breaking changes: * `Chewy.use_after_commit_callbacks = false` returns previous RDBMS behavior in tests @@ -347,7 +353,7 @@ # Version 0.5.2 -## Incompatible changes: +## Breaking changes: * `Chewy::Type::Base` removed in favour of using `Chewy::Type` as a base class for all types @@ -387,7 +393,7 @@ # Version 0.5.0 -## Incompatible changes: +## Breaking changes: * 404 exception (IndexMissingException) while query is swallowed and treated like an empty result set. diff --git a/lib/chewy/index.rb b/lib/chewy/index.rb index 0989703d3..a3b198873 100644 --- a/lib/chewy/index.rb +++ b/lib/chewy/index.rb @@ -19,42 +19,94 @@ class Index self._settings = Chewy::Index::Settings.new class << self - # Setups or returns ElasticSearch index name + # @overload index_name(suggest) + # If suggested name is passed, it is set up as the new base name for + # the index. Used for the index base name redefinition. # - # class UsersIndex < Chewy::Index - # end - # UsersIndex.index_name # => 'users' + # @example + # class UsersIndex < Chewy::Index + # index_name :legacy_users + # end + # UsersIndex.index_name # => 'legacy_users' # - # class UsersIndex < Chewy::Index - # index_name 'dudes' - # end - # UsersIndex.index_name # => 'dudes' + # @param suggest [String, Symbol] suggested base name + # @return [String] new base name + # + # @overload index_name(prefix: nil, suffix: nil) + # If suggested name is not passed, returns the base name accompanied + # with the prefix (if any) and suffix (if passed). + # + # @example + # class UsersIndex < Chewy::Index + # end + # + # Chewy.settings = {prefix: 'test'} + # UsersIndex.index_name # => 'test_users' + # UsersIndex.index_name(prefix: 'foobar') # => 'foobar_users' + # UsersIndex.index_name(suffix: '2017') # => 'test_users_2017' + # UsersIndex.index_name(prefix: '', suffix: '2017') # => 'users_2017' # - def index_name(suggest = nil) - raise UndefinedIndex unless _index_name(suggest) + # @param prefix [String] index name prefix, uses {.prefix} method by default + # @param suffix [String] index name suffix, used for creating several indexes for the same alias during the zero-downtime reset + # @raise [UndefinedIndex] if the base name is blank + # @return [String] result index name + def index_name(suggest = nil, prefix: nil, suffix: nil) if suggest - @index_name = nil - _index_name(suggest) + @base_name = suggest.to_s.underscore.presence else - @index_name ||= build_index_name(_index_name, prefix: prefix_with_deprecation) + [ + prefix || prefix_with_deprecation, + base_name, + suffix + ].reject(&:blank?).join('_') end end - def _index_name(suggest = nil) - if suggest - @_index_name = suggest - elsif name - @_index_name ||= name.sub(/Index\z/, '').demodulize.underscore - end - @_index_name + # Base name for the index. Uses the default value inferred from the + # class name unless redefined. + # + # @example + # class Namespace::UsersIndex < Chewy::Index + # end + # UsersIndex.index_name # => 'users' + # + # Class.new(Chewy::Index).base_name # => raises UndefinedIndex + # + # @raise [UndefinedIndex] when the base name is blank + # @return [String] current base name + def base_name + @base_name ||= name.sub(/Index\z/, '').demodulize.underscore if name + raise UndefinedIndex if @base_name.blank? + @base_name end + # Similar to the {.base_name} but respects the class namespace, also, + # can't be redefined. Used to reference index with the string identifier + # + # @example + # class Namespace::UsersIndex < Chewy::Index + # end + # UsersIndex.derivable_name # => 'namespace/users' + # + # Class.new(Chewy::Index).derivable_name # => nil + # + # @return [String, nil] derivable name or nil when it is impossible to calculate def derivable_name @derivable_name ||= name.sub(/Index\z/, '').underscore if name end - # Setups or returns pure Elasticsearch index name - # without any prefixes/suffixes + # Used as a default value for {.index_name}. Return prefix from the configuration + # but can be redefined per-index to be more dynamic. + # + # @example + # class UsersIndex < Chewy::Index + # def self.prefix + # 'foobar' + # end + # end + # UsersIndex.index_name # => 'foobar_users' + # + # @return [String] prefix def prefix Chewy.configuration[:prefix] end @@ -166,11 +218,6 @@ def journal? types.any?(&:journal?) end - def build_index_name(*args) - options = args.extract_options! - [options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join('_') - end - def settings_hash _settings.to_hash end @@ -223,6 +270,11 @@ def prefix_with_deprecation prefix end end + + def build_index_name(*args) + ActiveSupport::Deprecation.warn '`Chewy::Index.build_index_name` is deprecated and will be removed soon, use `Chewy::Index.index_name` instead' + index_name(args.extract_options!) + end end end end diff --git a/lib/chewy/index/actions.rb b/lib/chewy/index/actions.rb index e981b39ee..5073de242 100644 --- a/lib/chewy/index/actions.rb +++ b/lib/chewy/index/actions.rb @@ -54,7 +54,7 @@ def create(*args) # def create!(*args) options = args.extract_options!.reverse_merge!(alias: true) - name = build_index_name(suffix: args.first) + name = index_name(suffix: args.first) if Chewy::Runtime.version >= 1.1 body = specification_hash @@ -78,7 +78,7 @@ def create!(*args) # UsersIndex.delete '01-2014' # deletes `users_01-2014` index # def delete(suffix = nil) - result = client.indices.delete index: build_index_name(suffix: suffix) + result = client.indices.delete index: index_name(suffix: suffix) Chewy.wait_for_status if result result # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound @@ -172,7 +172,7 @@ def reset!(suffix = nil, journal: false) *indexes.map do |index| {remove: {index: index, alias: index_name}} end, - {add: {index: build_index_name(suffix: suffix), alias: index_name}} + {add: {index: index_name(suffix: suffix), alias: index_name}} ]} client.indices.delete index: indexes if indexes.present? result @@ -206,7 +206,7 @@ def original_index_settings(suffix) def update_settings(*args) options = args.extract_options! - name = build_index_name suffix: options[:suffix] + name = index_name suffix: options[:suffix] client.indices.put_settings index: name, body: {index: options[:settings]} end diff --git a/lib/chewy/type.rb b/lib/chewy/type.rb index c0fafb0f5..994a24ed5 100644 --- a/lib/chewy/type.rb +++ b/lib/chewy/type.rb @@ -25,7 +25,7 @@ class Type include Witchcraft include Import - singleton_class.delegate :index_name, :_index_name, :derivable_index_name, :client, to: :index + singleton_class.delegate :index_name, :derivable_index_name, :client, to: :index class_attribute :_default_import_options self._default_import_options = {} @@ -50,8 +50,16 @@ def type_name adapter.type_name end - # Returns index and type names as a string identifier + # Appends type name to {Chewy::Index.derivable_name} # + # @example + # class Namespace::UsersIndex < Chewy::Index + # define_type User + # end + # UsersIndex::User.derivable_name # => 'namespace/users#user' + # + # @see Chewy::Index.derivable_name + # @return [String, nil] derivable name or nil when it is impossible to calculate def derivable_name @derivable_name ||= [index.derivable_name, type_name].join('#') if index && index.derivable_name end diff --git a/lib/chewy/type/import/bulk_request.rb b/lib/chewy/type/import/bulk_request.rb index e01bb75b0..f3e40e23f 100644 --- a/lib/chewy/type/import/bulk_request.rb +++ b/lib/chewy/type/import/bulk_request.rb @@ -47,7 +47,7 @@ def perform(body) def request_base @request_base ||= { - index: @type.index.build_index_name(suffix: @suffix), + index: @type.index_name(suffix: @suffix), type: @type.type_name }.merge!(@bulk_options) end diff --git a/lib/chewy/type/syncer.rb b/lib/chewy/type/syncer.rb index bbc2aa0d4..39cfb7143 100644 --- a/lib/chewy/type/syncer.rb +++ b/lib/chewy/type/syncer.rb @@ -105,7 +105,7 @@ def outdated_sync_field_type return @outdated_sync_field_type if instance_variable_defined?(:@outdated_sync_field_type) mappings = @type.client.indices.get_mapping( - index: @type.index.index_name, + index: @type.index_name, type: @type.type_name ).values.first.fetch('mappings', {}) diff --git a/spec/chewy/index/actions_spec.rb b/spec/chewy/index/actions_spec.rb index 38309a7be..ec946bfc5 100644 --- a/spec/chewy/index/actions_spec.rb +++ b/spec/chewy/index/actions_spec.rb @@ -383,7 +383,7 @@ context 'reset_disable_refresh_interval' do let(:suffix) { Time.now.to_i } - let(:name) { CitiesIndex.build_index_name(suffix: suffix) } + let(:name) { CitiesIndex.index_name(suffix: suffix) } let(:before_import_body) do { index: {refresh_interval: -1} @@ -442,7 +442,7 @@ context 'reset_no_replicas' do let(:suffix) { Time.now.to_i } - let(:name) { CitiesIndex.build_index_name(suffix: suffix) } + let(:name) { CitiesIndex.index_name(suffix: suffix) } let(:before_import_body) do { index: {number_of_replicas: 0} diff --git a/spec/chewy/index_spec.rb b/spec/chewy/index_spec.rb index 79be58170..09b2dac8a 100644 --- a/spec/chewy/index_spec.rb +++ b/spec/chewy/index_spec.rb @@ -61,10 +61,16 @@ specify { expect(stub_const('DeveloperIndex', Class.new(Chewy::Index)).index_name).to eq('developer') } specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).index_name).to eq('developers') } + specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).index_name(suffix: '')).to eq('developers') } + specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).index_name(suffix: '2013')).to eq('developers_2013') } + specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).index_name(prefix: '')).to eq('developers') } + specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).index_name(prefix: 'test')).to eq('test_developers') } + context do before { allow(Chewy).to receive_messages(configuration: {prefix: 'testing'}) } specify { expect(DummiesIndex.index_name).to eq('testing_dummies') } specify { expect(stub_index(:dummies) { index_name :users }.index_name).to eq('testing_users') } + specify { expect(stub_index(:dummies) { index_name :users }.index_name(prefix: '')).to eq('users') } end end @@ -197,14 +203,6 @@ def self.by_id; end specify { expect(PlacesIndex.scopes).to match_array(%i[by_rating by_name]) } end - describe '.build_index_name' do - specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).build_index_name(suffix: '')).to eq('developers') } - specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).build_index_name(suffix: '2013')).to eq('developers_2013') } - specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).build_index_name(prefix: '')).to eq('developers') } - specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).build_index_name(prefix: 'test')).to eq('test_developers') } - specify { expect(stub_const('DevelopersIndex', Class.new(Chewy::Index)).build_index_name(:users, prefix: 'test', suffix: '2013')).to eq('test_users_2013') } - end - describe '.settings_hash' do before { allow(Chewy).to receive_messages(config: Chewy::Config.send(:new)) } diff --git a/spec/chewy_spec.rb b/spec/chewy_spec.rb index 47b2021c5..b537adfc2 100644 --- a/spec/chewy_spec.rb +++ b/spec/chewy_spec.rb @@ -130,6 +130,8 @@ stub_index(:companies).create! Chewy.massacre + + allow(Chewy).to receive_messages(configuration: Chewy.configuration.merge(prefix: 'prefix1')) end specify { expect(AdminsIndex.exists?).to eq(true) } From ed4c2e582833ac9a9e4fb9ba8e976a10114d300a Mon Sep 17 00:00:00 2001 From: pyromaniac Date: Mon, 26 Jun 2017 10:21:25 +0700 Subject: [PATCH 4/4] Cache index_name locally to avoid its changing --- lib/chewy/index/actions.rb | 40 ++++++++++++++++++++------------------ lib/chewy/index/aliases.rb | 3 ++- lib/chewy/query.rb | 2 +- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/chewy/index/actions.rb b/lib/chewy/index/actions.rb index 5073de242..01c40331b 100644 --- a/lib/chewy/index/actions.rb +++ b/lib/chewy/index/actions.rb @@ -52,17 +52,18 @@ def create(*args) # Suffixed index names might be used for zero-downtime mapping change, for example. # Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/). # - def create!(*args) - options = args.extract_options!.reverse_merge!(alias: true) - name = index_name(suffix: args.first) + def create!(suffix = nil, **options) + options.reverse_merge!(alias: true) + general_name = index_name + suffixed_name = index_name(suffix: suffix) if Chewy::Runtime.version >= 1.1 body = specification_hash - body[:aliases] = {index_name => {}} if options[:alias] && name != index_name - result = client.indices.create(index: name, body: body) + body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name + result = client.indices.create(index: suffixed_name, body: body) else - result = client.indices.create(index: name, body: specification_hash) - result &&= client.indices.put_alias(index: name, name: index_name) if options[:alias] && name != index_name + result = client.indices.create(index: suffixed_name, body: specification_hash) + result &&= client.indices.put_alias(index: suffixed_name, name: general_name) if options[:alias] && name != index_name end Chewy.wait_for_status if result @@ -164,15 +165,18 @@ def reset!(suffix = nil, journal: false) result = if suffix.present? && (indexes = self.indexes).present? create! suffix, alias: false - optimize_index_settings suffix + general_name = index_name + suffixed_name = index_name(suffix: suffix) + + optimize_index_settings suffixed_name result = import suffix: suffix, journal: journal, refresh: !Chewy.reset_disable_refresh_interval - original_index_settings suffix + original_index_settings suffixed_name client.indices.update_aliases body: {actions: [ *indexes.map do |index| - {remove: {index: index, alias: index_name}} + {remove: {index: index, alias: general_name}} end, - {add: {index: index_name(suffix: suffix), alias: index_name}} + {add: {index: suffixed_name, alias: general_name}} ]} client.indices.delete index: indexes if indexes.present? result @@ -187,27 +191,25 @@ def reset!(suffix = nil, journal: false) private - def optimize_index_settings(suffix) + def optimize_index_settings(index_name) settings = {} settings[:refresh_interval] = -1 if Chewy.reset_disable_refresh_interval settings[:number_of_replicas] = 0 if Chewy.reset_no_replicas - update_settings suffix: suffix, settings: settings if settings.any? + update_settings index_name, settings: settings if settings.any? end - def original_index_settings(suffix) + def original_index_settings(index_name) settings = {} if Chewy.reset_disable_refresh_interval settings.merge! index_settings(:refresh_interval) settings[:refresh_interval] = '1s' if settings.empty? end settings.merge! index_settings(:number_of_replicas) if Chewy.reset_no_replicas - update_settings suffix: suffix, settings: settings if settings.any? + update_settings index_name, settings: settings if settings.any? end - def update_settings(*args) - options = args.extract_options! - name = index_name suffix: options[:suffix] - client.indices.put_settings index: name, body: {index: options[:settings]} + def update_settings(index_name, **options) + client.indices.put_settings index: index_name, body: {index: options[:settings]} end def index_settings(setting_name) diff --git a/lib/chewy/index/aliases.rb b/lib/chewy/index/aliases.rb index 6d6e2277d..6a8a93b1f 100644 --- a/lib/chewy/index/aliases.rb +++ b/lib/chewy/index/aliases.rb @@ -11,7 +11,8 @@ def indexes end def aliases - client.indices.get_alias(index: index_name, name: '*')[index_name].try(:[], 'aliases').try(:keys) || [] + name = index_name + client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || [] rescue Elasticsearch::Transport::Transport::Errors::NotFound [] end diff --git a/lib/chewy/query.rb b/lib/chewy/query.rb index e4b7a7db9..413276026 100644 --- a/lib/chewy/query.rb +++ b/lib/chewy/query.rb @@ -1045,7 +1045,7 @@ def reset def _request @_request ||= begin request = criteria.request_body - request[:index] = _indexes.map(&:index_name) + request[:index] = _indexes_hash.keys request[:type] = _types.map(&:type_name) request end