Skip to content

Commit 3792616

Browse files
rsutphinkarmi
authored andcommitted
[MODEL] Added proper support for the new "multi_fields" in the mapping DSL
This branch updates the `mapping` DSL to have nested `indexes` calls produce embedded fields (instead of properties) for the core types where this is appropriate. This is allowed starting with [elasticsearch 1.0][es10mf] as a replacement for `multi_field`. [es10mf]: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_multi_fields.html `type: "multi_field"` will still work as before; this change should not break backwards compatibility. Closes elastic#138
1 parent f5162fa commit 3792616

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def as_json(options={})
3636
class Mappings
3737
attr_accessor :options, :type
3838

39+
# @private
40+
TYPES_WITH_EMBEDDED_PROPERTIES = %w(object nested)
41+
3942
def initialize(type, options={})
4043
raise ArgumentError, "`type` is missing" if type.nil?
4144

@@ -49,7 +52,7 @@ def indexes(name, options = {}, &block)
4952

5053
if block_given?
5154
@mapping[name][:type] ||= 'object'
52-
properties = @mapping[name][:type] == 'multi_field' ? :fields : :properties
55+
properties = TYPES_WITH_EMBEDDED_PROPERTIES.include?(@mapping[name][:type]) ? :properties : :fields
5356

5457
@mapping[name][properties] ||= {}
5558

elasticsearch-model/test/unit/indexing_test.rb

+24-15
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,36 @@ class NotFound < Exception; end
9090
assert_equal 'string', mappings.to_hash[:mytype][:properties][:bar][:type]
9191
end
9292

93-
should "define embedded properties" do
94-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
93+
%w(string long double boolean multi_field).each do |outer_type|
94+
should "define embedded fields for #{outer_type}" do
95+
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
9596

96-
mappings.indexes :foo do
97-
indexes :bar
98-
end
97+
mappings.indexes :foo, type: outer_type do
98+
indexes :raw, analyzer: 'keyword'
99+
end
99100

100-
mappings.indexes :multi, type: 'multi_field' do
101-
indexes :multi, analyzer: 'snowball'
102-
indexes :raw, analyzer: 'keyword'
101+
foo_mapping = mappings.to_hash[:mytype][:properties][:foo]
102+
103+
assert_equal outer_type, foo_mapping[:type]
104+
assert_nil foo_mapping[:properties]
105+
assert_equal 'string', foo_mapping[:fields][:raw][:type]
103106
end
107+
end
104108

105-
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
106-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
109+
%w(object nested).each do |outer_type|
110+
should "define embedded properties for #{outer_type}" do
111+
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
107112

108-
assert_equal 'multi_field', mappings.to_hash[:mytype][:properties][:multi][:type]
109-
assert_equal 'snowball', mappings.to_hash[:mytype][:properties][:multi][:fields][:multi][:analyzer]
110-
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:multi][:fields][:raw][:analyzer]
111-
end
113+
mappings.indexes :foo, type: outer_type do
114+
indexes :bar
115+
end
112116

113-
should "define multi_field properties" do
117+
foo_mapping = mappings.to_hash[:mytype][:properties][:foo]
118+
119+
assert_equal outer_type, foo_mapping[:type]
120+
assert_nil foo_mapping[:fields]
121+
assert_equal 'string', foo_mapping[:properties][:bar][:type]
122+
end
114123
end
115124
end
116125

0 commit comments

Comments
 (0)