Skip to content

Commit 5160424

Browse files
committed
[STORE] Refactored the :index paramter to use repository index_name
1 parent c4b1d41 commit 5160424

File tree

7 files changed

+30
-22
lines changed

7 files changed

+30
-22
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/repository/find.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ def find(*args)
1818

1919
def exists?(id, options={})
2020
type = (klass ? __get_type_from_class(klass) : '_all')
21-
client.exists( { index: 'test', type: type, id: id }.merge(options) )
21+
client.exists( { index: index_name, type: type, id: id }.merge(options) )
2222
end
2323

2424
def __find_one(id, options={})
2525
type = (klass ? __get_type_from_class(klass) : '_all')
26-
document = client.get( { index: 'test', type: type, id: id }.merge(options) )
26+
document = client.get( { index: index_name, type: type, id: id }.merge(options) )
2727

2828
deserialize(document)
2929
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
@@ -32,7 +32,7 @@ def __find_one(id, options={})
3232

3333
def __find_many(ids, options={})
3434
type = (klass ? __get_type_from_class(klass) : '_all')
35-
documents = client.mget( { index: 'test', type: type, body: { ids: ids } }.merge(options) )
35+
documents = client.mget( { index: index_name, type: type, body: { ids: ids } }.merge(options) )
3636

3737
documents['docs'].map { |document| document['found'] ? deserialize(document) : nil }
3838
end

elasticsearch-persistence/lib/elasticsearch/persistence/repository/search.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ def search(query_or_definition, options={})
77
type = (klass ? __get_type_from_class(klass) : nil )
88
case
99
when query_or_definition.respond_to?(:to_hash)
10-
response = client.search( { index: 'test', type: type, body: query_or_definition.to_hash }.merge(options) )
10+
response = client.search( { index: index_name, type: type, body: query_or_definition.to_hash }.merge(options) )
1111
when query_or_definition.is_a?(String)
12-
response = client.search( { index: 'test', type: type, q: query_or_definition }.merge(options) )
12+
response = client.search( { index: index_name, type: type, q: query_or_definition }.merge(options) )
1313
else
1414
raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String" +
1515
" -- #{query_or_definition.class} given."

elasticsearch-persistence/lib/elasticsearch/persistence/repository/store.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def save(document, options={})
77
serialized = serialize(document)
88
id = __get_id_from_document(serialized)
99
type = klass || __get_type_from_class(document.class)
10-
client.index( { index: 'test', type: type, id: id, body: serialized }.merge(options) )
10+
client.index( { index: index_name, type: type, id: id, body: serialized }.merge(options) )
1111
end
1212

1313
def delete(document, options={})
@@ -19,7 +19,7 @@ def delete(document, options={})
1919
id = __get_id_from_document(serialized)
2020
type = klass || __get_type_from_class(document.class)
2121
end
22-
client.delete( { index: 'test', type: type, id: id }.merge(options) )
22+
client.delete( { index: index_name, type: type, id: id }.merge(options) )
2323
end
2424
end
2525

elasticsearch-persistence/test/unit/repository_find_test.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class MyDocument; end
99

1010
@client = mock
1111
@shoulda_subject.stubs(:klass).returns(nil)
12+
@shoulda_subject.stubs(:index_name).returns('my_index')
1213
@shoulda_subject.stubs(:client).returns(@client)
1314
end
1415

@@ -78,10 +79,11 @@ class MyDocument; end
7879

7980
should "pass options to the client" do
8081
@client.expects(:exists).with do |arguments|
81-
assert_equal 'bambam', arguments[:routing]
82+
assert_equal 'foobarbam', arguments[:index]
83+
assert_equal 'bambam', arguments[:routing]
8284
end
8385

84-
subject.exists? '1', routing: 'bambam'
86+
subject.exists? '1', index: 'foobarbam', routing: 'bambam'
8587
end
8688
end
8789

@@ -155,25 +157,26 @@ class MyDocument; end
155157
@client
156158
.expects(:get)
157159
.with do |arguments|
158-
assert_equal 'bambam', arguments[:routing]
160+
assert_equal 'foobarbam', arguments[:index]
161+
assert_equal 'bambam', arguments[:routing]
159162
end
160163
.returns({'_source' => { 'foo' => 'bar' }})
161164

