Skip to content

Commit 5dee640

Browse files
PnPiejpountz
authored andcommitted
Disable date field mapping changing (#25285)
Make date field mapping unchangeable. Closes #25271
1 parent 0580ee6 commit 5dee640

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.Locale;
5757
import java.util.Map;
5858
import java.util.Objects;
59+
import java.util.ArrayList;
5960
import static org.elasticsearch.index.mapper.TypeParsers.parseDateTimeFormatter;
6061

6162
/** A {@link FieldMapper} for ip addresses. */
@@ -212,16 +213,12 @@ public String typeName() {
212213
@Override
213214
public void checkCompatibility(MappedFieldType fieldType, List<String> conflicts, boolean strict) {
214215
super.checkCompatibility(fieldType, conflicts, strict);
215-
if (strict) {
216-
DateFieldType other = (DateFieldType)fieldType;
217-
if (Objects.equals(dateTimeFormatter().format(), other.dateTimeFormatter().format()) == false) {
218-
conflicts.add("mapper [" + name()
219-
+ "] is used by multiple types. Set update_all_types to true to update [format] across all types.");
220-
}
221-
if (Objects.equals(dateTimeFormatter().locale(), other.dateTimeFormatter().locale()) == false) {
222-
conflicts.add("mapper [" + name()
223-
+ "] is used by multiple types. Set update_all_types to true to update [locale] across all types.");
224-
}
216+
DateFieldType other = (DateFieldType) fieldType;
217+
if (Objects.equals(dateTimeFormatter().format(), other.dateTimeFormatter().format()) == false) {
218+
conflicts.add("mapper [" + name() + "] has different [format] values");
219+
}
220+
if (Objects.equals(dateTimeFormatter().locale(), other.dateTimeFormatter().locale()) == false) {
221+
conflicts.add("mapper [" + name() + "] has different [locale] values");
225222
}
226223
}
227224

@@ -491,8 +488,8 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
491488

492489
@Override
493490
protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
491+
final DateFieldMapper other = (DateFieldMapper) mergeWith;
494492
super.doMerge(mergeWith, updateAllTypes);
495-
DateFieldMapper other = (DateFieldMapper) mergeWith;
496493
this.includeInAll = other.includeInAll;
497494
if (other.ignoreMalformed.explicit()) {
498495
this.ignoreMalformed = other.ignoreMalformed;

core/src/main/java/org/elasticsearch/index/mapper/ParentFieldMapper.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
296296

297297
@Override
298298
protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
299+
ParentFieldMapper fieldMergeWith = (ParentFieldMapper) mergeWith;
299300
ParentFieldType currentFieldType = (ParentFieldType) fieldType.clone();
300301
super.doMerge(mergeWith, updateAllTypes);
301-
ParentFieldMapper fieldMergeWith = (ParentFieldMapper) mergeWith;
302302
if (fieldMergeWith.parentType != null && Objects.equals(parentType, fieldMergeWith.parentType) == false) {
303303
throw new IllegalArgumentException("The _parent field's type option can't be changed: [" + parentType + "]->[" + fieldMergeWith.parentType + "]");
304304
}
305305

306-
List<String> conflicts = new ArrayList<>();
307-
fieldType().checkCompatibility(fieldMergeWith.fieldType, conflicts, true);
308-
if (conflicts.isEmpty() == false) {
309-
throw new IllegalArgumentException("Merge conflicts: " + conflicts);
310-
}
311-
312306
if (active()) {
313307
fieldType = currentFieldType;
314308
}

core/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
4343
import static org.hamcrest.Matchers.containsString;
44+
import static org.hamcrest.Matchers.notNullValue;
4445

4546
public class DateFieldMapperTests extends ESSingleNodeTestCase {
4647

@@ -387,4 +388,26 @@ public void testTimeZoneParsing() throws Exception {
387388

388389
assertEquals(randomDate.withZone(DateTimeZone.UTC).getMillis(), fields[0].numericValue().longValue());
389390
}
391+
392+
public void testMergeDate() throws IOException {
393+
String initMapping = XContentFactory.jsonBuilder().startObject().startObject("movie")
394+
.startObject("properties")
395+
.startObject("release_date").field("type", "date").field("format", "yyyy/MM/dd").endObject()
396+
.endObject().endObject().endObject().string();
397+
DocumentMapper initMapper = indexService.mapperService().merge("movie", new CompressedXContent(initMapping),
398+
MapperService.MergeReason.MAPPING_UPDATE, randomBoolean());
399+
400+
assertThat(initMapper.mappers().getMapper("release_date"), notNullValue());
401+
assertFalse(initMapper.mappers().getMapper("release_date").fieldType().stored());
402+
403+
String updateFormatMapping = XContentFactory.jsonBuilder().startObject().startObject("movie")
404+
.startObject("properties")
405+
.startObject("release_date").field("type", "date").field("format", "epoch_millis").endObject()
406+
.endObject().endObject().endObject().string();
407+
408+
Exception e = expectThrows(IllegalArgumentException.class,
409+
() -> indexService.mapperService().merge("movie", new CompressedXContent(updateFormatMapping),
410+
MapperService.MergeReason.MAPPING_UPDATE, randomBoolean()));
411+
assertThat(e.getMessage(), containsString("[mapper [release_date] has different [format] values]"));
412+
}
390413
}

core/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ protected MappedFieldType createDefaultFieldType() {
5858
@Before
5959
public void setupProperties() {
6060
setDummyNullValue(10);
61-
addModifier(new Modifier("format", true) {
61+
addModifier(new Modifier("format", false) {
6262
@Override
6363
public void modify(MappedFieldType ft) {
6464
((DateFieldType) ft).setDateTimeFormatter(Joda.forPattern("basic_week_date", Locale.ROOT));
6565
}
6666
});
67-
addModifier(new Modifier("locale", true) {
67+
addModifier(new Modifier("locale", false) {
6868
@Override
6969
public void modify(MappedFieldType ft) {
7070
((DateFieldType) ft).setDateTimeFormatter(Joda.forPattern("date_optional_time", Locale.CANADA));

core/src/test/java/org/elasticsearch/index/mapper/DocumentMapperMergeTests.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
import org.elasticsearch.common.compress.CompressedXContent;
2525
import org.elasticsearch.common.xcontent.XContentFactory;
2626
import org.elasticsearch.index.analysis.NamedAnalyzer;
27-
import org.elasticsearch.index.mapper.DocumentFieldMappers;
28-
import org.elasticsearch.index.mapper.DocumentMapper;
29-
import org.elasticsearch.index.mapper.DocumentMapperParser;
30-
import org.elasticsearch.index.mapper.MapperService;
31-
import org.elasticsearch.index.mapper.Mapping;
32-
import org.elasticsearch.index.mapper.ObjectMapper;
33-
import org.elasticsearch.index.mapper.ParsedDocument;
3427
import org.elasticsearch.test.ESSingleNodeTestCase;
3528

3629
import java.io.IOException;

0 commit comments

Comments
 (0)