Skip to content

Commit 62191f8

Browse files
authored
[MODEL] Only add the document_type to the request if it's defined (#894)
1 parent f9a87ff commit 62191f8

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

Diff for: elasticsearch-model/lib/elasticsearch/model/indexing.rb

+26-25
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,13 @@ def self.included(base)
368368
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:index
369369
#
370370
def index_document(options={})
371-
document = self.as_indexed_json
372-
373-
client.index(
374-
{ index: index_name,
375-
type: document_type,
376-
id: self.id,
377-
body: document }.merge(options)
378-
)
371+
document = as_indexed_json
372+
request = { index: index_name,
373+
id: id,
374+
body: document }
375+
request.merge!(type: document_type) if document_type
376+
377+
client.index(request.merge!(options))
379378
end
380379

381380
# Deletes the model instance from the index
@@ -392,11 +391,11 @@ def index_document(options={})
392391
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:delete
393392
#
394393
def delete_document(options={})
395-
client.delete(
396-
{ index: index_name,
397-
type: document_type,
398-
id: self.id }.merge(options)
399-
)
394+
request = { index: index_name,
395+
id: self.id }
396+
request.merge!(type: document_type) if document_type
397+
398+
client.delete(request.merge!(options))
400399
end
401400

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

434-
client.update(
435-
{ index: index_name,
436-
type: document_type,
437-
id: self.id,
438-
body: { doc: attributes } }.merge(options)
439-
) unless attributes.empty?
433+
unless attributes.empty?
434+
request = { index: index_name,
435+
id: self.id,
436+
body: { doc: attributes } }
437+
request.merge!(type: document_type) if document_type
438+
439+
client.update(request.merge!(options))
440+
end
440441
else
441442
index_document(options)
442443
end
@@ -457,12 +458,12 @@ def update_document(options={})
457458
# @return [Hash] The response from Elasticsearch
458459
#
459460
def update_document_attributes(attributes, options={})
460-
client.update(
461-
{ index: index_name,
462-
type: document_type,
463-
id: self.id,
464-
body: { doc: attributes } }.merge(options)
465-
)
461+
request = { index: index_name,
462+
id: self.id,
463+
body: { doc: attributes } }
464+
request.merge!(type: document_type) if document_type
465+
466+
client.update(request.merge!(options))
466467
end
467468
end
468469

Diff for: elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def changes
418418
expect(instance).to receive(:client).and_return(client)
419419
expect(instance).to receive(:as_indexed_json).and_return('JSON')
420420
expect(instance).to receive(:index_name).and_return('foo')
421-
expect(instance).to receive(:document_type).and_return('bar')
421+
expect(instance).to receive(:document_type).twice.and_return('bar')
422422
expect(instance).to receive(:id).and_return('1')
423423
end
424424

@@ -458,7 +458,7 @@ def changes
458458
before do
459459
expect(instance).to receive(:client).and_return(client)
460460
expect(instance).to receive(:index_name).and_return('foo')
461-
expect(instance).to receive(:document_type).and_return('bar')
461+
expect(instance).to receive(:document_type).twice.and_return('bar')
462462
expect(instance).to receive(:id).and_return('1')
463463
end
464464

@@ -602,7 +602,7 @@ def changes
602602
before do
603603
expect(instance).to receive(:client).and_return(client)
604604
expect(instance).to receive(:index_name).and_return('foo')
605-
expect(instance).to receive(:document_type).and_return('bar')
605+
expect(instance).to receive(:document_type).twice.and_return('bar')
606606
expect(instance).to receive(:id).and_return('1')
607607
instance.instance_variable_set(:@__changed_model_attributes, { author: 'john' })
608608
end

0 commit comments

Comments
 (0)