Skip to content

Commit ddc4726

Browse files
committed
[STORE] Added, that _source is accessible from a model instance
1 parent ca664ed commit ddc4726

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/model.rb

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def deserialize(document)
108108
object.instance_variable_set :@_index, document['_index']
109109
object.instance_variable_set :@_type, document['_type']
110110
object.instance_variable_set :@_version, document['_version']
111+
object.instance_variable_set :@_source, document['_source']
111112

112113
# Store the "hit" information (highlighting, score, ...)
113114
#

elasticsearch-persistence/lib/elasticsearch/persistence/model/base.rb

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def _version
4949
@_version
5050
end
5151

52+
# Return the raw document `_source`
53+
#
54+
def _source
55+
@_source
56+
end
57+
5258
def to_s
5359
"#<#{self.class} #{attributes.to_hash.inspect.gsub(/:(\w+)=>/, '\1: ')}>"
5460
end; alias :inspect :to_s

elasticsearch-persistence/test/unit/model_gateway_test.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ def run!; DummyGatewayModel.gateway { |g| @b += 1 }; end
8989
assert DummyGatewayModel.gateway.deserialize('_id' => 'abc123', '_source' => {}).instance_variable_get(:@persisted)
9090
end
9191

92+
should "allow accessing the raw _source" do
93+
assert_equal 'bar', DummyGatewayModel.gateway.deserialize('_source' => { 'foo' => 'bar' })._source['foo']
94+
end
95+
9296
should "allow to access the raw hit from results as Hashie::Mash" do
9397
assert_equal 0.42, DummyGatewayModel.gateway.deserialize('_score' => 0.42, '_source' => {}).hit._score
9498
end
9599

96-
97-
98100
end
99101
end

0 commit comments

Comments
 (0)