19
19
20
20
package org .elasticsearch .action .admin .indices .mapping .get ;
21
21
22
+ import org .elasticsearch .Version ;
22
23
import org .elasticsearch .action .ActionResponse ;
23
24
import org .elasticsearch .common .ParseField ;
24
- import org .elasticsearch .common .bytes .BytesArray ;
25
25
import org .elasticsearch .common .bytes .BytesReference ;
26
26
import org .elasticsearch .common .io .stream .StreamInput ;
27
27
import org .elasticsearch .common .io .stream .StreamOutput ;
32
32
import org .elasticsearch .common .xcontent .XContentHelper ;
33
33
import org .elasticsearch .common .xcontent .XContentType ;
34
34
import org .elasticsearch .index .mapper .Mapper ;
35
+ import org .elasticsearch .index .mapper .MapperService ;
35
36
36
37
import java .io .IOException ;
37
38
import java .io .InputStream ;
@@ -51,38 +52,35 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
51
52
52
53
private static final ParseField MAPPINGS = new ParseField ("mappings" );
53
54
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 ;
56
56
57
- GetFieldMappingsResponse (Map <String , Map <String , Map < String , FieldMappingMetaData > >> mappings ) {
57
+ GetFieldMappingsResponse (Map <String , Map <String , FieldMappingMetaData >> mappings ) {
58
58
this .mappings = mappings ;
59
59
}
60
60
61
61
GetFieldMappingsResponse (StreamInput in ) throws IOException {
62
62
super (in );
63
63
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 );
65
65
for (int i = 0 ; i < size ; i ++) {
66
66
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 ()));
77
76
}
78
- indexMapBuilder .put (index , unmodifiableMap (typeMapBuilder ));
77
+ indexMapBuilder .put (index , unmodifiableMap (fieldMapBuilder ));
79
78
}
80
79
mappings = unmodifiableMap (indexMapBuilder );
81
-
82
80
}
83
81
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 () {
86
84
return mappings ;
87
85
}
88
86
@@ -92,32 +90,23 @@ public Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings() {
92
90
* @param field field name as specified in the {@link GetFieldMappingsRequest}
93
91
* @return FieldMappingMetaData for the requested field or null if not found.
94
92
*/
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 );
97
95
if (indexMapping == null ) {
98
96
return null ;
99
97
}
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 );
105
99
}
106
100
107
101
@ Override
108
102
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 ()) {
111
105
builder .startObject (indexEntry .getKey ());
112
106
builder .startObject (MAPPINGS .getPreferredName ());
113
107
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 ());
121
110
}
122
111
123
112
builder .endObject ();
@@ -138,7 +127,6 @@ private void addFieldMappingsToBuilder(XContentBuilder builder,
138
127
}
139
128
140
129
public static class FieldMappingMetaData implements ToXContentFragment {
141
- public static final FieldMappingMetaData NULL = new FieldMappingMetaData ("" , BytesArray .EMPTY );
142
130
143
131
private static final ParseField FULL_NAME = new ParseField ("full_name" );
144
132
private static final ParseField MAPPING = new ParseField ("mapping" );
@@ -165,10 +153,6 @@ public Map<String, Object> sourceAsMap() {
165
153
return XContentHelper .convertToMap (source , true , XContentType .JSON ).v2 ();
166
154
}
167
155
168
- public boolean isNull () {
169
- return NULL .fullName ().equals (fullName ) && NULL .source .length () == source .length ();
170
- }
171
-
172
156
//pkg-private for testing
173
157
BytesReference getSource () {
174
158
return source ;
@@ -210,18 +194,18 @@ public int hashCode() {
210
194
@ Override
211
195
public void writeTo (StreamOutput out ) throws IOException {
212
196
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 ()) {
214
198
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
+ }
215
203
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 );
225
209
}
226
210
}
227
211
}
0 commit comments