1212import org .elasticsearch .common .CheckedConsumer ;
1313import org .elasticsearch .common .Explicit ;
1414import 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 ;
1815import org .elasticsearch .common .xcontent .XContentParser ;
19- import org .elasticsearch .geometry .Geometry ;
20- import org .elasticsearch .geometry .Point ;
2116import org .elasticsearch .index .mapper .Mapper .TypeParser .ParserContext ;
2217
2318import java .io .IOException ;
2823/** Base class for for spatial fields that only support indexing points */
2924public abstract class AbstractPointGeometryFieldMapper <T > extends AbstractGeometryFieldMapper <T > {
3025
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 );
3530 }
3631
37- protected final ParsedPoint nullValue ;
32+ protected final T nullValue ;
3833
3934 protected AbstractPointGeometryFieldMapper (String simpleName , MappedFieldType mappedFieldType ,
4035 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 ) {
4338 super (simpleName , mappedFieldType , ignoreMalformed , ignoreZValue , multiFields , copyTo , parser );
4439 this .nullValue = nullValue ;
4540 }
4641
4742 protected AbstractPointGeometryFieldMapper (String simpleName , MappedFieldType mappedFieldType ,
4843 MultiFields multiFields , CopyTo copyTo ,
49- Parser <? extends T > parser , String onScriptError ) {
44+ Parser <T > parser , String onScriptError ) {
5045 super (simpleName , mappedFieldType , multiFields , copyTo , parser , onScriptError );
5146 this .nullValue = null ;
5247 }
@@ -56,80 +51,62 @@ public final boolean parsesArrayValue() {
5651 return true ;
5752 }
5853
59- public ParsedPoint getNullValue () {
54+ public T getNullValue () {
6055 return nullValue ;
6156 }
6257
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 ;
8464 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 ) {
9373 this .field = field ;
9474 this .pointSupplier = pointSupplier ;
9575 this .objectParser = objectParser ;
96- this .nullValue = nullValue == null ? null : process (nullValue );
76+ this .nullValue = nullValue == null ? null : validate (nullValue );
9777 this .ignoreZValue = ignoreZValue ;
9878 this .ignoreMalformed = ignoreMalformed ;
99- this .geometryParser = new GeometryParser (true , true , true );
10079 }
10180
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 );
11084
11185 @ Override
11286 public void parse (
11387 XContentParser parser ,
114- CheckedConsumer <P , IOException > consumer ,
88+ CheckedConsumer <T , IOException > consumer ,
11589 Consumer <Exception > onMalformed
11690 ) throws IOException {
11791 if (parser .currentToken () == XContentParser .Token .START_ARRAY ) {
11892 XContentParser .Token token = parser .nextToken ();
119- P point = pointSupplier .get ();
93+ T point = pointSupplier .get ();
12094 if (token == XContentParser .Token .VALUE_NUMBER ) {
12195 double x = parser .doubleValue ();
12296 parser .nextToken ();
12397 double y = parser .doubleValue ();
12498 token = parser .nextToken ();
12599 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+ }
127104 } else if (token != XContentParser .Token .END_ARRAY ) {
128105 throw new ElasticsearchParseException ("field type does not accept > 3 dimensions" );
129106 }
130107
131- point . resetCoords ( x , y );
132- consumer .accept (process (point ));
108+ reset ( point , x , y );
109+ consumer .accept (validate (point ));
133110 } else {
134111 while (token != XContentParser .Token .END_ARRAY ) {
135112 parseAndConsumeFromObject (parser , point , consumer , onMalformed );
@@ -148,22 +125,16 @@ public void parse(
148125
149126 private void parseAndConsumeFromObject (
150127 XContentParser parser ,
151- P point ,
152- CheckedConsumer <P , IOException > consumer ,
128+ T point ,
129+ CheckedConsumer <T , IOException > consumer ,
153130 Consumer <Exception > onMalformed
154131 ) {
155132 try {
156133 point = objectParser .apply (parser , point );
157- consumer .accept (process (point ));
134+ consumer .accept (validate (point ));
158135 } catch (Exception e ) {
159136 onMalformed .accept (e );
160137 }
161138 }
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- }
168139 }
169140}
0 commit comments