Skip to content

Render Mappings more Compact in GET /_cluster/state #83846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/83846.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 83846
summary: Render Mappings more Compact in GET /_cluster/state
area: Cluster Coordination
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -1763,18 +1763,26 @@ public static void toXContent(IndexMetadata indexMetadata, XContentBuilder build
}
builder.endArray();
} else {
builder.startObject(KEY_MAPPINGS);
MappingMetadata mmd = indexMetadata.mapping();
if (mmd != null) {
Map<String, Object> mapping = XContentHelper.convertToMap(mmd.source().uncompressed(), false).v2();
if (mapping.size() == 1 && mapping.containsKey(mmd.type())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(mmd.type());
final MappingMetadata mmd = indexMetadata.mapping();
if (params.paramAsBoolean(Metadata.MAPPINGS_BY_HASH_PARAM, true)) {
if (mmd == null) {
builder.nullField(KEY_MAPPINGS);
} else {
builder.field(KEY_MAPPINGS, indexMetadata.mapping().getSha256());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to pick a new name for the SHA256. mappings_sha256 maybe or perhaps shared_mappings_id. Then we can also allow dumping both, which could provide a transitional path for clients.

}
builder.field(mmd.type());
builder.map(mapping);
} else {
builder.startObject(KEY_MAPPINGS);
if (mmd != null) {
Map<String, Object> mapping = mmd.sourceAsMap();
if (mapping.size() == 1 && mapping.containsKey(mmd.type())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(mmd.type());
}
builder.field(mmd.type());
builder.map(mapping);
}
builder.endObject();
}
builder.endObject();
}

for (Map.Entry<String, DiffableStringMap> cursor : indexMetadata.customData.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -126,7 +127,7 @@ public CompressedXContent source() {
*/
@SuppressWarnings("unchecked")
public Map<String, Object> sourceAsMap() throws ElasticsearchParseException {
Map<String, Object> mapping = XContentHelper.convertToMap(source.compressedReference(), true).v2();
Map<String, Object> mapping = XContentHelper.convertToMap(source.compressedReference(), true, XContentType.JSON).v2();
if (mapping.size() == 1 && mapping.containsKey(type())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(type());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
public static final String ALL = "_all";
public static final String UNKNOWN_CLUSTER_UUID = "_na_";

public static final String MAPPINGS_BY_HASH_PARAM = "mappings_by_hash";

public enum XContentContext {
/* Custom metadata should be returns as part of API call */
API,
Expand Down Expand Up @@ -2124,6 +2126,15 @@ public static void toXContent(Metadata metadata, XContentBuilder builder, ToXCon
builder.endObject();

if (context == XContentContext.API) {
if (params.paramAsBoolean(MAPPINGS_BY_HASH_PARAM, true)) {
builder.startObject("mappings");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can call this shared_mappings and then have a flag of the same name to the API as well as a cluster setting to disable dumping non-shared mappings?

for (Map.Entry<String, MappingMetadata> mappingEntry : metadata.getMappingsByHash().entrySet()) {
builder.startObject(mappingEntry.getKey());
builder.mapContents(mappingEntry.getValue().sourceAsMap());
builder.endObject();
}
builder.endObject();
}
builder.startObject("indices");
for (IndexMetadata indexMetadata : metadata) {
IndexMetadata.Builder.toXContent(indexMetadata, builder, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.LongSupplier;

import static java.util.Collections.singletonMap;
import static org.elasticsearch.rest.RestRequest.Method.GET;

public class RestClusterStateAction extends BaseRestHandler {

public static final Map<String, String> TO_XCONTENT_PARAMS = Map.of(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API);
private final SettingsFilter settingsFilter;

private final ThreadPool threadPool;
Expand Down Expand Up @@ -116,10 +116,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
) {
@Override
protected ToXContent.Params getParams() {
return new ToXContent.DelegatingMapParams(
singletonMap(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API),
request
);
return new ToXContent.DelegatingMapParams(TO_XCONTENT_PARAMS, request);
}
}.map(response -> new RestClusterStateResponse(clusterStateRequest, response, threadPool::relativeTimeInMillis))
);
Expand All @@ -130,8 +127,9 @@ protected ToXContent.Params getParams() {
static {
final Set<String> responseParams = new HashSet<>();
responseParams.add("metric");
responseParams.add(Metadata.MAPPINGS_BY_HASH_PARAM);
responseParams.addAll(Settings.FORMAT_PARAMS);
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams);
RESPONSE_PARAMS = Set.copyOf(responseParams);
}

@Override
Expand Down