Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

update document should update with hash from as_indexed_json. #182

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

Closed
dwkoogt wants to merge 5 commits into elastic:master from dwkoogt:update_changed_index
Closed
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
4 changes: 3 additions & 1 deletion elasticsearch-model/lib/elasticsearch/model/indexing.rb
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ def delete_document(options={})
def update_document(options={})
if changed_attributes = self.instance_variable_get(:@__changed_attributes)
attributes = if respond_to?(:as_indexed_json)
self.as_indexed_json.select { |k,v| changed_attributes.keys.map(&:to_s).include? k.to_s }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change for performance? Original version seemed more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original change included with_indifferent_access method which is a rails extension which I removed here.

Meekohi reacted with thumbs up emoji
indexed_json = self.as_indexed_json
(self.attributes.keys.map(&:to_s) - changed_attributes.keys.map(&:to_s)).each{ |k| indexed_json.delete_if{ |a,b| a.to_s == k } }
indexed_json
else
changed_attributes
end
Expand Down
70 changes: 70 additions & 0 deletions elasticsearch-model/test/unit/indexing_test.rb
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def attributes; { :_id => 1, :foo => 'B', :bar => 'D', :baz => 'F' }; end

def changed_attributes; [:foo, :bar]; end

def changes
Expand All @@ -197,6 +199,31 @@ def as_indexed_json(options={})
end
end

class ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJsonWithDerivedAttribute
extend Elasticsearch::Model::Indexing::ClassMethods
include Elasticsearch::Model::Indexing::InstanceMethods

def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def attributes; { :_id => 1, :foo => 'B', :bar => 'D', :baz => 'F' }; end

def changed_attributes; [:foo, :bar]; end

def changes
{:foo => ['A', 'B'], :bar => ['C', 'D']}
end

def as_indexed_json(options={})
{ :foo => 'B', :bar => 'D', :qux => derived_value }
end

def derived_value
'E'
end
end

should "register before_save callback when included" do
::DummyIndexingModelWithCallbacks.expects(:before_save).returns(true)
::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
Expand Down Expand Up @@ -367,6 +394,49 @@ def as_indexed_json(options={})
instance.update_document
end

should "exclude attributes not contained in changed_attributes from custom as_indexed_json during partial update" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new

# Set the fake `changes` hash
instance.instance_variable_set(:@__changed_attributes, {'foo' => 'B' })

# Overload as_indexed_json
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR', 'bar' => 'BAZ' })

client.expects(:update).with do |payload|
assert_equal({'foo' => 'BAR'}, payload[:body][:doc])
true
end

instance.expects(:client).returns(client)
instance.expects(:index_name).returns('foo')
instance.expects(:document_type).returns('bar')
instance.expects(:id).returns('1')

instance.update_document
end

should "include derived attributes from custom as_indexed_json during partial update" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJsonWithDerivedAttribute.new

# Set the fake `changes` hash
instance.instance_variable_set(:@__changed_attributes, {foo: 'B'})

client.expects(:update).with do |payload|
assert_equal({foo: 'B', qux: 'E'}, payload[:body][:doc])
true
end

instance.expects(:client).returns(client)
instance.expects(:index_name).returns('foo')
instance.expects(:document_type).returns('bar')
instance.expects(:id).returns('1')

instance.update_document
end

should "update only the specific attributes" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacks.new
Expand Down

AltStyle によって変換されたページ (->オリジナル) /