Skip to content

Commit e0a15e8

Browse files
authored
Remove the 'array value parser' marker interface. (#57571) (#57622)
This PR replaces the marker interface with the method FieldMapper#parsesArrayValue. I find this cleaner and it will help with the fields retrieval work (#55363). The refactor also ensures that only field mappers can declare they parse array values. Previously other types like ObjectMapper could implement the marker interface and be passed array values, which doesn't make sense.
1 parent 7fd94f7 commit e0a15e8

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ protected void parseCreateField(ParseContext context) throws IOException {
307307
protected abstract void addDocValuesFields(String name, Processed geometry, List<IndexableField> fields, ParseContext context);
308308
protected abstract void addMultiFields(ParseContext context, Processed geometry) throws IOException;
309309

310+
@Override
311+
public final boolean parsesArrayValue() {
312+
return true;
313+
}
314+
310315
/** parsing logic for geometry indexing */
311316
@Override
312317
public void parse(ParseContext context) throws IOException {

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

-29
This file was deleted.

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
* This field can also be extended to add search criteria to suggestions
8484
* for query-time filtering and boosting (see {@link ContextMappings}
8585
*/
86-
public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapperParser {
86+
public class CompletionFieldMapper extends FieldMapper {
8787
public static final String CONTENT_TYPE = "completion";
8888

8989
/**
@@ -421,6 +421,11 @@ public CompletionFieldType fieldType() {
421421
return (CompletionFieldType) super.fieldType();
422422
}
423423

424+
@Override
425+
public boolean parsesArrayValue() {
426+
return true;
427+
}
428+
424429
/**
425430
* Parses and indexes inputs
426431
*

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ private static void parseArray(ParseContext context, ObjectMapper parentMapper,
538538
// There is a concrete mapper for this field already. Need to check if the mapper
539539
// expects an array, if so we pass the context straight to the mapper and if not
540540
// we serialize the array components
541-
if (mapper instanceof ArrayValueMapperParser) {
541+
if (parsesArrayValue(mapper)) {
542542
parseObjectOrField(context, mapper);
543543
} else {
544544
parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
@@ -559,7 +559,7 @@ private static void parseArray(ParseContext context, ObjectMapper parentMapper,
559559
Mapper.BuilderContext builderContext = new Mapper.BuilderContext(context.indexSettings().getSettings(), context.path());
560560
mapper = builder.build(builderContext);
561561
assert mapper != null;
562-
if (mapper instanceof ArrayValueMapperParser) {
562+
if (parsesArrayValue(mapper)) {
563563
context.addDynamicMapper(mapper);
564564
context.path().add(arrayFieldName);
565565
parseObjectOrField(context, mapper);
@@ -578,6 +578,10 @@ private static void parseArray(ParseContext context, ObjectMapper parentMapper,
578578
}
579579
}
580580

581+
private static boolean parsesArrayValue(Mapper mapper) {
582+
return mapper instanceof FieldMapper && ((FieldMapper) mapper).parsesArrayValue();
583+
}
584+
581585
private static void parseNonDynamicArray(ParseContext context, ObjectMapper mapper,
582586
final String lastFieldName, String arrayFieldName) throws IOException {
583587
XContentParser parser = context.parser();

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

+10
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ public CopyTo copyTo() {
273273
return copyTo;
274274
}
275275

276+
/**
277+
* Whether this mapper can handle an array value during document parsing. If true,
278+
* when an array is encountered during parsing, the document parser will pass the
279+
* whole array to the mapper. If false, the array is split into individual values
280+
* and each value is passed to the mapper for parsing.
281+
*/
282+
public boolean parsesArrayValue() {
283+
return false;
284+
}
285+
276286
/**
277287
* Parse the field value using the provided {@link ParseContext}.
278288
*/

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
*
4545
* Uses lucene 6 LatLonPoint encoding
4646
*/
47-
public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends GeoPoint>, List<? extends GeoPoint>>
48-
implements ArrayValueMapperParser {
47+
public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends GeoPoint>, List<? extends GeoPoint>> {
4948
public static final String CONTENT_TYPE = "geo_point";
5049

5150
public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, GeoPointFieldType> {

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.common.settings.Settings;
1414
import org.elasticsearch.common.xcontent.XContentParser;
1515
import org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper;
16-
import org.elasticsearch.index.mapper.ArrayValueMapperParser;
1716
import org.elasticsearch.index.mapper.MappedFieldType;
1817
import org.elasticsearch.index.mapper.ParseContext;
1918
import org.elasticsearch.xpack.spatial.common.CartesianPoint;
@@ -30,8 +29,7 @@
3029
*
3130
* Uses lucene 8 XYPoint encoding
3231
*/
33-
public class PointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends CartesianPoint>, List<? extends CartesianPoint>>
34-
implements ArrayValueMapperParser {
32+
public class PointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends CartesianPoint>, List<? extends CartesianPoint>> {
3533
public static final String CONTENT_TYPE = "point";
3634

3735
public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, PointFieldType> {

x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.common.xcontent.XContentParser.Token;
1919
import org.elasticsearch.common.xcontent.support.XContentMapValues;
2020
import org.elasticsearch.index.fielddata.IndexFieldData;
21-
import org.elasticsearch.index.mapper.ArrayValueMapperParser;
2221
import org.elasticsearch.index.mapper.FieldMapper;
2322
import org.elasticsearch.index.mapper.MappedFieldType;
2423
import org.elasticsearch.index.mapper.Mapper;
@@ -40,7 +39,7 @@
4039
/**
4140
* A {@link FieldMapper} for indexing a dense vector of floats.
4241
*/
43-
public class DenseVectorFieldMapper extends FieldMapper implements ArrayValueMapperParser {
42+
public class DenseVectorFieldMapper extends FieldMapper {
4443

4544
public static final String CONTENT_TYPE = "dense_vector";
4645
public static short MAX_DIMS_COUNT = 2048; //maximum allowed number of dimensions
@@ -173,6 +172,11 @@ public DenseVectorFieldType fieldType() {
173172
return (DenseVectorFieldType) super.fieldType();
174173
}
175174

175+
@Override
176+
public boolean parsesArrayValue() {
177+
return true;
178+
}
179+
176180
@Override
177181
public void parse(ParseContext context) throws IOException {
178182
if (context.externalValueSet()) {

0 commit comments

Comments
 (0)