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,44 @@ 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
+ DEPRECATION_LOGGER .deprecate ("ip_mapper_null_field" , "Error parsing [" + nullValue .getValue ()
104
+ + "] as IP in [null_value] on field [" + name () + "]); [null_value] will be ignored" );
105
+ return null ;
106
+ }
107
+ }
108
+
98
109
@ Override
99
110
protected List <Parameter <?>> getParameters () {
100
- return Arrays .asList (indexed , hasDocValues , stored , ignoreMalformed , nullValue );
111
+ return Arrays .asList (indexed , hasDocValues , stored , ignoreMalformed , nullValue , meta );
101
112
}
102
113
103
114
@ Override
@@ -111,7 +122,7 @@ public IpFieldMapper build(BuilderContext context) {
111
122
112
123
public static final TypeParser PARSER = new TypeParser ((n , c ) -> {
113
124
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING .get (c .getSettings ());
114
- return new Builder (n , ignoreMalformedByDefault );
125
+ return new Builder (n , ignoreMalformedByDefault , c . indexVersionCreated () );
115
126
});
116
127
117
128
public static final class IpFieldType extends SimpleMappedFieldType {
@@ -322,9 +333,12 @@ public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
322
333
private final boolean hasDocValues ;
323
334
private final boolean stored ;
324
335
private final boolean ignoreMalformed ;
336
+
325
337
private final InetAddress nullValue ;
338
+ private final String nullValueAsString ;
326
339
327
340
private final boolean ignoreMalformedByDefault ;
341
+ private final Version indexCreatedVersion ;
328
342
329
343
private IpFieldMapper (
330
344
String simpleName ,
@@ -338,7 +352,9 @@ private IpFieldMapper(
338
352
this .hasDocValues = builder .hasDocValues .getValue ();
339
353
this .stored = builder .stored .getValue ();
340
354
this .ignoreMalformed = builder .ignoreMalformed .getValue ();
341
- this .nullValue = builder .nullValue .getValue ();
355
+ this .nullValue = builder .parseNullValue ();
356
+ this .nullValueAsString = builder .nullValue .getValue ();
357
+ this .indexCreatedVersion = builder .indexCreatedVersion ;
342
358
}
343
359
344
360
@ Override
@@ -424,6 +440,6 @@ protected Object parseSourceValue(Object value) {
424
440
425
441
@ Override
426
442
public ParametrizedFieldMapper .Builder getMergeBuilder () {
427
- return new Builder (simpleName (), ignoreMalformedByDefault ).init (this );
443
+ return new Builder (simpleName (), ignoreMalformedByDefault , indexCreatedVersion ).init (this );
428
444
}
429
445
}
0 commit comments