162-
subject.__find_one '1', routing: 'bambam'
165+
subject.__find_one '1', index: 'foobarbam', routing: 'bambam'
163166
end
164167
end
165168

166169
context "'__find_many' method" do
167170
setup do
168171
@response = {"docs"=>
169-
[ {"_index"=>"test",
172+
[ {"_index"=>"my_index",
170173
"_type"=>"note",
171174
"_id"=>"1",
172175
"_version"=>1,
173176
"found"=>true,
174177
"_source"=>{"id"=>"1", "title"=>"Test 1"}},
175178

176-
{"_index"=>"test",
179+
{"_index"=>"my_index",
177180
"_type"=>"note",
178181
"_id"=>"2",
179182
"_version"=>1,
@@ -241,20 +244,20 @@ class MyDocument; end
241244

242245
should "find keep missing documents in the result as nil" do
243246
@response = {"docs"=>
244-
[ {"_index"=>"test",
247+
[ {"_index"=>"my_index",
245248
"_type"=>"note",
246249
"_id"=>"1",
247250
"_version"=>1,
248251
"found"=>true,
249252
"_source"=>{"id"=>"1", "title"=>"Test 1"}},
250253

251-
{"_index"=>"test",
254+
{"_index"=>"my_index",
252255
"_type"=>"note",
253256
"_id"=>"3",
254257
"_version"=>1,
255258
"found"=>false},
256259

257-
{"_index"=>"test",
260+
{"_index"=>"my_index",
258261
"_type"=>"note",
259262
"_id"=>"2",
260263
"_version"=>1,
@@ -299,11 +302,12 @@ class MyDocument; end
299302
@client
300303
.expects(:mget)
301304
.with do |arguments|
302-
assert_equal 'bambam', arguments[:routing]
305+
assert_equal 'foobarbam', arguments[:index]
306+
assert_equal 'bambam', arguments[:routing]
303307
end
304308
.returns(@response)
305309

306-
subject.__find_many ['1', '2'], routing: 'bambam'
310+
subject.__find_many ['1', '2'], index: 'foobarbam', routing: 'bambam'
307311
end
308312
end
309313

elasticsearch-persistence/test/unit/repository_response_results_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class MyDocument; end
1515
{ "total" => 2,
1616
"max_score" => 0.19,
1717
"hits" =>
18-
[{"_index" => "test",
18+
[{"_index" => "my_index",
1919
"_type" => "note",
2020
"_id" => "1",
2121
"_score" => 0.19,
2222
"_source" => {"id" => 1, "title" => "Test 1"}},
2323

24-
{"_index" => "test",
24+
{"_index" => "my_index",
2525
"_type" => "note",
2626
"_id" => "2",
2727
"_score" => 0.19,

elasticsearch-persistence/test/unit/repository_search_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class MyDocument; end
99

1010
@client = mock
1111
@shoulda_subject.stubs(:klass).returns(nil)
12+
@shoulda_subject.stubs(:index_name).returns('test')
1213
@shoulda_subject.stubs(:client).returns(@client)
1314
end
1415

elasticsearch-persistence/test/unit/repository_store_test.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class MyDocument; end
66

77
setup do
88
@shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Store }.new
9+
@shoulda_subject.stubs(:index_name).returns('test')
910
end
1011

1112
context "save" do
@@ -49,11 +50,12 @@ class MyDocument; end
4950

5051
client = mock
5152
client.expects(:index).with do |arguments|
53+
assert_equal 'foobarbam', arguments[:index]
5254
assert_equal 'bambam', arguments[:routing]
5355
end
5456
subject.expects(:client).returns(client)
5557

56-
subject.save({foo: 'bar'}, routing: 'bambam')
58+
subject.save({foo: 'bar'}, { index: 'foobarbam', routing: 'bambam' })
5759
end
5860
end
5961

@@ -109,11 +111,12 @@ class MyDocument; end
109111

110112
client = mock
111113
client.expects(:delete).with do |arguments|
114+
assert_equal 'foobarbam', arguments[:index]
112115
assert_equal 'bambam', arguments[:routing]
113116
end
114117
subject.expects(:client).returns(client)
115118

116-
subject.delete('1', routing: 'bambam')
119+
subject.delete('1', index: 'foobarbam', routing: 'bambam')
117120
end
118121
end
119122
end

0 commit comments

Comments
 (0)