Skip to content

Commit 603be00

Browse files
authored
Set parsesArrayValue to true for Shape field mappers (#73940)
bring shape and point implementation close together.
1 parent 1ed587d commit 603be00

File tree

12 files changed

+40
-48
lines changed

12 files changed

+40
-48
lines changed

server/src/main/java/org/elasticsearch/common/geo/GeoFormatterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GeoFormatterFactory {
3535
public static Function<Geometry, Object> getFormatter(String name) {
3636
Function<Geometry, Object> format = FORMATTERS.get(name);
3737
if (format == null) {
38-
throw new IllegalArgumentException("Unrecognized geometry format [" + format + "].");
38+
throw new IllegalArgumentException("Unrecognized geometry format [" + name + "].");
3939
}
4040
return format;
4141
}

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
package org.elasticsearch.index.mapper;
99

1010
import org.apache.lucene.search.Query;
11-
import org.apache.lucene.util.SetOnce;
12-
import org.elasticsearch.core.CheckedConsumer;
1311
import org.elasticsearch.common.Explicit;
1412
import org.elasticsearch.common.geo.GeoFormatterFactory;
1513
import org.elasticsearch.common.xcontent.XContentParser;
1614
import org.elasticsearch.common.xcontent.support.MapXContentParser;
15+
import org.elasticsearch.core.CheckedConsumer;
1716
import org.elasticsearch.index.analysis.NamedAnalyzer;
1817
import org.elasticsearch.index.query.SearchExecutionContext;
1918

@@ -65,12 +64,10 @@ private void fetchFromSource(Object sourceMap, Consumer<Object> consumer, Functi
6564
public abstract static class AbstractGeometryFieldType<T> extends MappedFieldType {
6665

6766
protected final Parser<T> geometryParser;
68-
protected final boolean parsesArrayValue;
6967

7068
protected AbstractGeometryFieldType(String name, boolean indexed, boolean stored, boolean hasDocValues,
71-
boolean parsesArrayValue, Parser<T> geometryParser, Map<String, String> meta) {
69+
Parser<T> geometryParser, Map<String, String> meta) {
7270
super(name, indexed, stored, hasDocValues, TextSearchInfo.NONE, meta);
73-
this.parsesArrayValue = parsesArrayValue;
7471
this.geometryParser = geometryParser;
7572
}
7673

@@ -81,32 +78,21 @@ public final Query termQuery(Object value, SearchExecutionContext context) {
8178
}
8279

8380
/**
84-
* Gets the formatter. If the method return null, then the format is unsupported and an error is thrown.
81+
* Gets the formatter by name.
8582
*/
8683
protected abstract Function<T, Object> getFormatter(String format);
8784

8885
@Override
8986
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
9087
Function<T, Object> formatter = getFormatter(format != null ? format : GeoFormatterFactory.GEOJSON);
91-
if (parsesArrayValue) {
92-
return new ArraySourceValueFetcher(name(), context) {
93-
@Override
94-
protected Object parseSourceValue(Object value) {
95-
List<Object> values = new ArrayList<>();
96-
geometryParser.fetchFromSource(value, values::add, formatter);
97-
return values;
98-
}
99-
};
100-
} else {
101-
return new SourceValueFetcher(name(), context) {
102-
@Override
103-
protected Object parseSourceValue(Object value) {
104-
SetOnce<Object> holder = new SetOnce<>();
105-
geometryParser.fetchFromSource(value, holder::set, formatter);
106-
return holder.get();
107-
}
108-
};
109-
}
88+
return new ArraySourceValueFetcher(name(), context) {
89+
@Override
90+
protected Object parseSourceValue(Object value) {
91+
List<Object> values = new ArrayList<>();
92+
geometryParser.fetchFromSource(value, values::add, formatter);
93+
return values;
94+
}
95+
};
11096
}
11197
}
11298

@@ -187,4 +173,9 @@ public boolean ignoreMalformed() {
187173
public boolean ignoreZValue() {
188174
return ignoreZValue.value();
189175
}
176+
177+
@Override
178+
public final boolean parsesArrayValue() {
179+
return true;
180+
}
190181
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ protected AbstractPointGeometryFieldMapper(String simpleName, MappedFieldType ma
4646
this.nullValue = null;
4747
}
4848

49-
@Override
50-
public final boolean parsesArrayValue() {
51-
return true;
52-
}
53-
5449
public T getNullValue() {
5550
return nullValue;
5651
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ public abstract static class AbstractShapeGeometryFieldType<T> extends AbstractG
3838
private final Orientation orientation;
3939

4040
protected AbstractShapeGeometryFieldType(String name, boolean isSearchable, boolean isStored, boolean hasDocValues,
41-
boolean parsesArrayValue, Parser<T> parser,
42-
Orientation orientation, Map<String, String> meta) {
43-
super(name, isSearchable, isStored, hasDocValues, parsesArrayValue, parser, meta);
41+
Parser<T> parser, Orientation orientation, Map<String, String> meta) {
42+
super(name, isSearchable, isStored, hasDocValues, parser, meta);
4443
this.orientation = orientation;
4544
}
4645

@@ -70,11 +69,6 @@ protected AbstractShapeGeometryFieldMapper(String simpleName, MappedFieldType ma
7069
ignoreMalformed, coerce, ignoreZValue, orientation, multiFields, copyTo, parser);
7170
}
7271

73-
@Override
74-
public final boolean parsesArrayValue() {
75-
return false;
76-
}
77-
7872
public boolean coerce() {
7973
return coerce.value();
8074
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public static class GeoPointFieldType extends AbstractGeometryFieldType<GeoPoint
224224

225225
private GeoPointFieldType(String name, boolean indexed, boolean stored, boolean hasDocValues,
226226
Parser<GeoPoint> parser, FieldValues<GeoPoint> scriptValues, Map<String, String> meta) {
227-
super(name, indexed, stored, hasDocValues, true, parser, meta);
227+
super(name, indexed, stored, hasDocValues, parser, meta);
228228
this.scriptValues = scriptValues;
229229
}
230230

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static class GeoShapeFieldType extends AbstractShapeGeometryFieldType<Geo
118118

119119
public GeoShapeFieldType(String name, boolean indexed, Orientation orientation,
120120
Parser<Geometry> parser, Map<String, String> meta) {
121-
super(name, indexed, false, false, false, parser, orientation, meta);
121+
super(name, indexed, false, false, parser, orientation, meta);
122122
}
123123

124124
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ public void parse(
3232
Consumer<Exception> onMalformed
3333
) throws IOException {
3434
try {
35-
consumer.accept(geometryParser.parse(parser));
35+
if (parser.currentToken() == XContentParser.Token.START_ARRAY) {
36+
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
37+
parse(parser, consumer, onMalformed);
38+
}
39+
} else {
40+
consumer.accept(geometryParser.parse(parser));
41+
}
3642
} catch (ParseException | ElasticsearchParseException | IllegalArgumentException e) {
3743
onMalformed.accept(e);
3844
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,13 @@ public void parse(
325325
Consumer<Exception> onMalformed
326326
) throws IOException {
327327
try {
328-
consumer.accept(ShapeParser.parse(parser));
328+
if (parser.currentToken() == XContentParser.Token.START_ARRAY) {
329+
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
330+
parse(parser, consumer, onMalformed);
331+
}
332+
} else {
333+
consumer.accept(ShapeParser.parse(parser));
334+
}
329335
} catch (ElasticsearchParseException e) {
330336
onMalformed.accept(e);
331337
}
@@ -351,7 +357,7 @@ public static final class GeoShapeFieldType extends AbstractShapeGeometryFieldTy
351357

352358
private GeoShapeFieldType(String name, boolean indexed, Orientation orientation,
353359
LegacyGeoShapeParser parser, Map<String, String> meta) {
354-
super(name, indexed, false, false, false, parser, orientation, meta);
360+
super(name, indexed, false, false, parser, orientation, meta);
355361
this.queryProcessor = new LegacyGeoShapeQueryProcessor(this);
356362
}
357363

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/CartesianFormatterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CartesianFormatterFactory {
3535
public static Function<Geometry, Object> getFormatter(String name) {
3636
Function<Geometry, Object> format = FORMATTERS.get(name);
3737
if (format == null) {
38-
throw new IllegalArgumentException("Unrecognized geometry format [" + format + "].");
38+
throw new IllegalArgumentException("Unrecognized geometry format [" + name + "].");
3939
}
4040
return format;
4141
}

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static final class GeoShapeWithDocValuesFieldType extends AbstractShapeGe
140140

141141
public GeoShapeWithDocValuesFieldType(String name, boolean indexed, boolean hasDocValues,
142142
Orientation orientation, GeoShapeParser parser, Map<String, String> meta) {
143-
super(name, indexed, false, hasDocValues, false, parser, orientation, meta);
143+
super(name, indexed, false, hasDocValues, parser, orientation, meta);
144144
}
145145

146146
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier<SearchLookup> searchLookup) {

0 commit comments

Comments
 (0)