Skip to content

[MODEL] Only add the document_type to the request if it's defined #894

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

Merged
merged 1 commit into from
Jul 26, 2019
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
51 changes: 26 additions & 25 deletions elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,13 @@ def self.included(base)
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:index
#
def index_document(options={})
document = self.as_indexed_json

client.index(
{ index: index_name,
type: document_type,
id: self.id,
body: document }.merge(options)
)
document = as_indexed_json
request = { index: index_name,
id: id,
body: document }
request.merge!(type: document_type) if document_type

client.index(request.merge!(options))
end

# Deletes the model instance from the index
Expand All @@ -392,11 +391,11 @@ def index_document(options={})
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:delete
#
def delete_document(options={})
client.delete(
{ index: index_name,
type: document_type,
id: self.id }.merge(options)
)
request = { index: index_name,
id: self.id }
request.merge!(type: document_type) if document_type

client.delete(request.merge!(options))
end

# Tries to gather the changed attributes of a model instance
Expand Down Expand Up @@ -431,12 +430,14 @@ def update_document(options={})
attributes_in_database
end

client.update(
{ index: index_name,
type: document_type,
id: self.id,
body: { doc: attributes } }.merge(options)
) unless attributes.empty?
unless attributes.empty?
request = { index: index_name,
id: self.id,
body: { doc: attributes } }
request.merge!(type: document_type) if document_type

client.update(request.merge!(options))
end
else
index_document(options)
end
Expand All @@ -457,12 +458,12 @@ def update_document(options={})
# @return [Hash] The response from Elasticsearch
#
def update_document_attributes(attributes, options={})
client.update(
{ index: index_name,
type: document_type,
id: self.id,
body: { doc: attributes } }.merge(options)
)
request = { index: index_name,
id: self.id,
body: { doc: attributes } }
request.merge!(type: document_type) if document_type

client.update(request.merge!(options))
end
end

Expand Down
6 changes: 3 additions & 3 deletions elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def changes
expect(instance).to receive(:client).and_return(client)
expect(instance).to receive(:as_indexed_json).and_return('JSON')
expect(instance).to receive(:index_name).and_return('foo')
expect(instance).to receive(:document_type).and_return('bar')
expect(instance).to receive(:document_type).twice.and_return('bar')
expect(instance).to receive(:id).and_return('1')
end

Expand Down Expand Up @@ -458,7 +458,7 @@ def changes
before do
expect(instance).to receive(:client).and_return(client)
expect(instance).to receive(:index_name).and_return('foo')
expect(instance).to receive(:document_type).and_return('bar')
expect(instance).to receive(:document_type).twice.and_return('bar')
expect(instance).to receive(:id).and_return('1')
end

Expand Down Expand Up @@ -602,7 +602,7 @@ def changes
before do
expect(instance).to receive(:client).and_return(client)
expect(instance).to receive(:index_name).and_return('foo')
expect(instance).to receive(:document_type).and_return('bar')
expect(instance).to receive(:document_type).twice.and_return('bar')
expect(instance).to receive(:id).and_return('1')
instance.instance_variable_set(:@__changed_model_attributes, { author: 'john' })
end
Expand Down