Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ setup:
name: my-data-stream-1
- match: { data_streams.0.name: my-data-stream-1 }
- match: { data_streams.0.mappings: {} }
- length: { data_streams.0.effective_mappings._doc.properties: 2 }
- length: { data_streams.0.effective_mappings.properties: 2 }

- do:
indices.get_data_stream:
Expand All @@ -56,7 +56,7 @@ setup:
- match: { data_streams.0.name: my-data-stream-1 }
- match: { data_streams.0.applied_to_data_stream: true }
- match: { data_streams.0.mappings.properties.name.type: "keyword" }
- match: { data_streams.0.effective_mappings._doc.properties.name.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.name.type: "keyword" }

- do:
indices.rollover:
Expand All @@ -71,9 +71,9 @@ setup:
indices.get_data_stream_mappings:
name: my-data-stream-1
- match: { data_streams.0.name: my-data-stream-1 }
- length: { data_streams.0.effective_mappings._doc.properties: 3 }
- length: { data_streams.0.effective_mappings.properties: 3 }
- match: { data_streams.0.mappings.properties.name.type: "keyword" }
- match: { data_streams.0.effective_mappings._doc.properties.name.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.name.type: "keyword" }

- do:
indices.get_data_stream:
Expand Down Expand Up @@ -149,9 +149,9 @@ setup:
- match: { data_streams.0.mappings.properties.field1.type: "text" }
- match: { data_streams.0.mappings.properties.field2: null }
- match: { data_streams.0.mappings.properties.field3.type: "text" }
- match: { data_streams.0.effective_mappings._doc.properties.field1.type: "text" }
- match: { data_streams.0.effective_mappings._doc.properties.field2.type: "keyword" }
- match: { data_streams.0.effective_mappings._doc.properties.field3.type: "text" }
- match: { data_streams.0.effective_mappings.properties.field1.type: "text" }
- match: { data_streams.0.effective_mappings.properties.field2.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.field3.type: "text" }

- do:
cluster.put_component_template:
Expand Down Expand Up @@ -180,10 +180,10 @@ setup:
indices.get_data_stream_mappings:
name: my-component-only-data-stream-1
- match: { data_streams.0.name: my-component-only-data-stream-1 }
- length: { data_streams.0.effective_mappings._doc.properties: 5 }
- length: { data_streams.0.effective_mappings.properties: 5 }
- match: { data_streams.0.mappings.properties.field1.type: "text" }
- match: { data_streams.0.effective_mappings._doc.properties.field3.type: "text" }
- match: { data_streams.0.effective_mappings._doc.properties.field4.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.field3.type: "text" }
- match: { data_streams.0.effective_mappings.properties.field4.type: "keyword" }

- do:
indices.get_data_stream:
Expand Down Expand Up @@ -212,7 +212,7 @@ setup:
- match: { data_streams.0.name: my-component-only-data-stream-1 }
- match: { data_streams.0.applied_to_data_stream: true }
- match: { data_streams.0.mappings null }
- match: { data_streams.0.effective_mappings._doc.properties.field1.type: "keyword" }
- match: { data_streams.0.effective_mappings._doc.properties.field2.type: "keyword" }
- match: { data_streams.0.effective_mappings._doc.properties.field3: null }
- match: { data_streams.0.effective_mappings._doc.properties.field4.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.field1.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.field2.type: "keyword" }
- match: { data_streams.0.effective_mappings.properties.field3: null }
- match: { data_streams.0.effective_mappings.properties.field4.type: "keyword" }
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
Expand Down Expand Up @@ -120,6 +121,7 @@ public record DataStreamMappingsResponse(String dataStreamName, CompressedXConte
ToXContent {

@Override
@SuppressWarnings("unchecked")
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("name", dataStreamName);
Expand All @@ -131,6 +133,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
true,
XContentType.JSON
).v2();
// Strip the _doc wrapper if present
if (uncompressedEffectiveMappings.containsKey(MapperService.SINGLE_MAPPING_NAME)) {
uncompressedEffectiveMappings = (Map<String, Object>) uncompressedEffectiveMappings.get(MapperService.SINGLE_MAPPING_NAME);
}
builder.field("effective_mappings");
builder.map(uncompressedEffectiveMappings);
builder.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
Expand Down Expand Up @@ -196,6 +197,7 @@ public void writeTo(StreamOutput out) throws IOException {
}

@Override
@SuppressWarnings("unchecked")
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("name", dataStreamName);
Expand All @@ -213,6 +215,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
true,
XContentType.JSON
).v2();
// Strip the _doc wrapper if present
if (uncompressedEffectiveMappings.containsKey(MapperService.SINGLE_MAPPING_NAME)) {
uncompressedEffectiveMappings = (Map<String, Object>) uncompressedEffectiveMappings.get(MapperService.SINGLE_MAPPING_NAME);
}
if (uncompressedEffectiveMappings.isEmpty() == false) {
builder.field("effective_mappings");
builder.map(uncompressedEffectiveMappings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,61 @@ public void testResponseToXContent() throws IOException {
);
}
}

public void testResponseToXContentStripsDocWrapper() throws IOException {
Map<String, Object> dataStream1Mappings = Map.of(
"properties",
Map.of("field1", Map.of("type", "keyword"))
);
// Effective mappings with _doc wrapper (as it might come from MapperService)
Map<String, Object> dataStream1EffectiveMappingsWithDocWrapper = Map.of(
"_doc",
Map.of(
"properties",
Map.of("field1", Map.of("type", "keyword"), "field2", Map.of("type", "text"))
)
);
// Expected output without _doc wrapper
Map<String, Object> expectedEffectiveMappings = Map.of(
"properties",
Map.of("field1", Map.of("type", "keyword"), "field2", Map.of("type", "text"))
);
GetDataStreamMappingsAction.DataStreamMappingsResponse dataStreamMappingsResponse =
new GetDataStreamMappingsAction.DataStreamMappingsResponse(
"dataStream1",
new CompressedXContent(dataStream1Mappings),
new CompressedXContent(dataStream1EffectiveMappingsWithDocWrapper)
);
List<GetDataStreamMappingsAction.DataStreamMappingsResponse> responseList = List.of(dataStreamMappingsResponse);
GetDataStreamMappingsAction.Response response = new GetDataStreamMappingsAction.Response(responseList);
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
builder.humanReadable(true);
response.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
try {
xcontent.toXContent(builder, EMPTY_PARAMS);
} catch (IOException e) {
fail(e);
}
});
Map<String, Object> xContentMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
assertThat(
xContentMap,
equalTo(
Map.of(
"data_streams",
List.of(
Map.of(
"name",
"dataStream1",
"mappings",
dataStream1Mappings,
"effective_mappings",
expectedEffectiveMappings
)
)
)
)
);
}
}
}