Skip to content

Commit

Permalink
Remove the 'array value parser' marker interface. (#57571)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jtibshirani authored Jun 3, 2020
1 parent 0df456c commit 88a2aeb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ protected void parseCreateField(ParseContext context) throws IOException {
protected abstract void addDocValuesFields(String name, Processed geometry, List<IndexableField> fields, ParseContext context);
protected abstract void addMultiFields(ParseContext context, Processed geometry) throws IOException;

@Override
public final boolean parsesArrayValue() {
return true;
}

/** parsing logic for geometry indexing */
@Override
public void parse(ParseContext context) throws IOException {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
* This field can also be extended to add search criteria to suggestions
* for query-time filtering and boosting (see {@link ContextMappings}
*/
public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapperParser {
public class CompletionFieldMapper extends FieldMapper {
public static final String CONTENT_TYPE = "completion";

/**
Expand Down Expand Up @@ -426,6 +426,11 @@ public CompletionFieldType fieldType() {
return (CompletionFieldType) super.fieldType();
}

@Override
public boolean parsesArrayValue() {
return true;
}

/**
* Parses and indexes inputs
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ private static void parseArray(ParseContext context, ObjectMapper parentMapper,
// There is a concrete mapper for this field already. Need to check if the mapper
// expects an array, if so we pass the context straight to the mapper and if not
// we serialize the array components
if (mapper instanceof ArrayValueMapperParser) {
if (parsesArrayValue(mapper)) {
parseObjectOrField(context, mapper);
} else {
parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
Expand All @@ -543,7 +543,7 @@ private static void parseArray(ParseContext context, ObjectMapper parentMapper,
Mapper.BuilderContext builderContext = new Mapper.BuilderContext(context.indexSettings().getSettings(), context.path());
mapper = builder.build(builderContext);
assert mapper != null;
if (mapper instanceof ArrayValueMapperParser) {
if (parsesArrayValue(mapper)) {
context.addDynamicMapper(mapper);
context.path().add(arrayFieldName);
parseObjectOrField(context, mapper);
Expand All @@ -562,6 +562,10 @@ private static void parseArray(ParseContext context, ObjectMapper parentMapper,
}
}

private static boolean parsesArrayValue(Mapper mapper) {
return mapper instanceof FieldMapper && ((FieldMapper) mapper).parsesArrayValue();
}

private static void parseNonDynamicArray(ParseContext context, ObjectMapper mapper,
final String lastFieldName, String arrayFieldName) throws IOException {
XContentParser parser = context.parser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ public CopyTo copyTo() {
return copyTo;
}

/**
* Whether this mapper can handle an array value during document parsing. If true,
* when an array is encountered during parsing, the document parser will pass the
* whole array to the mapper. If false, the array is split into individual values
* and each value is passed to the mapper for parsing.
*/
public boolean parsesArrayValue() {
return false;
}

/**
* Parse the field value using the provided {@link ParseContext}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
*
* Uses lucene 6 LatLonPoint encoding
*/
public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends GeoPoint>, List<? extends GeoPoint>>
implements ArrayValueMapperParser {
public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends GeoPoint>, List<? extends GeoPoint>> {
public static final String CONTENT_TYPE = "geo_point";

public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, GeoPointFieldType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper;
import org.elasticsearch.index.mapper.ArrayValueMapperParser;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.xpack.spatial.common.CartesianPoint;
Expand All @@ -30,8 +29,7 @@
*
* Uses lucene 8 XYPoint encoding
*/
public class PointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends CartesianPoint>, List<? extends CartesianPoint>>
implements ArrayValueMapperParser {
public class PointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends CartesianPoint>, List<? extends CartesianPoint>> {
public static final String CONTENT_TYPE = "point";

public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, PointFieldType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.mapper.ArrayValueMapperParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.Mapper;
Expand All @@ -40,7 +39,7 @@
/**
* A {@link FieldMapper} for indexing a dense vector of floats.
*/
public class DenseVectorFieldMapper extends FieldMapper implements ArrayValueMapperParser {
public class DenseVectorFieldMapper extends FieldMapper {

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

@Override
public boolean parsesArrayValue() {
return true;
}

@Override
public void parse(ParseContext context) throws IOException {
if (context.externalValueSet()) {
Expand Down

0 comments on commit 88a2aeb

Please sign in to comment.