@@ -120,7 +120,8 @@ public void testConflictNewType() throws Exception {
120
120
XContentBuilder mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type1" )
121
121
.startObject ("properties" ).startObject ("foo" ).field ("type" , "long" ).endObject ()
122
122
.endObject ().endObject ().endObject ();
123
- MapperService mapperService = createIndex ("test" , Settings .builder ().build (), "type1" , mapping ).mapperService ();
123
+ MapperService mapperService = createIndex ("test" , Settings .builder ().put ("index.version.created" ,
124
+ Version .V_5_6_0 ).build (), "type1" , mapping ).mapperService ();
124
125
125
126
XContentBuilder update = XContentFactory .jsonBuilder ().startObject ().startObject ("type2" )
126
127
.startObject ("properties" ).startObject ("foo" ).field ("type" , "double" ).endObject ()
@@ -134,17 +135,8 @@ public void testConflictNewType() throws Exception {
134
135
assertTrue (e .getMessage (), e .getMessage ().contains ("mapper [foo] cannot be changed from type [long] to [double]" ));
135
136
}
136
137
137
- try {
138
- mapperService .merge ("type2" , new CompressedXContent (Strings .toString (update )), MapperService .MergeReason .MAPPING_UPDATE , false );
139
- fail ();
140
- } catch (IllegalArgumentException e ) {
141
- // expected
142
- assertTrue (e .getMessage (), e .getMessage ().contains ("mapper [foo] cannot be changed from type [long] to [double]" ));
143
- }
144
-
145
138
assertThat (((FieldMapper ) mapperService .documentMapper ("type1" ).mapping ().root ().getMapper ("foo" )).fieldType ().typeName (),
146
139
equalTo ("long" ));
147
- assertNull (mapperService .documentMapper ("type2" ));
148
140
}
149
141
150
142
// same as the testConflictNewType except that the mapping update is on an existing type
@@ -224,18 +216,53 @@ public void testRejectFieldDefinedTwice() throws IOException {
224
216
.endObject ()
225
217
.endObject ().endObject ());
226
218
227
- MapperService mapperService1 = createIndex ("test1" ).mapperService ();
219
+ MapperService mapperService1 = createIndex ("test1" , Settings .builder ().put ("index.version.created" ,
220
+ Version .V_5_6_0 ).build ()).mapperService ();
221
+
228
222
mapperService1 .merge ("type1" , new CompressedXContent (mapping1 ), MergeReason .MAPPING_UPDATE , false );
229
223
IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
230
- () -> mapperService1 .merge ("type2" , new CompressedXContent (mapping2 ), MergeReason .MAPPING_UPDATE , false ));
224
+ () -> mapperService1 .merge ("type2" , new CompressedXContent (mapping2 ), MergeReason .MAPPING_UPDATE , false ));
231
225
assertThat (e .getMessage (), equalTo ("[foo] is defined as a field in mapping [type2"
232
- + "] but this name is already used for an object in other types" ));
226
+ + "] but this name is already used for an object in other types" ));
233
227
234
- MapperService mapperService2 = createIndex ("test2" ).mapperService ();
228
+ MapperService mapperService2 = createIndex ("test2" , Settings .builder ().put ("index.version.created" ,
229
+ Version .V_5_6_0 ).build ()).mapperService ();
235
230
mapperService2 .merge ("type2" , new CompressedXContent (mapping2 ), MergeReason .MAPPING_UPDATE , false );
236
231
e = expectThrows (IllegalArgumentException .class ,
237
- () -> mapperService2 .merge ("type1" , new CompressedXContent (mapping1 ), MergeReason .MAPPING_UPDATE , false ));
232
+ () -> mapperService2 .merge ("type1" , new CompressedXContent (mapping1 ), MergeReason .MAPPING_UPDATE , false ));
238
233
assertThat (e .getMessage (), equalTo ("[foo] is defined as an object in mapping [type1"
239
- + "] but this name is already used for a field in other types" ));
234
+ + "] but this name is already used for a field in other types" ));
235
+ }
236
+
237
+ public void testRejectFieldDefinedTwiceInSameType () throws IOException {
238
+ String mapping1 = Strings .toString (XContentFactory .jsonBuilder ().startObject ()
239
+ .startObject ("type" )
240
+ .startObject ("properties" )
241
+ .startObject ("foo" )
242
+ .field ("type" , "object" )
243
+ .endObject ()
244
+ .endObject ()
245
+ .endObject ().endObject ());
246
+ String mapping2 = Strings .toString (XContentFactory .jsonBuilder ().startObject ()
247
+ .startObject ("type" )
248
+ .startObject ("properties" )
249
+ .startObject ("foo" )
250
+ .field ("type" , "long" )
251
+ .endObject ()
252
+ .endObject ()
253
+ .endObject ().endObject ());
254
+
255
+ MapperService mapperService1 = createIndex ("test1" ).mapperService ();
256
+
257
+ mapperService1 .merge ("type" , new CompressedXContent (mapping1 ), MergeReason .MAPPING_UPDATE , false );
258
+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
259
+ () -> mapperService1 .merge ("type" , new CompressedXContent (mapping2 ), MergeReason .MAPPING_UPDATE , false ));
260
+ assertThat (e .getMessage (), equalTo ("Can't merge a non object mapping [foo] with an object mapping [foo]" ));
261
+
262
+ MapperService mapperService2 = createIndex ("test2" ).mapperService ();
263
+ mapperService2 .merge ("type" , new CompressedXContent (mapping2 ), MergeReason .MAPPING_UPDATE , false );
264
+ e = expectThrows (IllegalArgumentException .class ,
265
+ () -> mapperService2 .merge ("type" , new CompressedXContent (mapping1 ), MergeReason .MAPPING_UPDATE , false ));
266
+ assertThat (e .getMessage (), equalTo ("mapper [foo] of different type, current_type [long], merged_type [ObjectMapper]" ));
240
267
}
241
268
}
0 commit comments