Skip to content

Commit 5c7cd12

Browse files
David Padillakarmi
David Padilla
authored andcommitted
[MODEL] Added, that index settings/mappings can be loaded from a YAML or JSON file
This change allows you to specify a YAML file with settings for the indices. It would be helpful if you want to reuse analyzers in many different indexes. Also, YAML is much more readable than hashes in ruby code. Example: # config/elasticsearch/custom_analyzers.yml # # number_of_shards: 5 # analysis: # analyzer: # my_custom_analyzer: # tokenizer: "whitespace" # filter: ["lowercase", "asciifolding"] # class Article include Elasticsearch::Model settings "config/elasticsearch/custom_analyzers.yml" end class Author include Elasticsearch::Model settings "config/elasticsearch/custom_analyzers.yml" end Closes: elastic#346 Closes: elastic#351
1 parent 917c2df commit 5c7cd12

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,24 @@ def mapping(options={}, &block)
153153
#
154154
# # => {:index=>{:number_of_shards=>1}}
155155
#
156+
# You can specify a YAML file with settings
157+
#
158+
# @example Define index settings from YAML file
159+
#
160+
# # config/elasticsearch/articles.yml:
161+
# #
162+
# # index:
163+
# # number_of_shards: 1
164+
# #
165+
#
166+
# Article.settings "config/elasticsearch/articles.yml"
167+
#
168+
# Article.settings.to_hash
169+
#
170+
# # => { "index" => { "number_of_shards" => 1 } }
171+
#
156172
def settings(settings={}, &block)
173+
settings = YAML.load_file(settings) if settings.is_a?(String)
157174
@settings ||= Settings.new(settings)
158175

159176
@settings.settings.update(settings) unless settings.empty?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
baz:
2+
'qux'

elasticsearch-model/test/unit/indexing_test.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ class NotFound < Exception; end
2828
assert_instance_of Elasticsearch::Model::Indexing::Settings, DummyIndexingModel.settings
2929
end
3030

31-
should "update and return the index settings" do
31+
should "update and return the index settings from a hash" do
3232
DummyIndexingModel.settings foo: 'boo'
3333
DummyIndexingModel.settings bar: 'bam'
3434

3535
assert_equal( {foo: 'boo', bar: 'bam'}, DummyIndexingModel.settings.to_hash)
3636
end
3737

38+
should "update and return the index settings from a yml file" do
39+
DummyIndexingModel.settings "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+
3845
should "evaluate the block" do
3946
DummyIndexingModel.expects(:foo)
4047

0 commit comments

Comments
 (0)