30
30
import org .apache .lucene .search .TermQuery ;
31
31
import org .apache .lucene .util .ArrayUtil ;
32
32
import org .apache .lucene .util .BytesRef ;
33
+ import org .elasticsearch .Version ;
33
34
import org .elasticsearch .common .Nullable ;
34
35
import org .elasticsearch .common .collect .Tuple ;
36
+ import org .elasticsearch .common .logging .DeprecationLogger ;
35
37
import org .elasticsearch .common .network .InetAddresses ;
36
- import org .elasticsearch .common .network .NetworkAddress ;
37
38
import org .elasticsearch .index .fielddata .IndexFieldData ;
38
39
import org .elasticsearch .index .fielddata .ScriptDocValues ;
39
40
import org .elasticsearch .index .fielddata .plain .SortedSetOrdinalsIndexFieldData ;
55
56
/** A {@link FieldMapper} for ip addresses. */
56
57
public class IpFieldMapper extends ParametrizedFieldMapper {
57
58
59
+ private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger .getLogger (IpFieldMapper .class );
60
+
58
61
public static final String CONTENT_TYPE = "ip" ;
59
62
60
63
private static IpFieldMapper toType (FieldMapper in ) {
@@ -68,36 +71,48 @@ public static class Builder extends ParametrizedFieldMapper.Builder {
68
71
private final Parameter <Boolean > stored = Parameter .storeParam (m -> toType (m ).stored , false );
69
72
70
73
private final Parameter <Boolean > ignoreMalformed ;
71
- private final Parameter <InetAddress > nullValue = new Parameter <>("null_value" , false , () -> null ,
72
- (n , c , o ) -> o == null ? null : InetAddresses .forString (o .toString ()), m -> toType (m ).nullValue )
73
- .setSerializer ((b , f , v ) -> {
74
- if (v == null ) {
75
- b .nullField (f );
76
- } else {
77
- b .field (f , InetAddresses .toAddrString (v ));
78
- }
79
- }, NetworkAddress ::format )
80
- .acceptsNull ();
74
+ private final Parameter <String > nullValue
75
+ = Parameter .stringParam ("null_value" , false , m -> toType (m ).nullValueAsString , null ).acceptsNull ();
81
76
82
77
private final Parameter <Map <String , String >> meta = Parameter .metaParam ();
83
78
84
79
private final boolean ignoreMalformedByDefault ;
80
+ private final Version indexCreatedVersion ;
85
81
86
- public Builder (String name , boolean ignoreMalformedByDefault ) {
82
+ public Builder (String name , boolean ignoreMalformedByDefault , Version indexCreatedVersion ) {
87
83
super (name );
88
84
this .ignoreMalformedByDefault = ignoreMalformedByDefault ;
85
+ this .indexCreatedVersion = indexCreatedVersion ;
89
86
this .ignoreMalformed
90
87
= Parameter .boolParam ("ignore_malformed" , true , m -> toType (m ).ignoreMalformed , ignoreMalformedByDefault );
91
88
}
92
89
93
- Builder nullValue (InetAddress nullValue ) {
90
+ Builder nullValue (String nullValue ) {
94
91
this .nullValue .setValue (nullValue );
95
92
return this ;
96
93
}
97
94
95
+ private InetAddress parseNullValue () {
96
+ String nullValueAsString = nullValue .getValue ();
97
+ if (nullValueAsString == null ) {
98
+ return null ;
99
+ }
100
+ try {
101
+ return InetAddresses .forString (nullValueAsString );
102
+ } catch (Exception e ) {
103
+ if (indexCreatedVersion .onOrAfter (Version .V_8_0_0 )) {
104
+ throw new MapperParsingException ("Error parsing [null_value] on field [" + name () + "]: " + e .getMessage (), e );
105
+ } else {
106
+ DEPRECATION_LOGGER .deprecate ("ip_mapper_null_field" , "Error parsing [" + nullValue .getValue ()
107
+ + "] as IP in [null_value] on field [" + name () + "]); [null_value] will be ignored" );
108
+ return null ;
109
+ }
110
+ }
111
+ }
112
+
98
113
@ Override
99
114
protected List <Parameter <?>> getParameters () {
100
- return List .of (indexed , hasDocValues , stored , ignoreMalformed , nullValue );
115
+ return List .of (indexed , hasDocValues , stored , ignoreMalformed , nullValue , meta );
101
116
}
102
117
103
118
@ Override
@@ -111,7 +126,7 @@ public IpFieldMapper build(BuilderContext context) {
111
126
112
127
public static final TypeParser PARSER = new TypeParser ((n , c ) -> {
113
128
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING .get (c .getSettings ());
114
- return new Builder (n , ignoreMalformedByDefault );
129
+ return new Builder (n , ignoreMalformedByDefault , c . indexVersionCreated () );
115
130
});
116
131
117
132
public static final class IpFieldType extends SimpleMappedFieldType {
@@ -322,9 +337,12 @@ public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
322
337
private final boolean hasDocValues ;
323
338
private final boolean stored ;
324
339
private final boolean ignoreMalformed ;
340
+
325
341
private final InetAddress nullValue ;
342
+ private final String nullValueAsString ;
326
343
327
344
private final boolean ignoreMalformedByDefault ;
345
+ private final Version indexCreatedVersion ;
328
346
329
347
private IpFieldMapper (
330
348
String simpleName ,
@@ -338,7 +356,9 @@ private IpFieldMapper(
338
356
this .hasDocValues = builder .hasDocValues .getValue ();
339
357
this .stored = builder .stored .getValue ();
340
358
this .ignoreMalformed = builder .ignoreMalformed .getValue ();
341
- this .nullValue = builder .nullValue .getValue ();
359
+ this .nullValue = builder .parseNullValue ();
360
+ this .nullValueAsString = builder .nullValue .getValue ();
361
+ this .indexCreatedVersion = builder .indexCreatedVersion ;
342
362
}
343
363
344
364
@ Override
@@ -424,6 +444,6 @@ protected Object parseSourceValue(Object value) {
424
444
425
445
@ Override
426
446
public ParametrizedFieldMapper .Builder getMergeBuilder () {
427
- return new Builder (simpleName (), ignoreMalformedByDefault ).init (this );
447
+ return new Builder (simpleName (), ignoreMalformedByDefault , indexCreatedVersion ).init (this );
428
448
}
429
449
}
0 commit comments