Skip to content

Commit cd9cc67

Browse files
committedMay 23, 2014
[MODEL] Fixes for the change id/type for Response::Result in elastic#90
* Removed the code which returned id based on _source in `method_missing` * Reorganized and renamed some tests * Added a test for demonstrating difference between `result.type` and `result._source.type` (same with `id`) Related: elastic#90
1 parent 5486a87 commit cd9cc67

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed
 

‎elasticsearch-model/lib/elasticsearch/model/response/result.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def initialize(attributes={})
1717
@result = Hashie::Mash.new(attributes)
1818
end
1919

20-
# Alias `id` to `_id`
20+
# Return document `_id` as `id`
2121
#
2222
def id
2323
@result['_id']
2424
end
2525

26-
# Alias `type` to `_type`
26+
# Return document `_type` as `_type`
2727
#
2828
def type
2929
@result['_type']
@@ -35,12 +35,6 @@ def method_missing(name, *arguments)
3535
case
3636
when name.to_s.end_with?('?')
3737
@result.__send__(name, *arguments) || ( @result._source && @result._source.__send__(name, *arguments) )
38-
when name.to_s == "id"
39-
if @result._source && @result._source.respond_to?(name)
40-
@result._source.__send__ name, *arguments
41-
else
42-
super
43-
end
4438
when @result.respond_to?(name)
4539
@result.__send__ name, *arguments
4640
when @result._source && @result._source.respond_to?(name)

‎elasticsearch-model/test/integration/active_record_basic_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ class ::Article < ActiveRecord::Base
7272
assert_equal [1, 2], response.records.map(&:id)
7373
end
7474

75-
should "iterate aliased id and type over results" do
75+
should "return _id and _type as #id and #type" do
7676
response = Article.search('title:test')
7777

78-
assert_equal ['1', '2'], response.results.map(&:id)
79-
assert_equal ['article', 'article'], response.results.map(&:type)
78+
assert_equal '1', response.results.first.id
79+
assert_equal 'article', response.results.first.type
8080
end
8181

8282
should "access results from records" do

‎elasticsearch-model/test/unit/response_result_test.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
1515
assert_raise(NoMethodError) { result.xoxo }
1616
end
1717

18+
should "return _id as #id" do
19+
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _id: 42, _source: { id: 12 }
20+
21+
assert_equal 42, result.id
22+
assert_equal 12, result._source.id
23+
end
24+
25+
should "return _type as #type" do
26+
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _type: 'baz', _source: { type: 'BAM' }
27+
28+
assert_equal 'baz', result.type
29+
assert_equal 'BAM', result._source.type
30+
end
31+
1832
should "delegate method calls to `_source` when available" do
1933
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _source: { bar: 'baz' }
2034

@@ -72,17 +86,5 @@ class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
7286
result.instance_variable_get(:@result).expects(:as_json)
7387
result.as_json(except: 'foo')
7488
end
75-
76-
should "map the _id column to id" do
77-
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _id: 42
78-
79-
assert_equal 42, result.id
80-
end
81-
82-
should "map the _type column to type" do
83-
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _type: 'baz'
84-
85-
assert_equal 'baz', result.type
86-
end
8789
end
8890
end

0 commit comments

Comments
 (0)