12
12
import org .elasticsearch .common .CheckedConsumer ;
13
13
import org .elasticsearch .common .Explicit ;
14
14
import org .elasticsearch .common .TriFunction ;
15
- import org .elasticsearch .common .geo .GeoPoint ;
16
- import org .elasticsearch .common .geo .GeometryFormat ;
17
- import org .elasticsearch .common .geo .GeometryParser ;
18
15
import org .elasticsearch .common .xcontent .XContentParser ;
19
- import org .elasticsearch .geometry .Geometry ;
20
- import org .elasticsearch .geometry .Point ;
21
16
import org .elasticsearch .index .mapper .Mapper .TypeParser .ParserContext ;
22
17
23
18
import java .io .IOException ;
28
23
/** Base class for for spatial fields that only support indexing points */
29
24
public abstract class AbstractPointGeometryFieldMapper <T > extends AbstractGeometryFieldMapper <T > {
30
25
31
- public static Parameter <ParsedPoint > nullValueParam (Function <FieldMapper , ParsedPoint > initializer ,
32
- TriFunction <String , ParserContext , Object , ParsedPoint > parser ,
33
- Supplier <ParsedPoint > def ) {
34
- return new Parameter <>("null_value" , false , def , parser , initializer );
26
+ public static < T > Parameter <T > nullValueParam (Function <FieldMapper , T > initializer ,
27
+ TriFunction <String , ParserContext , Object , T > parser ,
28
+ Supplier <T > def ) {
29
+ return new Parameter <T >("null_value" , false , def , parser , initializer );
35
30
}
36
31
37
- protected final ParsedPoint nullValue ;
32
+ protected final T nullValue ;
38
33
39
34
protected AbstractPointGeometryFieldMapper (String simpleName , MappedFieldType mappedFieldType ,
40
35
MultiFields multiFields , Explicit <Boolean > ignoreMalformed ,
41
- Explicit <Boolean > ignoreZValue , ParsedPoint nullValue , CopyTo copyTo ,
42
- Parser <? extends T > parser ) {
36
+ Explicit <Boolean > ignoreZValue , T nullValue , CopyTo copyTo ,
37
+ Parser <T > parser ) {
43
38
super (simpleName , mappedFieldType , ignoreMalformed , ignoreZValue , multiFields , copyTo , parser );
44
39
this .nullValue = nullValue ;
45
40
}
46
41
47
42
protected AbstractPointGeometryFieldMapper (String simpleName , MappedFieldType mappedFieldType ,
48
43
MultiFields multiFields , CopyTo copyTo ,
49
- Parser <? extends T > parser , String onScriptError ) {
44
+ Parser <T > parser , String onScriptError ) {
50
45
super (simpleName , mappedFieldType , multiFields , copyTo , parser , onScriptError );
51
46
this .nullValue = null ;
52
47
}
@@ -56,80 +51,62 @@ public final boolean parsesArrayValue() {
56
51
return true ;
57
52
}
58
53
59
- public ParsedPoint getNullValue () {
54
+ public T getNullValue () {
60
55
return nullValue ;
61
56
}
62
57
63
- /** represents a Point that has been parsed by {@link PointParser} */
64
- public interface ParsedPoint {
65
- void validate (String fieldName );
66
- void normalize (String fieldName );
67
- void resetCoords (double x , double y );
68
- Point asGeometry ();
69
- default boolean isNormalizable (double coord ) {
70
- return Double .isNaN (coord ) == false && Double .isInfinite (coord ) == false ;
71
- }
72
- }
73
-
74
- /** A parser implementation that can parse the various point formats */
75
- public static class PointParser <P extends ParsedPoint > extends Parser <P > {
76
- /**
77
- * Note that this parser is only used for formatting values.
78
- */
79
- private final GeometryParser geometryParser ;
80
- private final String field ;
81
- private final Supplier <P > pointSupplier ;
82
- private final CheckedBiFunction <XContentParser , P , P , IOException > objectParser ;
83
- private final P nullValue ;
58
+ /** A base parser implementation for point formats */
59
+ protected abstract static class PointParser <T > extends Parser <T > {
60
+ protected final String field ;
61
+ private final Supplier <T > pointSupplier ;
62
+ private final CheckedBiFunction <XContentParser , T , T , IOException > objectParser ;
63
+ private final T nullValue ;
84
64
private final boolean ignoreZValue ;
85
- private final boolean ignoreMalformed ;
86
-
87
- public PointParser (String field ,
88
- Supplier <P > pointSupplier ,
89
- CheckedBiFunction <XContentParser , P , P , IOException > objectParser ,
90
- P nullValue ,
91
- boolean ignoreZValue ,
92
- boolean ignoreMalformed ) {
65
+ protected final boolean ignoreMalformed ;
66
+
67
+ protected PointParser (String field ,
68
+ Supplier <T > pointSupplier ,
69
+ CheckedBiFunction <XContentParser , T , T , IOException > objectParser ,
70
+ T nullValue ,
71
+ boolean ignoreZValue ,
72
+ boolean ignoreMalformed ) {
93
73
this .field = field ;
94
74
this .pointSupplier = pointSupplier ;
95
75
this .objectParser = objectParser ;
96
- this .nullValue = nullValue == null ? null : process (nullValue );
76
+ this .nullValue = nullValue == null ? null : validate (nullValue );
97
77
this .ignoreZValue = ignoreZValue ;
98
78
this .ignoreMalformed = ignoreMalformed ;
99
- this .geometryParser = new GeometryParser (true , true , true );
100
79
}
101
80
102
- private P process (P in ) {
103
- if (ignoreMalformed == false ) {
104
- in .validate (field );
105
- } else {
106
- in .normalize (field );
107
- }
108
- return in ;
109
- }
81
+ protected abstract T validate (T in );
82
+
83
+ protected abstract void reset (T in , double x , double y );
110
84
111
85
@ Override
112
86
public void parse (
113
87
XContentParser parser ,
114
- CheckedConsumer <P , IOException > consumer ,
88
+ CheckedConsumer <T , IOException > consumer ,
115
89
Consumer <Exception > onMalformed
116
90
) throws IOException {
117
91
if (parser .currentToken () == XContentParser .Token .START_ARRAY ) {
118
92
XContentParser .Token token = parser .nextToken ();
119
- P point = pointSupplier .get ();
93
+ T point = pointSupplier .get ();
120
94
if (token == XContentParser .Token .VALUE_NUMBER ) {
121
95
double x = parser .doubleValue ();
122
96
parser .nextToken ();
123
97
double y = parser .doubleValue ();
124
98
token = parser .nextToken ();
125
99
if (token == XContentParser .Token .VALUE_NUMBER ) {
126
- GeoPoint .assertZValue (ignoreZValue , parser .doubleValue ());
100
+ if (ignoreZValue == false ) {
101
+ throw new ElasticsearchParseException ("Exception parsing coordinates: found Z value [{}] but [ignore_z_value] "
102
+ + "parameter is [{}]" , parser .doubleValue (), ignoreZValue );
103
+ }
127
104
} else if (token != XContentParser .Token .END_ARRAY ) {
128
105
throw new ElasticsearchParseException ("field type does not accept > 3 dimensions" );
129
106
}
130
107
131
- point . resetCoords ( x , y );
132
- consumer .accept (process (point ));
108
+ reset ( point , x , y );
109
+ consumer .accept (validate (point ));
133
110
} else {
134
111
while (token != XContentParser .Token .END_ARRAY ) {
135
112
parseAndConsumeFromObject (parser , point , consumer , onMalformed );
@@ -148,22 +125,16 @@ public void parse(
148
125
149
126
private void parseAndConsumeFromObject (
150
127
XContentParser parser ,
151
- P point ,
152
- CheckedConsumer <P , IOException > consumer ,
128
+ T point ,
129
+ CheckedConsumer <T , IOException > consumer ,
153
130
Consumer <Exception > onMalformed
154
131
) {
155
132
try {
156
133
point = objectParser .apply (parser , point );
157
- consumer .accept (process (point ));
134
+ consumer .accept (validate (point ));
158
135
} catch (Exception e ) {
159
136
onMalformed .accept (e );
160
137
}
161
138
}
162
-
163
- @ Override
164
- public Object format (P point , String format ) {
165
- GeometryFormat <Geometry > geometryFormat = geometryParser .geometryFormat (format );
166
- return geometryFormat .toXContentAsObject (point .asGeometry ());
167
- }
168
139
}
169
140
}
0 commit comments