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.
This commit is contained in:
parent
7fd94f7d0f
commit
e0a15e8dc4
|
@ -307,6 +307,11 @@ public abstract class AbstractGeometryFieldMapper<Parsed, Processed> extends Fie
|
||||||
protected abstract void addDocValuesFields(String name, Processed geometry, List<IndexableField> fields, ParseContext context);
|
protected abstract void addDocValuesFields(String name, Processed geometry, List<IndexableField> fields, ParseContext context);
|
||||||
protected abstract void addMultiFields(ParseContext context, Processed geometry) throws IOException;
|
protected abstract void addMultiFields(ParseContext context, Processed geometry) throws IOException;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final boolean parsesArrayValue() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/** parsing logic for geometry indexing */
|
/** parsing logic for geometry indexing */
|
||||||
@Override
|
@Override
|
||||||
public void parse(ParseContext context) throws IOException {
|
public void parse(ParseContext context) throws IOException {
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch licenses this file to you under
|
|
||||||
* the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
* not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.elasticsearch.index.mapper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A marker interface indicating that this mapper can handle array value, and the array
|
|
||||||
* itself should be passed to it.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public interface ArrayValueMapperParser {
|
|
||||||
}
|
|
|
@ -83,7 +83,7 @@ import static org.elasticsearch.index.mapper.TypeParsers.parseMultiField;
|
||||||
* This field can also be extended to add search criteria to suggestions
|
* This field can also be extended to add search criteria to suggestions
|
||||||
* for query-time filtering and boosting (see {@link ContextMappings}
|
* 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";
|
public static final String CONTENT_TYPE = "completion";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -421,6 +421,11 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp
|
||||||
return (CompletionFieldType) super.fieldType();
|
return (CompletionFieldType) super.fieldType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean parsesArrayValue() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses and indexes inputs
|
* Parses and indexes inputs
|
||||||
*
|
*
|
||||||
|
|
|
@ -538,7 +538,7 @@ final class DocumentParser {
|
||||||
// There is a concrete mapper for this field already. Need to check if the mapper
|
// 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
|
// expects an array, if so we pass the context straight to the mapper and if not
|
||||||
// we serialize the array components
|
// we serialize the array components
|
||||||
if (mapper instanceof ArrayValueMapperParser) {
|
if (parsesArrayValue(mapper)) {
|
||||||
parseObjectOrField(context, mapper);
|
parseObjectOrField(context, mapper);
|
||||||
} else {
|
} else {
|
||||||
parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
|
parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
|
||||||
|
@ -559,7 +559,7 @@ final class DocumentParser {
|
||||||
Mapper.BuilderContext builderContext = new Mapper.BuilderContext(context.indexSettings().getSettings(), context.path());
|
Mapper.BuilderContext builderContext = new Mapper.BuilderContext(context.indexSettings().getSettings(), context.path());
|
||||||
mapper = builder.build(builderContext);
|
mapper = builder.build(builderContext);
|
||||||
assert mapper != null;
|
assert mapper != null;
|
||||||
if (mapper instanceof ArrayValueMapperParser) {
|
if (parsesArrayValue(mapper)) {
|
||||||
context.addDynamicMapper(mapper);
|
context.addDynamicMapper(mapper);
|
||||||
context.path().add(arrayFieldName);
|
context.path().add(arrayFieldName);
|
||||||
parseObjectOrField(context, mapper);
|
parseObjectOrField(context, mapper);
|
||||||
|
@ -578,6 +578,10 @@ final class DocumentParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean parsesArrayValue(Mapper mapper) {
|
||||||
|
return mapper instanceof FieldMapper && ((FieldMapper) mapper).parsesArrayValue();
|
||||||
|
}
|
||||||
|
|
||||||
private static void parseNonDynamicArray(ParseContext context, ObjectMapper mapper,
|
private static void parseNonDynamicArray(ParseContext context, ObjectMapper mapper,
|
||||||
final String lastFieldName, String arrayFieldName) throws IOException {
|
final String lastFieldName, String arrayFieldName) throws IOException {
|
||||||
XContentParser parser = context.parser();
|
XContentParser parser = context.parser();
|
||||||
|
|
|
@ -273,6 +273,16 @@ public abstract class FieldMapper extends Mapper implements Cloneable {
|
||||||
return 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}.
|
* Parse the field value using the provided {@link ParseContext}.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -44,8 +44,7 @@ import java.util.Map;
|
||||||
*
|
*
|
||||||
* Uses lucene 6 LatLonPoint encoding
|
* Uses lucene 6 LatLonPoint encoding
|
||||||
*/
|
*/
|
||||||
public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends GeoPoint>, List<? extends GeoPoint>>
|
public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends GeoPoint>, List<? extends GeoPoint>> {
|
||||||
implements ArrayValueMapperParser {
|
|
||||||
public static final String CONTENT_TYPE = "geo_point";
|
public static final String CONTENT_TYPE = "geo_point";
|
||||||
|
|
||||||
public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, GeoPointFieldType> {
|
public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, GeoPointFieldType> {
|
||||||
|
|
|
@ -13,7 +13,6 @@ import org.elasticsearch.common.Explicit;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper;
|
import org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper;
|
||||||
import org.elasticsearch.index.mapper.ArrayValueMapperParser;
|
|
||||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||||
import org.elasticsearch.index.mapper.ParseContext;
|
import org.elasticsearch.index.mapper.ParseContext;
|
||||||
import org.elasticsearch.xpack.spatial.common.CartesianPoint;
|
import org.elasticsearch.xpack.spatial.common.CartesianPoint;
|
||||||
|
@ -30,8 +29,7 @@ import java.util.Map;
|
||||||
*
|
*
|
||||||
* Uses lucene 8 XYPoint encoding
|
* Uses lucene 8 XYPoint encoding
|
||||||
*/
|
*/
|
||||||
public class PointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends CartesianPoint>, List<? extends CartesianPoint>>
|
public class PointFieldMapper extends AbstractPointGeometryFieldMapper<List<? extends CartesianPoint>, List<? extends CartesianPoint>> {
|
||||||
implements ArrayValueMapperParser {
|
|
||||||
public static final String CONTENT_TYPE = "point";
|
public static final String CONTENT_TYPE = "point";
|
||||||
|
|
||||||
public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, PointFieldType> {
|
public static class Builder extends AbstractPointGeometryFieldMapper.Builder<Builder, PointFieldType> {
|
||||||
|
|
|
@ -18,7 +18,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser.Token;
|
import org.elasticsearch.common.xcontent.XContentParser.Token;
|
||||||
import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
||||||
import org.elasticsearch.index.fielddata.IndexFieldData;
|
import org.elasticsearch.index.fielddata.IndexFieldData;
|
||||||
import org.elasticsearch.index.mapper.ArrayValueMapperParser;
|
|
||||||
import org.elasticsearch.index.mapper.FieldMapper;
|
import org.elasticsearch.index.mapper.FieldMapper;
|
||||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||||
import org.elasticsearch.index.mapper.Mapper;
|
import org.elasticsearch.index.mapper.Mapper;
|
||||||
|
@ -40,7 +39,7 @@ import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpect
|
||||||
/**
|
/**
|
||||||
* A {@link FieldMapper} for indexing a dense vector of floats.
|
* 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 final String CONTENT_TYPE = "dense_vector";
|
||||||
public static short MAX_DIMS_COUNT = 2048; //maximum allowed number of dimensions
|
public static short MAX_DIMS_COUNT = 2048; //maximum allowed number of dimensions
|
||||||
|
@ -173,6 +172,11 @@ public class DenseVectorFieldMapper extends FieldMapper implements ArrayValueMap
|
||||||
return (DenseVectorFieldType) super.fieldType();
|
return (DenseVectorFieldType) super.fieldType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean parsesArrayValue() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(ParseContext context) throws IOException {
|
public void parse(ParseContext context) throws IOException {
|
||||||
if (context.externalValueSet()) {
|
if (context.externalValueSet()) {
|
||||||
|
|
Loading…
Reference in New Issue