Skip to content

Commit b969783

Browse files
David Padillakarmi
David Padilla
authored andcommitted
[MODEL] Added, that index settings can be loaded from any object that responds to :read
Related: elastic#346 Related: elastic#351
1 parent 5c7cd12 commit b969783

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+22-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ def mapping(options={}, &block)
153153
#
154154
# # => {:index=>{:number_of_shards=>1}}
155155
#
156-
# You can specify a YAML file with settings
156+
# You can read settings from any object that responds to :read
157+
# as long as its return value can be parsed as either YAML or JSON.
157158
#
158159
# @example Define index settings from YAML file
159160
#
@@ -163,14 +164,28 @@ def mapping(options={}, &block)
163164
# # number_of_shards: 1
164165
# #
165166
#
166-
# Article.settings "config/elasticsearch/articles.yml"
167+
# Article.settings File.open("config/elasticsearch/articles.yml")
168+
#
169+
# Article.settings.to_hash
170+
#
171+
# # => { "index" => { "number_of_shards" => 1 } }
172+
#
173+
#
174+
# @example Define index settings from JSON file
175+
#
176+
# # config/elasticsearch/articles.json:
177+
# #
178+
# # { "index": { "number_of_shards": 1 } }
179+
# #
180+
#
181+
# Article.settings File.open("config/elasticsearch/articles.json")
167182
#
168183
# Article.settings.to_hash
169184
#
170185
# # => { "index" => { "number_of_shards" => 1 } }
171186
#
172187
def settings(settings={}, &block)
173-
settings = YAML.load_file(settings) if settings.is_a?(String)
188+
settings = YAML.load(settings.read) if settings.respond_to?(:read)
174189
@settings ||= Settings.new(settings)
175190

176191
@settings.settings.update(settings) unless settings.empty?
@@ -183,6 +198,10 @@ def settings(settings={}, &block)
183198
end
184199
end
185200

201+
def load_settings_from_io(settings)
202+
YAML.load(settings.read)
203+
end
204+
186205
# Creates an index with correct name, automatically passing
187206
# `settings` and `mappings` defined in the model
188207
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "baz": "qux" }

elasticsearch-model/test/unit/indexing_test.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ class NotFound < Exception; end
3636
end
3737

3838
should "update and return the index settings from a yml file" do
39-
DummyIndexingModel.settings "test/support/model.yml"
39+
DummyIndexingModel.settings File.open("test/support/model.yml")
40+
DummyIndexingModel.settings bar: 'bam'
41+
42+
assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
43+
end
44+
45+
should "update and return the index settings from a json file" do
46+
DummyIndexingModel.settings File.open("test/support/model.json")
4047
DummyIndexingModel.settings bar: 'bam'
4148

4249
assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)

0 commit comments

Comments
 (0)