Skip to content

Commit 35355e2

Browse files
authored
Remove types from internal GetFieldMappings request/response objects (#48815)
Type filters and intermediate type levels in mappings responses have already been removed from the GetFieldMappings REST layer; we can also remove them from the internal Node client classes. Relates to #41059
1 parent b696c92 commit 35355e2

File tree

15 files changed

+147
-226
lines changed

15 files changed

+147
-226
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_field_mapping/20_missing_field.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"Return empty object if field doesn't exist, but type and index do":
2+
"Return empty object if field doesn't exist, but index does":
33

44
- do:
55
indices.create:
@@ -17,3 +17,4 @@
1717
fields: not_existent
1818

1919
- match: { 'test_index.mappings': {}}
20+

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsIndexRequest.java

+16-18
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,39 @@
1919

2020
package org.elasticsearch.action.admin.indices.mapping.get;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.ActionRequestValidationException;
2324
import org.elasticsearch.action.OriginalIndices;
2425
import org.elasticsearch.action.support.IndicesOptions;
2526
import org.elasticsearch.action.support.single.shard.SingleShardRequest;
27+
import org.elasticsearch.common.Strings;
2628
import org.elasticsearch.common.io.stream.StreamInput;
2729
import org.elasticsearch.common.io.stream.StreamOutput;
2830

2931
import java.io.IOException;
3032

3133
public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMappingsIndexRequest> {
3234

33-
private final boolean probablySingleFieldRequest;
3435
private final boolean includeDefaults;
3536
private final String[] fields;
36-
private final String[] types;
3737

38-
private OriginalIndices originalIndices;
38+
private final OriginalIndices originalIndices;
3939

4040
GetFieldMappingsIndexRequest(StreamInput in) throws IOException {
4141
super(in);
42-
types = in.readStringArray();
42+
if (in.getVersion().before(Version.V_8_0_0)) {
43+
in.readStringArray(); // former types array
44+
}
4345
fields = in.readStringArray();
4446
includeDefaults = in.readBoolean();
45-
probablySingleFieldRequest = in.readBoolean();
47+
if (in.getVersion().before(Version.V_8_0_0)) {
48+
in.readBoolean(); // former probablySingleField boolean
49+
}
4650
originalIndices = OriginalIndices.readOriginalIndices(in);
4751
}
4852

49-
GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index, boolean probablySingleFieldRequest) {
50-
this.probablySingleFieldRequest = probablySingleFieldRequest;
53+
GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index) {
5154
this.includeDefaults = other.includeDefaults();
52-
this.types = other.types();
5355
this.fields = other.fields();
5456
assert index != null;
5557
this.index(index);
@@ -61,18 +63,10 @@ public ActionRequestValidationException validate() {
6163
return null;
6264
}
6365

64-
public String[] types() {
65-
return types;
66-
}
67-
6866
public String[] fields() {
6967
return fields;
7068
}
7169

72-
public boolean probablySingleFieldRequest() {
73-
return probablySingleFieldRequest;
74-
}
75-
7670
public boolean includeDefaults() {
7771
return includeDefaults;
7872
}
@@ -90,10 +84,14 @@ public IndicesOptions indicesOptions() {
9084
@Override
9185
public void writeTo(StreamOutput out) throws IOException {
9286
super.writeTo(out);
93-
out.writeStringArray(types);
87+
if (out.getVersion().before(Version.V_8_0_0)) {
88+
out.writeStringArray(Strings.EMPTY_ARRAY);
89+
}
9490
out.writeStringArray(fields);
9591
out.writeBoolean(includeDefaults);
96-
out.writeBoolean(probablySingleFieldRequest);
92+
if (out.getVersion().before(Version.V_8_0_0)) {
93+
out.writeBoolean(false);
94+
}
9795
OriginalIndices.writeOriginalIndices(originalIndices, out);
9896
}
9997

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsRequest.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.mapping.get;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.ActionRequest;
2324
import org.elasticsearch.action.ActionRequestValidationException;
2425
import org.elasticsearch.action.IndicesRequest;
@@ -28,6 +29,7 @@
2829
import org.elasticsearch.common.io.stream.StreamOutput;
2930

3031
import java.io.IOException;
32+
import java.util.Arrays;
3133

3234
/**
3335
* Request the mappings of specific fields
@@ -44,7 +46,6 @@ public class GetFieldMappingsRequest extends ActionRequest implements IndicesReq
4446
private boolean includeDefaults = false;
4547

4648
private String[] indices = Strings.EMPTY_ARRAY;
47-
private String[] types = Strings.EMPTY_ARRAY;
4849

4950
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
5051

@@ -53,7 +54,12 @@ public GetFieldMappingsRequest() {}
5354
public GetFieldMappingsRequest(StreamInput in) throws IOException {
5455
super(in);
5556
indices = in.readStringArray();
56-
types = in.readStringArray();
57+
if (in.getVersion().before(Version.V_8_0_0)) {
58+
String[] types = in.readStringArray();
59+
if (types != Strings.EMPTY_ARRAY) {
60+
throw new IllegalArgumentException("Expected empty type array but received [" + Arrays.toString(types) + "]");
61+
}
62+
}
5763
indicesOptions = IndicesOptions.readIndicesOptions(in);
5864
local = in.readBoolean();
5965
fields = in.readStringArray();
@@ -79,11 +85,6 @@ public GetFieldMappingsRequest indices(String... indices) {
7985
return this;
8086
}
8187

82-
public GetFieldMappingsRequest types(String... types) {
83-
this.types = types;
84-
return this;
85-
}
86-
8788
public GetFieldMappingsRequest indicesOptions(IndicesOptions indicesOptions) {
8889
this.indicesOptions = indicesOptions;
8990
return this;
@@ -94,10 +95,6 @@ public String[] indices() {
9495
return indices;
9596
}
9697

97-
public String[] types() {
98-
return types;
99-
}
100-
10198
@Override
10299
public IndicesOptions indicesOptions() {
103100
return indicesOptions;
@@ -132,7 +129,9 @@ public ActionRequestValidationException validate() {
132129
public void writeTo(StreamOutput out) throws IOException {
133130
super.writeTo(out);
134131
out.writeStringArray(indices);
135-
out.writeStringArray(types);
132+
if (out.getVersion().before(Version.V_8_0_0)) {
133+
out.writeStringArray(Strings.EMPTY_ARRAY);
134+
}
136135
indicesOptions.writeIndicesOptions(out);
137136
out.writeBoolean(local);
138137
out.writeStringArray(fields);

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsRequestBuilder.java

-10
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ public GetFieldMappingsRequestBuilder addIndices(String... indices) {
4242
return this;
4343
}
4444

45-
public GetFieldMappingsRequestBuilder setTypes(String... types) {
46-
request.types(types);
47-
return this;
48-
}
49-
50-
public GetFieldMappingsRequestBuilder addTypes(String... types) {
51-
request.types(ArrayUtils.concat(request.types(), types));
52-
return this;
53-
}
54-
5545
public GetFieldMappingsRequestBuilder setIndicesOptions(IndicesOptions indicesOptions) {
5646
request.indicesOptions(indicesOptions);
5747
return this;

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java

+34-50
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
package org.elasticsearch.action.admin.indices.mapping.get;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.ActionResponse;
2324
import org.elasticsearch.common.ParseField;
24-
import org.elasticsearch.common.bytes.BytesArray;
2525
import org.elasticsearch.common.bytes.BytesReference;
2626
import org.elasticsearch.common.io.stream.StreamInput;
2727
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.common.xcontent.XContentHelper;
3333
import org.elasticsearch.common.xcontent.XContentType;
3434
import org.elasticsearch.index.mapper.Mapper;
35+
import org.elasticsearch.index.mapper.MapperService;
3536

3637
import java.io.IOException;
3738
import java.io.InputStream;
@@ -51,38 +52,35 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
5152

5253
private static final ParseField MAPPINGS = new ParseField("mappings");
5354

54-
// TODO remove the middle `type` level of this
55-
private final Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings;
55+
private final Map<String, Map<String, FieldMappingMetaData>> mappings;
5656

57-
GetFieldMappingsResponse(Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings) {
57+
GetFieldMappingsResponse(Map<String, Map<String, FieldMappingMetaData>> mappings) {
5858
this.mappings = mappings;
5959
}
6060

6161
GetFieldMappingsResponse(StreamInput in) throws IOException {
6262
super(in);
6363
int size = in.readVInt();
64-
Map<String, Map<String, Map<String, FieldMappingMetaData>>> indexMapBuilder = new HashMap<>(size);
64+
Map<String, Map<String, FieldMappingMetaData>> indexMapBuilder = new HashMap<>(size);
6565
for (int i = 0; i < size; i++) {
6666
String index = in.readString();
67-
int typesSize = in.readVInt();
68-
Map<String, Map<String, FieldMappingMetaData>> typeMapBuilder = new HashMap<>(typesSize);
69-
for (int j = 0; j < typesSize; j++) {
70-
String type = in.readString();
71-
int fieldSize = in.readVInt();
72-
Map<String, FieldMappingMetaData> fieldMapBuilder = new HashMap<>(fieldSize);
73-
for (int k = 0; k < fieldSize; k++) {
74-
fieldMapBuilder.put(in.readString(), new FieldMappingMetaData(in.readString(), in.readBytesReference()));
75-
}
76-
typeMapBuilder.put(type, unmodifiableMap(fieldMapBuilder));
67+
if (in.getVersion().before(Version.V_8_0_0)) {
68+
int typesSize = in.readVInt();
69+
assert typesSize == 1;
70+
in.readString(); // type
71+
}
72+
int fieldSize = in.readVInt();
73+
Map<String, FieldMappingMetaData> fieldMapBuilder = new HashMap<>(fieldSize);
74+
for (int k = 0; k < fieldSize; k++) {
75+
fieldMapBuilder.put(in.readString(), new FieldMappingMetaData(in.readString(), in.readBytesReference()));
7776
}
78-
indexMapBuilder.put(index, unmodifiableMap(typeMapBuilder));
77+
indexMapBuilder.put(index, unmodifiableMap(fieldMapBuilder));
7978
}
8079
mappings = unmodifiableMap(indexMapBuilder);
81-
8280
}
8381

84-
/** returns the retrieved field mapping. The return map keys are index, type, field (as specified in the request). */
85-
public Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings() {
82+
/** returns the retrieved field mapping. The return map keys are index, field (as specified in the request). */
83+
public Map<String, Map<String, FieldMappingMetaData>> mappings() {
8684
return mappings;
8785
}
8886

@@ -92,32 +90,23 @@ public Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings() {
9290
* @param field field name as specified in the {@link GetFieldMappingsRequest}
9391
* @return FieldMappingMetaData for the requested field or null if not found.
9492
*/
95-
public FieldMappingMetaData fieldMappings(String index, String type, String field) {
96-
Map<String, Map<String, FieldMappingMetaData>> indexMapping = mappings.get(index);
93+
public FieldMappingMetaData fieldMappings(String index, String field) {
94+
Map<String, FieldMappingMetaData> indexMapping = mappings.get(index);
9795
if (indexMapping == null) {
9896
return null;
9997
}
100-
Map<String, FieldMappingMetaData> typeMapping = indexMapping.get(type);
101-
if (typeMapping == null) {
102-
return null;
103-
}
104-
return typeMapping.get(field);
98+
return indexMapping.get(field);
10599
}
106100

107101
@Override
108102
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
109-
builder.startObject();
110-
for (Map.Entry<String, Map<String, Map<String, FieldMappingMetaData>>> indexEntry : mappings.entrySet()) {
103+
builder.startObject();
104+
for (Map.Entry<String, Map<String, FieldMappingMetaData>> indexEntry : mappings.entrySet()) {
111105
builder.startObject(indexEntry.getKey());
112106
builder.startObject(MAPPINGS.getPreferredName());
113107

114-
Map<String, FieldMappingMetaData> mappings = null;
115-
for (Map.Entry<String, Map<String, FieldMappingMetaData>> typeEntry : indexEntry.getValue().entrySet()) {
116-
assert mappings == null;
117-
mappings = typeEntry.getValue();
118-
}
119-
if (mappings != null) {
120-
addFieldMappingsToBuilder(builder, params, mappings);
108+
if (indexEntry.getValue() != null) {
109+
addFieldMappingsToBuilder(builder, params, indexEntry.getValue());
121110
}
122111

123112
builder.endObject();
@@ -138,7 +127,6 @@ private void addFieldMappingsToBuilder(XContentBuilder builder,
138127
}
139128

140129
public static class FieldMappingMetaData implements ToXContentFragment {
141-
public static final FieldMappingMetaData NULL = new FieldMappingMetaData("", BytesArray.EMPTY);
142130

143131
private static final ParseField FULL_NAME = new ParseField("full_name");
144132
private static final ParseField MAPPING = new ParseField("mapping");
@@ -165,10 +153,6 @@ public Map<String, Object> sourceAsMap() {
165153
return XContentHelper.convertToMap(source, true, XContentType.JSON).v2();
166154
}
167155

168-
public boolean isNull() {
169-
return NULL.fullName().equals(fullName) && NULL.source.length() == source.length();
170-
}
171-
172156
//pkg-private for testing
173157
BytesReference getSource() {
174158
return source;
@@ -210,18 +194,18 @@ public int hashCode() {
210194
@Override
211195
public void writeTo(StreamOutput out) throws IOException {
212196
out.writeVInt(mappings.size());
213-
for (Map.Entry<String, Map<String, Map<String, FieldMappingMetaData>>> indexEntry : mappings.entrySet()) {
197+
for (Map.Entry<String, Map<String, FieldMappingMetaData>> indexEntry : mappings.entrySet()) {
214198
out.writeString(indexEntry.getKey());
199+
if (out.getVersion().before(Version.V_8_0_0)) {
200+
out.writeVInt(1);
201+
out.writeString(MapperService.SINGLE_MAPPING_NAME);
202+
}
215203
out.writeVInt(indexEntry.getValue().size());
216-
for (Map.Entry<String, Map<String, FieldMappingMetaData>> typeEntry : indexEntry.getValue().entrySet()) {
217-
out.writeString(typeEntry.getKey());
218-
out.writeVInt(typeEntry.getValue().size());
219-
for (Map.Entry<String, FieldMappingMetaData> fieldEntry : typeEntry.getValue().entrySet()) {
220-
out.writeString(fieldEntry.getKey());
221-
FieldMappingMetaData fieldMapping = fieldEntry.getValue();
222-
out.writeString(fieldMapping.fullName());
223-
out.writeBytesReference(fieldMapping.source);
224-
}
204+
for (Map.Entry<String, FieldMappingMetaData> fieldEntry : indexEntry.getValue().entrySet()) {
205+
out.writeString(fieldEntry.getKey());
206+
FieldMappingMetaData fieldMapping = fieldEntry.getValue();
207+
out.writeString(fieldMapping.fullName());
208+
out.writeBytesReference(fieldMapping.source);
225209
}
226210
}
227211
}

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ public void writeTo(StreamOutput out) throws IOException {
9999
@Override
100100
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
101101
for (final ObjectObjectCursor<String, MappingMetaData> indexEntry : getMappings()) {
102+
builder.startObject(indexEntry.key);
102103
if (indexEntry.value != null) {
103-
builder.startObject(indexEntry.key);
104104
builder.field(MAPPINGS.getPreferredName(), indexEntry.value.sourceAsMap());
105-
builder.endObject();
106105
} else {
107106
builder.startObject(MAPPINGS.getPreferredName()).endObject();
108107
}
108+
builder.endObject();
109109
}
110110
return builder;
111111
}

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsAction.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ protected void doExecute(Task task, GetFieldMappingsRequest request, final Actio
6565
if (concreteIndices.length == 0) {
6666
listener.onResponse(new GetFieldMappingsResponse(emptyMap()));
6767
} else {
68-
boolean probablySingleFieldRequest = concreteIndices.length == 1 && request.types().length == 1 && request.fields().length == 1;
6968
for (final String index : concreteIndices) {
70-
GetFieldMappingsIndexRequest shardRequest = new GetFieldMappingsIndexRequest(request, index, probablySingleFieldRequest);
69+
GetFieldMappingsIndexRequest shardRequest = new GetFieldMappingsIndexRequest(request, index);
7170

7271
client.executeLocally(TransportGetFieldMappingsIndexAction.TYPE, shardRequest, new ActionListener<>() {
7372
@Override
@@ -92,7 +91,7 @@ public void onFailure(Exception e) {
9291
}
9392

9493
private GetFieldMappingsResponse merge(AtomicReferenceArray<Object> indexResponses) {
95-
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mergedResponses = new HashMap<>();
94+
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>> mergedResponses = new HashMap<>();
9695
for (int i = 0; i < indexResponses.length(); i++) {
9796
Object element = indexResponses.get(i);
9897
if (element instanceof GetFieldMappingsResponse) {

0 commit comments

Comments
 (0)