Skip to content

Commit 2cbc937

Browse files
committed
[MODEL] Added automatic handling multi_field properties in indexes blocks
mappings.indexes :name, type: 'multi_field' do indexes :name, analyzer: 'snowball' indexes :raw, analyzer: 'keyword' end
1 parent 1872668 commit 2cbc937

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ def indexes(name, options = {}, &block)
4646
@mapping[name] = options
4747

4848
if block_given?
49-
@mapping[name][:type] ||= 'object'
50-
@mapping[name][:properties] ||= {}
49+
@mapping[name][:type] ||= 'object'
50+
properties = @mapping[name][:type] == 'multi_field' ? :fields : :properties
51+
52+
@mapping[name][properties] ||= {}
5153

5254
previous = @mapping
5355
begin
54-
@mapping = @mapping[name][:properties]
56+
@mapping = @mapping[name][properties]
5557
self.instance_eval(&block)
5658
ensure
5759
@mapping = previous

elasticsearch-model/test/unit/indexing_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,20 @@ def self.foo
7575
indexes :bar
7676
end
7777

78+
mappings.indexes :multi, type: 'multi_field' do
79+
indexes :multi, analyzer: 'snowball'
80+
indexes :raw, analyzer: 'keyword'
81+
end
82+
7883
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
7984
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
85+
86+
assert_equal 'multi_field', mappings.to_hash[:mytype][:properties][:multi][:type]
87+
assert_equal 'snowball', mappings.to_hash[:mytype][:properties][:multi][:fields][:multi][:analyzer]
88+
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:multi][:fields][:raw][:analyzer]
89+
end
90+
91+
should "define multi_field properties" do
8092
end
8193
end
8294

0 commit comments

Comments
 (0)