Skip to content

Commit ffc1aa4

Browse files
committed
[MODEL] [STORE] Changed the default mapping type to text
The default type for properties defined with the `indexes` method has been changed to the `text` field type. Also, fixed the integration test to have the correct mapping and to use the `bool` query instead of the old `filtered` query. See: * https://www.elastic.co/guide/en/elasticsearch/reference/current/string.html * https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html Related: #649
1 parent 757c57a commit ffc1aa4

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def indexes(name, options={}, &block)
6565
end
6666
end
6767

68-
# Set the type to `string` by default
69-
@mapping[name][:type] ||= 'string'
68+
# Set the type to `text` by default
69+
@mapping[name][:type] ||= 'text'
7070

7171
self
7272
end

elasticsearch-model/test/integration/active_record_associations_test.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ module Searchable
2929
indexes :authors do
3030
indexes :first_name
3131
indexes :last_name
32-
indexes :full_name, type: 'multi_field' do
33-
indexes :full_name
34-
indexes :raw, analyzer: 'keyword'
32+
indexes :full_name, type: 'text' do
33+
indexes :raw, type: 'keyword'
3534
end
3635
end
3736

38-
indexes :categories, analyzer: 'keyword'
37+
indexes :categories, type: 'keyword'
3938

4039
indexes :comments, type: 'nested' do
4140
indexes :text
@@ -188,8 +187,8 @@ class Post < ActiveRecord::Base
188187
Post.__elasticsearch__.refresh_index!
189188

190189
query = { query: {
191-
filtered: {
192-
query: {
190+
bool: {
191+
must: {
193192
multi_match: {
194193
fields: ['title'],
195194
query: 'first'

elasticsearch-model/test/unit/indexing_test.rb

+11-21
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,23 @@ class NotFound < Exception; end
8383
assert_equal 'boolean', mappings.to_hash[:mytype][:properties][:foo][:type]
8484
end
8585

86-
should "define type as string by default" do
86+
should "define type as 'text' by default" do
8787
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
8888

89-
mappings.indexes :bar, {}
90-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:bar][:type]
89+
mappings.indexes :bar
90+
assert_equal 'text', mappings.to_hash[:mytype][:properties][:bar][:type]
9191
end
9292

9393
should "define multiple fields" do
9494
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
9595

96-
mappings.indexes :foo_1, type: 'string' do
97-
indexes :raw, analyzer: 'keyword'
96+
mappings.indexes :my_field, type: 'text' do
97+
indexes :raw, type: 'keyword'
9898
end
9999

100-
mappings.indexes :foo_2, type: 'multi_field' do
101-
indexes :raw, analyzer: 'keyword'
102-
end
103-
104-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_1][:type]
105-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_1][:fields][:raw][:type]
106-
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:foo_1][:fields][:raw][:analyzer]
107-
assert_nil mappings.to_hash[:mytype][:properties][:foo_1][:properties]
108-
109-
assert_equal 'multi_field', mappings.to_hash[:mytype][:properties][:foo_2][:type]
110-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_2][:fields][:raw][:type]
111-
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:foo_2][:fields][:raw][:analyzer]
112-
assert_nil mappings.to_hash[:mytype][:properties][:foo_2][:properties]
100+
assert_equal 'text', mappings.to_hash[:mytype][:properties][:my_field][:type]
101+
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:my_field][:fields][:raw][:type]
102+
assert_nil mappings.to_hash[:mytype][:properties][:my_field][:properties]
113103
end
114104

115105
should "define embedded properties" do
@@ -134,15 +124,15 @@ class NotFound < Exception; end
134124
# Object is the default when `type` is missing and there's a block passed
135125
#
136126
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
137-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
127+
assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
138128
assert_nil mappings.to_hash[:mytype][:properties][:foo][:fields]
139129

140130
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo_object][:type]
141-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_object][:properties][:bar][:type]
131+
assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo_object][:properties][:bar][:type]
142132
assert_nil mappings.to_hash[:mytype][:properties][:foo_object][:fields]
143133

144134
assert_equal 'nested', mappings.to_hash[:mytype][:properties][:foo_nested][:type]
145-
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_nested][:properties][:bar][:type]
135+
assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo_nested][:properties][:bar][:type]
146136
assert_nil mappings.to_hash[:mytype][:properties][:foo_nested][:fields]
147137

148138
assert_equal :nested, mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:type]

elasticsearch-persistence/test/unit/repository_indexing_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MyDocument; end
2222
indexes :title
2323
end
2424

25-
assert_equal( {:"my_document"=>{:properties=>{:title=>{:type=>"string"}}}}, subject.mappings.to_hash )
25+
assert_equal( {:"my_document"=>{:properties=>{:title=>{:type=>"text"}}}}, subject.mappings.to_hash )
2626
end
2727
end
2828

0 commit comments

Comments
 (0)