diff --git a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesFetcher.java b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesFetcher.java index 7dc73940ce2ff..969d86f5f470c 100644 --- a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesFetcher.java +++ b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesFetcher.java @@ -178,7 +178,7 @@ static Map retrieveFieldCaps( false, false, null, - Collections.emptyMap() + Map.of() ); responseMap.put(parentField, fieldCap); } diff --git a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexResponse.java b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexResponse.java index e9e3a05169afc..06ea2dee17481 100644 --- a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexResponse.java @@ -203,14 +203,6 @@ public Map get() { return responseMap; } - /** - * - * Get the field capabilities for the provided {@code field} - */ - public IndexFieldCapabilities getField(String field) { - return responseMap.get(field); - } - TransportVersion getOriginVersion() { return originVersion; } diff --git a/server/src/main/java/org/elasticsearch/action/fieldcaps/IndexFieldCapabilities.java b/server/src/main/java/org/elasticsearch/action/fieldcaps/IndexFieldCapabilities.java index ef609a06cb8be..de2f6965e011d 100644 --- a/server/src/main/java/org/elasticsearch/action/fieldcaps/IndexFieldCapabilities.java +++ b/server/src/main/java/org/elasticsearch/action/fieldcaps/IndexFieldCapabilities.java @@ -63,7 +63,7 @@ public static IndexFieldCapabilities readFrom(StreamInput in) throws IOException isAggregatable, isDimension, metricType, - in.readMap(StreamInput::readString) + in.readImmutableMap(StreamInput::readString) ); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index a9d90f80c8a18..350ac22c5e216 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -36,7 +36,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.EnumSet; import java.util.HashMap; @@ -1108,7 +1107,7 @@ public static Parameter> metaParam() { return new Parameter<>( "meta", true, - Collections::emptyMap, + Map::of, (n, c, o) -> TypeParsers.parseMeta(n, o), m -> m.fieldType().meta(), XContentBuilder::stringStringMap, diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index d7fa0dae21b38..21ed56a82292c 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -79,7 +79,9 @@ public MappedFieldType( this.isStored = isStored; this.docValues = hasDocValues; this.textSearchInfo = Objects.requireNonNull(textSearchInfo); - this.meta = Objects.requireNonNull(meta); + // meta should be sorted but for the one item or empty case we can fall back to immutable maps to save some memory since order is + // irrelevant + this.meta = meta.size() <= 1 ? Map.copyOf(meta) : meta; } /** diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeParsers.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeParsers.java index c42c4df01c5fa..40c96b9976317 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeParsers.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TypeParsers.java @@ -38,6 +38,9 @@ public static Map parseMeta(String name, Object metaObject) { } @SuppressWarnings("unchecked") Map meta = (Map) metaObject; + if (meta.isEmpty()) { + return Map.of(); + } if (meta.size() > 5) { throw new MapperParsingException("[meta] can't have more than 5 entries, but got " + meta.size() + " on field [" + name + "]"); } @@ -69,6 +72,12 @@ public static Map parseMeta(String name, Object metaObject) { ); } } + var entrySet = meta.entrySet(); + if (entrySet.size() == 1) { + // no need to sort for a single entry + var entry = entrySet.iterator().next(); + return Map.of(entry.getKey(), (String) entry.getValue()); + } Map sortedMeta = new TreeMap<>(); for (Map.Entry entry : meta.entrySet()) { sortedMeta.put(entry.getKey(), (String) entry.getValue());