Skip to content

Commit e43cdf7

Browse files
authored
Restore v7 support in DateFieldMapper and IPFieldMapper (#119050)
This restores deprecation warnings previously removed with #113023 and restores some additional tests due to N-2 read-only support.
1 parent 5f293f3 commit e43cdf7

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.lucene.search.Query;
2626
import org.elasticsearch.ElasticsearchParseException;
2727
import org.elasticsearch.common.geo.ShapeRelation;
28+
import org.elasticsearch.common.logging.DeprecationCategory;
29+
import org.elasticsearch.common.logging.DeprecationLogger;
2830
import org.elasticsearch.common.lucene.BytesRefs;
2931
import org.elasticsearch.common.time.DateFormatter;
3032
import org.elasticsearch.common.time.DateFormatters;
@@ -34,6 +36,7 @@
3436
import org.elasticsearch.core.Nullable;
3537
import org.elasticsearch.core.TimeValue;
3638
import org.elasticsearch.index.IndexVersion;
39+
import org.elasticsearch.index.IndexVersions;
3740
import org.elasticsearch.index.fielddata.FieldDataContext;
3841
import org.elasticsearch.index.fielddata.IndexFieldData;
3942
import org.elasticsearch.index.fielddata.IndexNumericFieldData.NumericType;
@@ -76,6 +79,7 @@
7679
/** A {@link FieldMapper} for dates. */
7780
public final class DateFieldMapper extends FieldMapper {
7881

82+
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(DateFieldMapper.class);
7983
private static final Logger logger = LogManager.getLogger(DateFieldMapper.class);
8084

8185
public static final String CONTENT_TYPE = "date";
@@ -342,7 +346,20 @@ private Long parseNullValue(DateFieldType fieldType) {
342346
try {
343347
return fieldType.parse(nullValue.getValue());
344348
} catch (Exception e) {
345-
throw new MapperParsingException("Error parsing [null_value] on field [" + leafName() + "]: " + e.getMessage(), e);
349+
if (indexCreatedVersion.onOrAfter(IndexVersions.V_8_0_0)) {
350+
throw new MapperParsingException("Error parsing [null_value] on field [" + leafName() + "]: " + e.getMessage(), e);
351+
} else {
352+
DEPRECATION_LOGGER.warn(
353+
DeprecationCategory.MAPPINGS,
354+
"date_mapper_null_field",
355+
"Error parsing ["
356+
+ nullValue.getValue()
357+
+ "] as date in [null_value] on field ["
358+
+ leafName()
359+
+ "]); [null_value] will be ignored"
360+
);
361+
return null;
362+
}
346363
}
347364
}
348365

server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import org.apache.lucene.search.Query;
2424
import org.apache.lucene.util.BytesRef;
2525
import org.apache.lucene.util.automaton.CompiledAutomaton;
26+
import org.elasticsearch.common.logging.DeprecationCategory;
27+
import org.elasticsearch.common.logging.DeprecationLogger;
2628
import org.elasticsearch.common.network.InetAddresses;
2729
import org.elasticsearch.common.network.NetworkAddress;
2830
import org.elasticsearch.core.Nullable;
2931
import org.elasticsearch.core.Tuple;
3032
import org.elasticsearch.index.IndexVersion;
33+
import org.elasticsearch.index.IndexVersions;
3134
import org.elasticsearch.index.fielddata.FieldDataContext;
3235
import org.elasticsearch.index.fielddata.IndexFieldData;
3336
import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData;
@@ -59,6 +62,8 @@
5962
*/
6063
public class IpFieldMapper extends FieldMapper {
6164

65+
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(IpFieldMapper.class);
66+
6267
public static final String CONTENT_TYPE = "ip";
6368

6469
private static IpFieldMapper toType(FieldMapper in) {
@@ -129,7 +134,20 @@ private InetAddress parseNullValue() {
129134
try {
130135
return InetAddresses.forString(nullValueAsString);
131136
} catch (Exception e) {
132-
throw new MapperParsingException("Error parsing [null_value] on field [" + leafName() + "]: " + e.getMessage(), e);
137+
if (indexCreatedVersion.onOrAfter(IndexVersions.V_8_0_0)) {
138+
throw new MapperParsingException("Error parsing [null_value] on field [" + leafName() + "]: " + e.getMessage(), e);
139+
} else {
140+
DEPRECATION_LOGGER.warn(
141+
DeprecationCategory.MAPPINGS,
142+
"ip_mapper_null_field",
143+
"Error parsing ["
144+
+ nullValue.getValue()
145+
+ "] as IP in [null_value] on field ["
146+
+ leafName()
147+
+ "]); [null_value] will be ignored"
148+
);
149+
return null;
150+
}
133151
}
134152
}
135153

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.elasticsearch.index.IndexVersions;
2121
import org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType;
2222
import org.elasticsearch.script.DateFieldScript;
23+
import org.elasticsearch.script.ScriptService;
2324
import org.elasticsearch.search.DocValueFormat;
25+
import org.elasticsearch.test.index.IndexVersionUtils;
2426
import org.elasticsearch.xcontent.XContentBuilder;
2527

2628
import java.io.IOException;
@@ -44,6 +46,7 @@
4446
import static org.hamcrest.Matchers.lessThan;
4547
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4648
import static org.hamcrest.Matchers.notNullValue;
49+
import static org.mockito.Mockito.mock;
4750

4851
public class DateFieldMapperTests extends MapperTestCase {
4952

@@ -245,6 +248,10 @@ public void testBadNullValue() throws IOException {
245248
+ "failed to parse date field [foo] with format [strict_date_optional_time||epoch_millis]"
246249
)
247250
);
251+
252+
createDocumentMapper(IndexVersions.V_7_9_0, fieldMapping(b -> b.field("type", "date").field("null_value", "foo")));
253+
254+
assertWarnings("Error parsing [foo] as date in [null_value] on field [field]); [null_value] will be ignored");
248255
}
249256

250257
public void testNullConfigValuesFail() {
@@ -757,4 +764,51 @@ public void testLegacyField() throws Exception {
757764
assertNotEquals(DEFAULT_DATE_TIME_FORMATTER, ((DateFieldType) service.fieldType("mydate")).dateTimeFormatter);
758765
}
759766

767+
public void testLegacyDateFormatName() {
768+
DateFieldMapper.Builder builder = new DateFieldMapper.Builder(
769+
"format",
770+
DateFieldMapper.Resolution.MILLISECONDS,
771+
null,
772+
mock(ScriptService.class),
773+
true,
774+
// BWC compatible index, e.g 7.x
775+
IndexVersionUtils.randomVersionBetween(
776+
random(),
777+
IndexVersions.V_7_0_0,
778+
IndexVersionUtils.getPreviousVersion(IndexVersions.V_8_0_0)
779+
)
780+
);
781+
782+
// Check that we allow the use of camel case date formats on 7.x indices
783+
@SuppressWarnings("unchecked")
784+
FieldMapper.Parameter<String> formatParam = (FieldMapper.Parameter<String>) builder.getParameters()[3];
785+
formatParam.parse("date_time_format", mock(MappingParserContext.class), "strictDateOptionalTime");
786+
builder.buildFormatter(); // shouldn't throw exception
787+
788+
formatParam.parse("date_time_format", mock(MappingParserContext.class), "strictDateOptionalTime||strictDateOptionalTimeNanos");
789+
builder.buildFormatter(); // shouldn't throw exception
790+
791+
DateFieldMapper.Builder newFieldBuilder = new DateFieldMapper.Builder(
792+
"format",
793+
DateFieldMapper.Resolution.MILLISECONDS,
794+
null,
795+
mock(ScriptService.class),
796+
true,
797+
IndexVersion.current()
798+
);
799+
800+
@SuppressWarnings("unchecked")
801+
final FieldMapper.Parameter<String> newFormatParam = (FieldMapper.Parameter<String>) newFieldBuilder.getParameters()[3];
802+
803+
// Check that we don't allow the use of camel case date formats on 8.x indices
804+
assertEquals(
805+
"Error parsing [format] on field [format]: Invalid format: [strictDateOptionalTime]: Unknown pattern letter: t",
806+
expectThrows(IllegalArgumentException.class, () -> {
807+
newFormatParam.parse("date_time_format", mock(MappingParserContext.class), "strictDateOptionalTime");
808+
assertEquals("strictDateOptionalTime", newFormatParam.getValue());
809+
newFieldBuilder.buildFormatter();
810+
}).getMessage()
811+
);
812+
813+
}
760814
}

server/src/test/java/org/elasticsearch/index/mapper/IpFieldMapperTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.core.Tuple;
2222
import org.elasticsearch.index.IndexMode;
2323
import org.elasticsearch.index.IndexVersion;
24+
import org.elasticsearch.index.IndexVersions;
2425
import org.elasticsearch.script.IpFieldScript;
2526
import org.elasticsearch.test.ESTestCase;
2627
import org.elasticsearch.xcontent.XContentBuilder;
@@ -208,6 +209,12 @@ public void testNullValue() throws IOException {
208209
e.getMessage(),
209210
"Failed to parse mapping: Error parsing [null_value] on field [field]: ':1' is not an IP string literal."
210211
);
212+
213+
createDocumentMapper(IndexVersions.V_7_9_0, fieldMapping(b -> {
214+
b.field("type", "ip");
215+
b.field("null_value", ":1");
216+
}));
217+
assertWarnings("Error parsing [:1] as IP in [null_value] on field [field]); [null_value] will be ignored");
211218
}
212219

213220
public void testDimension() throws IOException {

0 commit comments

Comments
 (0)