|
19 | 19 |
|
20 | 20 | package org.elasticsearch.index.mapper; |
21 | 21 |
|
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
22 | 24 | import org.elasticsearch.common.Strings; |
23 | 25 | import org.elasticsearch.common.compress.CompressedXContent; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
24 | 28 | import org.elasticsearch.common.xcontent.XContentFactory; |
25 | 29 | import org.elasticsearch.index.mapper.MapperService.MergeReason; |
26 | 30 | import org.elasticsearch.test.ESSingleNodeTestCase; |
27 | 31 |
|
28 | 32 | import java.util.Arrays; |
29 | 33 |
|
| 34 | +import static org.elasticsearch.test.VersionUtils.randomVersionBetween; |
| 35 | +import static org.hamcrest.Matchers.containsString; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | +import static org.hamcrest.Matchers.instanceOf; |
| 38 | + |
30 | 39 | public class RootObjectMapperTests extends ESSingleNodeTestCase { |
31 | 40 |
|
32 | 41 | public void testNumericDetection() throws Exception { |
@@ -200,4 +209,188 @@ public void testIllegalDynamicTemplates() throws Exception { |
200 | 209 | () -> parser.parse("type", new CompressedXContent(mapping))); |
201 | 210 | assertEquals("Dynamic template syntax error. An array of named objects is expected.", e.getMessage()); |
202 | 211 | } |
| 212 | + |
| 213 | + public void testIllegalDynamicTemplateUnknownFieldType() throws Exception { |
| 214 | + XContentBuilder mapping = XContentFactory.jsonBuilder(); |
| 215 | + mapping.startObject(); |
| 216 | + { |
| 217 | + mapping.startObject("type"); |
| 218 | + mapping.startArray("dynamic_templates"); |
| 219 | + { |
| 220 | + mapping.startObject(); |
| 221 | + mapping.startObject("my_template"); |
| 222 | + mapping.field("match_mapping_type", "string"); |
| 223 | + mapping.startObject("mapping"); |
| 224 | + mapping.field("type", "string"); |
| 225 | + mapping.endObject(); |
| 226 | + mapping.endObject(); |
| 227 | + mapping.endObject(); |
| 228 | + } |
| 229 | + mapping.endArray(); |
| 230 | + mapping.endObject(); |
| 231 | + } |
| 232 | + mapping.endObject(); |
| 233 | + MapperService mapperService = createIndex("test").mapperService(); |
| 234 | + MapperParsingException e = expectThrows(MapperParsingException.class, |
| 235 | + () -> mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE)); |
| 236 | + assertThat(e.getRootCause(), instanceOf(IllegalArgumentException.class)); |
| 237 | + assertThat(e.getRootCause().getMessage(), equalTo("No mapper found for type [string]")); |
| 238 | + } |
| 239 | + |
| 240 | + public void testIllegalDynamicTemplateUnknownAttribute() throws Exception { |
| 241 | + XContentBuilder mapping = XContentFactory.jsonBuilder(); |
| 242 | + mapping.startObject(); |
| 243 | + { |
| 244 | + mapping.startObject("type"); |
| 245 | + mapping.startArray("dynamic_templates"); |
| 246 | + { |
| 247 | + mapping.startObject(); |
| 248 | + mapping.startObject("my_template"); |
| 249 | + mapping.field("match_mapping_type", "string"); |
| 250 | + mapping.startObject("mapping"); |
| 251 | + mapping.field("type", "keyword"); |
| 252 | + mapping.field("foo", "bar"); |
| 253 | + mapping.endObject(); |
| 254 | + mapping.endObject(); |
| 255 | + mapping.endObject(); |
| 256 | + } |
| 257 | + mapping.endArray(); |
| 258 | + mapping.endObject(); |
| 259 | + } |
| 260 | + mapping.endObject(); |
| 261 | + MapperService mapperService = createIndex("test").mapperService(); |
| 262 | + MapperParsingException e = expectThrows(MapperParsingException.class, |
| 263 | + () -> mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE)); |
| 264 | + assertThat(e.getRootCause(), instanceOf(IllegalArgumentException.class)); |
| 265 | + assertThat(e.getRootCause().getMessage(), equalTo("Unused mapping attributes [{foo=bar}]")); |
| 266 | + } |
| 267 | + |
| 268 | + public void testIllegalDynamicTemplateInvalidAttribute() throws Exception { |
| 269 | + XContentBuilder mapping = XContentFactory.jsonBuilder(); |
| 270 | + mapping.startObject(); |
| 271 | + { |
| 272 | + mapping.startObject("type"); |
| 273 | + mapping.startArray("dynamic_templates"); |
| 274 | + { |
| 275 | + mapping.startObject(); |
| 276 | + mapping.startObject("my_template"); |
| 277 | + mapping.field("match_mapping_type", "string"); |
| 278 | + mapping.startObject("mapping"); |
| 279 | + mapping.field("type", "text"); |
| 280 | + mapping.field("analyzer", "foobar"); |
| 281 | + mapping.endObject(); |
| 282 | + mapping.endObject(); |
| 283 | + mapping.endObject(); |
| 284 | + } |
| 285 | + mapping.endArray(); |
| 286 | + mapping.endObject(); |
| 287 | + } |
| 288 | + mapping.endObject(); |
| 289 | + MapperService mapperService = createIndex("test").mapperService(); |
| 290 | + MapperParsingException e = expectThrows(MapperParsingException.class, |
| 291 | + () -> mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE)); |
| 292 | + assertThat(e.getRootCause(), instanceOf(MapperParsingException.class)); |
| 293 | + assertThat(e.getRootCause().getMessage(), equalTo("analyzer [foobar] not found for field [__dummy__]")); |
| 294 | + } |
| 295 | + |
| 296 | + public void testIllegalDynamicTemplateNoMappingType() throws Exception { |
| 297 | + MapperService mapperService; |
| 298 | + |
| 299 | + { |
| 300 | + XContentBuilder mapping = XContentFactory.jsonBuilder(); |
| 301 | + mapping.startObject(); |
| 302 | + { |
| 303 | + mapping.startObject("type"); |
| 304 | + mapping.startArray("dynamic_templates"); |
| 305 | + { |
| 306 | + mapping.startObject(); |
| 307 | + mapping.startObject("my_template"); |
| 308 | + if (randomBoolean()) { |
| 309 | + mapping.field("match_mapping_type", "*"); |
| 310 | + } else { |
| 311 | + mapping.field("match", "string_*"); |
| 312 | + } |
| 313 | + mapping.startObject("mapping"); |
| 314 | + mapping.field("type", "{dynamic_type}"); |
| 315 | + mapping.field("index_phrases", true); |
| 316 | + mapping.endObject(); |
| 317 | + mapping.endObject(); |
| 318 | + mapping.endObject(); |
| 319 | + } |
| 320 | + mapping.endArray(); |
| 321 | + mapping.endObject(); |
| 322 | + } |
| 323 | + mapping.endObject(); |
| 324 | + mapperService = createIndex("test").mapperService(); |
| 325 | + DocumentMapper mapper = |
| 326 | + mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE); |
| 327 | + assertThat(mapper.mappingSource().toString(), containsString("\"index_phrases\":true")); |
| 328 | + } |
| 329 | + { |
| 330 | + XContentBuilder mapping = XContentFactory.jsonBuilder(); |
| 331 | + mapping.startObject(); |
| 332 | + { |
| 333 | + mapping.startObject("type"); |
| 334 | + mapping.startArray("dynamic_templates"); |
| 335 | + { |
| 336 | + mapping.startObject(); |
| 337 | + mapping.startObject("my_template"); |
| 338 | + if (randomBoolean()) { |
| 339 | + mapping.field("match_mapping_type", "*"); |
| 340 | + } else { |
| 341 | + mapping.field("match", "string_*"); |
| 342 | + } |
| 343 | + mapping.startObject("mapping"); |
| 344 | + mapping.field("type", "{dynamic_type}"); |
| 345 | + mapping.field("foo", "bar"); |
| 346 | + mapping.endObject(); |
| 347 | + mapping.endObject(); |
| 348 | + mapping.endObject(); |
| 349 | + } |
| 350 | + mapping.endArray(); |
| 351 | + mapping.endObject(); |
| 352 | + } |
| 353 | + mapping.endObject(); |
| 354 | + MapperParsingException e = expectThrows(MapperParsingException.class, |
| 355 | + () -> mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE)); |
| 356 | + assertThat(e.getRootCause(), instanceOf(IllegalArgumentException.class)); |
| 357 | + assertThat(e.getRootCause().getMessage(), equalTo("Unused mapping attributes [{foo=bar}]")); |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + @Override |
| 362 | + protected boolean forbidPrivateIndexSettings() { |
| 363 | + return false; |
| 364 | + } |
| 365 | + |
| 366 | + public void testIllegalDynamicTemplate7DotXIndex() throws Exception { |
| 367 | + XContentBuilder mapping = XContentFactory.jsonBuilder(); |
| 368 | + mapping.startObject(); |
| 369 | + { |
| 370 | + mapping.startObject("type"); |
| 371 | + mapping.startArray("dynamic_templates"); |
| 372 | + { |
| 373 | + mapping.startObject(); |
| 374 | + mapping.startObject("my_template"); |
| 375 | + mapping.field("match_mapping_type", "string"); |
| 376 | + mapping.startObject("mapping"); |
| 377 | + mapping.field("type", "string"); |
| 378 | + mapping.endObject(); |
| 379 | + mapping.endObject(); |
| 380 | + mapping.endObject(); |
| 381 | + } |
| 382 | + mapping.endArray(); |
| 383 | + mapping.endObject(); |
| 384 | + } |
| 385 | + mapping.endObject(); |
| 386 | + Version createdVersion = randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_7_0); |
| 387 | + Settings indexSettings = Settings.builder() |
| 388 | + .put(IndexMetaData.SETTING_INDEX_VERSION_CREATED.getKey(), createdVersion) |
| 389 | + .build(); |
| 390 | + MapperService mapperService = createIndex("test", indexSettings).mapperService(); |
| 391 | + DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE); |
| 392 | + assertThat(mapper.mappingSource().toString(), containsString("\"type\":\"string\"")); |
| 393 | + assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":" + |
| 394 | + "\"string\"}}], caused by [No mapper found for type [string]]"); |
| 395 | + } |
203 | 396 | } |
0 commit comments