|
@@ -30,7 +30,6 @@ import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
-import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.util.CollectionUtils;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
@@ -43,7 +42,6 @@ import org.elasticsearch.index.query.QueryShardException;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
-import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
@@ -71,78 +69,36 @@ public class SourceFieldMapper extends MetadataFieldMapper {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public static class Builder extends MetadataFieldMapper.Builder<Builder> {
|
|
|
+ private static SourceFieldMapper toType(FieldMapper in) {
|
|
|
+ return (SourceFieldMapper) in;
|
|
|
+ }
|
|
|
|
|
|
- private boolean enabled = Defaults.ENABLED;
|
|
|
+ public static class Builder extends MetadataFieldMapper.Builder {
|
|
|
|
|
|
- private String[] includes = null;
|
|
|
- private String[] excludes = null;
|
|
|
+ private final Parameter<Boolean> enabled = Parameter.boolParam("enabled", false, m -> toType(m).enabled, Defaults.ENABLED);
|
|
|
+ private final Parameter<List<String>> includes
|
|
|
+ = Parameter.stringArrayParam("includes", false, m -> Arrays.asList(toType(m).includes), Collections.emptyList());
|
|
|
+ private final Parameter<List<String>> excludes
|
|
|
+ = Parameter.stringArrayParam("excludes", false, m -> Arrays.asList(toType(m).excludes), Collections.emptyList());
|
|
|
|
|
|
public Builder() {
|
|
|
- super(Defaults.NAME, new FieldType(Defaults.FIELD_TYPE));
|
|
|
- }
|
|
|
-
|
|
|
- public Builder enabled(boolean enabled) {
|
|
|
- this.enabled = enabled;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public Builder includes(String[] includes) {
|
|
|
- this.includes = includes;
|
|
|
- return this;
|
|
|
+ super(Defaults.NAME);
|
|
|
}
|
|
|
|
|
|
- public Builder excludes(String[] excludes) {
|
|
|
- this.excludes = excludes;
|
|
|
- return this;
|
|
|
+ @Override
|
|
|
+ protected List<Parameter<?>> getParameters() {
|
|
|
+ return List.of(enabled, includes, excludes);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SourceFieldMapper build(BuilderContext context) {
|
|
|
- return new SourceFieldMapper(enabled, includes, excludes);
|
|
|
+ return new SourceFieldMapper(enabled.getValue(),
|
|
|
+ includes.getValue().toArray(String[]::new),
|
|
|
+ excludes.getValue().toArray(String[]::new));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class TypeParser implements MetadataFieldMapper.TypeParser {
|
|
|
- @Override
|
|
|
- public MetadataFieldMapper.Builder<?> parse(String name, Map<String, Object> node,
|
|
|
- ParserContext parserContext) throws MapperParsingException {
|
|
|
- Builder builder = new Builder();
|
|
|
-
|
|
|
- for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
|
|
|
- Map.Entry<String, Object> entry = iterator.next();
|
|
|
- String fieldName = entry.getKey();
|
|
|
- Object fieldNode = entry.getValue();
|
|
|
- if (fieldName.equals("enabled")) {
|
|
|
- builder.enabled(XContentMapValues.nodeBooleanValue(fieldNode, name + ".enabled"));
|
|
|
- iterator.remove();
|
|
|
- } else if (fieldName.equals("includes")) {
|
|
|
- List<Object> values = (List<Object>) fieldNode;
|
|
|
- String[] includes = new String[values.size()];
|
|
|
- for (int i = 0; i < includes.length; i++) {
|
|
|
- includes[i] = values.get(i).toString();
|
|
|
- }
|
|
|
- builder.includes(includes);
|
|
|
- iterator.remove();
|
|
|
- } else if (fieldName.equals("excludes")) {
|
|
|
- List<Object> values = (List<Object>) fieldNode;
|
|
|
- String[] excludes = new String[values.size()];
|
|
|
- for (int i = 0; i < excludes.length; i++) {
|
|
|
- excludes[i] = values.get(i).toString();
|
|
|
- }
|
|
|
- builder.excludes(excludes);
|
|
|
- iterator.remove();
|
|
|
- }
|
|
|
- }
|
|
|
- return builder;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public MetadataFieldMapper getDefault(ParserContext context) {
|
|
|
- final Settings indexSettings = context.mapperService().getIndexSettings().getSettings();
|
|
|
- return new SourceFieldMapper();
|
|
|
- }
|
|
|
- }
|
|
|
+ public static final TypeParser PARSER = new ConfigurableTypeParser(c -> new SourceFieldMapper(), c -> new Builder());
|
|
|
|
|
|
static final class SourceFieldType extends MappedFieldType {
|
|
|
|
|
@@ -177,32 +133,23 @@ public class SourceFieldMapper extends MetadataFieldMapper {
|
|
|
private final String[] excludes;
|
|
|
|
|
|
private SourceFieldMapper() {
|
|
|
- this(Defaults.ENABLED, null, null);
|
|
|
+ this(Defaults.ENABLED, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY);
|
|
|
}
|
|
|
|
|
|
private SourceFieldMapper(boolean enabled, String[] includes, String[] excludes) {
|
|
|
- super(Defaults.FIELD_TYPE, SourceFieldType.INSTANCE); // Only stored.
|
|
|
+ super(SourceFieldType.INSTANCE); // Only stored.
|
|
|
this.enabled = enabled;
|
|
|
this.includes = includes;
|
|
|
this.excludes = excludes;
|
|
|
final boolean filtered = CollectionUtils.isEmpty(includes) == false || CollectionUtils.isEmpty(excludes) == false;
|
|
|
- this.filter = enabled && filtered && fieldType.stored() ? XContentMapValues.filter(includes, excludes) : null;
|
|
|
- this.complete = enabled && includes == null && excludes == null;
|
|
|
+ this.filter = enabled && filtered ? XContentMapValues.filter(includes, excludes) : null;
|
|
|
+ this.complete = enabled && CollectionUtils.isEmpty(includes) && CollectionUtils.isEmpty(excludes);
|
|
|
}
|
|
|
|
|
|
public boolean enabled() {
|
|
|
return enabled;
|
|
|
}
|
|
|
|
|
|
- public String[] excludes() {
|
|
|
- return this.excludes != null ? this.excludes : Strings.EMPTY_ARRAY;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public String[] includes() {
|
|
|
- return this.includes != null ? this.includes : Strings.EMPTY_ARRAY;
|
|
|
- }
|
|
|
-
|
|
|
public boolean isComplete() {
|
|
|
return complete;
|
|
|
}
|
|
@@ -238,7 +185,7 @@ public class SourceFieldMapper extends MetadataFieldMapper {
|
|
|
|
|
|
@Nullable
|
|
|
public BytesReference applyFilters(@Nullable BytesReference originalSource, @Nullable XContentType contentType) throws IOException {
|
|
|
- if (enabled && fieldType.stored() && originalSource != null) {
|
|
|
+ if (enabled && originalSource != null) {
|
|
|
// Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data
|
|
|
if (filter != null) {
|
|
|
// we don't update the context source if we filter, we want to keep it as is...
|
|
@@ -264,46 +211,7 @@ public class SourceFieldMapper extends MetadataFieldMapper {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
- boolean includeDefaults = params.paramAsBoolean("include_defaults", false);
|
|
|
-
|
|
|
- // all are defaults, no need to write it at all
|
|
|
- if (!includeDefaults && enabled == Defaults.ENABLED && includes == null && excludes == null) {
|
|
|
- return builder;
|
|
|
- }
|
|
|
- builder.startObject(contentType());
|
|
|
- if (includeDefaults || enabled != Defaults.ENABLED) {
|
|
|
- builder.field("enabled", enabled);
|
|
|
- }
|
|
|
-
|
|
|
- if (includes != null) {
|
|
|
- builder.array("includes", includes);
|
|
|
- } else if (includeDefaults) {
|
|
|
- builder.array("includes", Strings.EMPTY_ARRAY);
|
|
|
- }
|
|
|
-
|
|
|
- if (excludes != null) {
|
|
|
- builder.array("excludes", excludes);
|
|
|
- } else if (includeDefaults) {
|
|
|
- builder.array("excludes", Strings.EMPTY_ARRAY);
|
|
|
- }
|
|
|
-
|
|
|
- builder.endObject();
|
|
|
- return builder;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void mergeOptions(FieldMapper other, List<String> conflicts) {
|
|
|
- SourceFieldMapper sourceMergeWith = (SourceFieldMapper) other;
|
|
|
- if (this.enabled != sourceMergeWith.enabled) {
|
|
|
- conflicts.add("Cannot update enabled setting for [_source]");
|
|
|
- }
|
|
|
- if (Arrays.equals(includes(), sourceMergeWith.includes()) == false) {
|
|
|
- conflicts.add("Cannot update includes setting for [_source]");
|
|
|
- }
|
|
|
- if (Arrays.equals(excludes(), sourceMergeWith.excludes()) == false) {
|
|
|
- conflicts.add("Cannot update excludes setting for [_source]");
|
|
|
- }
|
|
|
+ public ParametrizedFieldMapper.Builder getMergeBuilder() {
|
|
|
+ return new Builder().init(this);
|
|
|
}
|
|
|
-
|
|
|
}
|