Skip to content

Commit

Permalink
more continuing work on changes required for v7.2
Browse files Browse the repository at this point in the history
- option param 'include_type_name_on_create' that control the
:include_type_name argument used when creating indexes
  • Loading branch information
davidbl committed Nov 1, 2019
1 parent 18cd7b0 commit 47b1370
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ v0.13.0
- use timestamp with no colons
- update elasticsearch gem version for consistency with target ES version
- expose refresh_index to force write (because in v7, flush no longer forces writes)
- allow for optional 'include_type_name_on_create' arg so that the :include_type_name can be passed
v0.12.1
- use Arel.sql to avoid unsafe sql and eliminate deprecation warnings when used in Rails projects
v0.12.0
Expand Down
2 changes: 1 addition & 1 deletion lib/elasticity/index_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SubclassError < StandardError; end
VERSION_FOR_SUBCLASS_ERROR = "7.0.0".freeze
ATTRS = [
:index_base_name, :document_type, :mapping, :strategy, :subclasses,
:settings, :use_new_timestamp_format
:settings, :use_new_timestamp_format, :include_type_name_on_create
].freeze
VALIDATABLE_ATTRS = [:index_base_name, :document_type, :strategy].freeze

Expand Down
2 changes: 1 addition & 1 deletion lib/elasticity/index_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.set_delegates(obj, to)
def initialize(document_klass, index_config)
@document_klass = document_klass
@index_config = index_config
@strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type, @index_config.use_new_timestamp_format)
@strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type, @index_config.use_new_timestamp_format, @index_config.include_type_name_on_create)
end

delegate(
Expand Down
8 changes: 5 additions & 3 deletions lib/elasticity/strategies/alias_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ class AliasIndex

STATUSES = [:missing, :ok]

def initialize(client, index_base_name, document_type, use_new_timestamp_format = false)
def initialize(client, index_base_name, document_type, use_new_timestamp_format = false, include_type_name_on_create = true)
@client = client
@main_alias = index_base_name
@update_alias = "#{index_base_name}_update"
@document_type = document_type

# included for compatibility with v7
@use_new_timestamp_format = use_new_timestamp_format
@include_type_name_on_create = include_type_name_on_create
end


def ref_index_name
@main_alias
end
Expand Down Expand Up @@ -298,7 +300,7 @@ def build_index_name

def create_index(index_def)
name = build_index_name
@client.index_create(index: name, body: index_def)
@client.index_create(index: name, body: index_def, include_type_name: @include_type_name_on_create)
name
end

Expand Down
7 changes: 5 additions & 2 deletions lib/elasticity/strategies/single_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module Strategies
class SingleIndex
STATUSES = [:missing, :ok]

def initialize(client, index_name, document_type, use_new_timestamp_format = false)
def initialize(client, index_name, document_type, use_new_timestamp_format = false, include_type_name_on_create = true)
@client = client
@index_name = index_name
@document_type = document_type

# included for compatibility with v7
@include_type_name_on_create = include_type_name_on_create

# not currently used. included for argument compatiblity with AliasStrategy
@use_new_timestamp_format = use_new_timestamp_format
end
Expand All @@ -26,7 +29,7 @@ def missing?

def create(index_def)
if missing?
@client.index_create(index: @index_name, body: index_def)
@client.index_create(index: @index_name, body: index_def, include_type_name: @include_type_name_on_create)
else
raise IndexError.new(@index_name, "index already exist")
end
Expand Down
19 changes: 19 additions & 0 deletions spec/units/index_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ class Multied < Elasticity::Document
end
end

describe "passing the include_type_name option" do
it "allows passing of a include_type_name_on_create option" do
config = described_class.new(elasticity_config, defaults) {}
expect(config.index_base_name).to eql('users')
expect(config.document_type).to eql('user')
expect(config.include_type_name_on_create).to be_falsy

config = described_class.new(elasticity_config, defaults) do |c|
c.index_base_name = 'user_documents'
c.document_type = 'users'
c.include_type_name_on_create = true
end

expect(config.index_base_name).to eql('user_documents')
expect(config.document_type).to eql('users')
expect(config.include_type_name_on_create).to be_truthy
end
end

def stub_version(version)
allow_any_instance_of(Elasticity::InstrumentedClient).to receive(:versions).and_return([version])
end
Expand Down

0 comments on commit 47b1370

Please sign in to comment.