38
38
import org .elasticsearch .common .xcontent .XContentFactory ;
39
39
import org .elasticsearch .common .xcontent .XContentHelper ;
40
40
import org .elasticsearch .common .xcontent .XContentParser ;
41
+ import org .elasticsearch .common .xcontent .json .JsonXContent ;
41
42
42
43
import java .io .IOException ;
44
+ import java .io .UncheckedIOException ;
43
45
import java .util .ArrayList ;
44
46
import java .util .Collections ;
45
47
import java .util .List ;
@@ -132,12 +134,15 @@ public Settings settings() {
132
134
return this .settings ;
133
135
}
134
136
135
- public ImmutableOpenMap <String , CompressedXContent > mappings () {
136
- return this .mappings ;
137
+ public CompressedXContent mappings () {
138
+ if (this .mappings .isEmpty ()) {
139
+ return null ;
140
+ }
141
+ return this .mappings .iterator ().next ().value ;
137
142
}
138
143
139
- public ImmutableOpenMap < String , CompressedXContent > getMappings () {
140
- return this .mappings ;
144
+ public CompressedXContent getMappings () {
145
+ return this .mappings () ;
141
146
}
142
147
143
148
public ImmutableOpenMap <String , AliasMetaData > aliases () {
@@ -219,6 +224,19 @@ public void writeTo(StreamOutput out) throws IOException {
219
224
out .writeOptionalVInt (version );
220
225
}
221
226
227
+ @ Override
228
+ public String toString () {
229
+ try {
230
+ XContentBuilder builder = JsonXContent .contentBuilder ();
231
+ builder .startObject ();
232
+ IndexTemplateMetaData .Builder .toXContentWithTypes (this , builder , ToXContent .EMPTY_PARAMS );
233
+ builder .endObject ();
234
+ return Strings .toString (builder );
235
+ } catch (IOException e ) {
236
+ throw new UncheckedIOException (e );
237
+ }
238
+ }
239
+
222
240
public static class Builder {
223
241
224
242
private static final Set <String > VALID_FIELDS = Sets .newHashSet (
@@ -251,7 +269,7 @@ public Builder(IndexTemplateMetaData indexTemplateMetaData) {
251
269
patterns (indexTemplateMetaData .patterns ());
252
270
settings (indexTemplateMetaData .settings ());
253
271
254
- mappings = ImmutableOpenMap .builder (indexTemplateMetaData .mappings () );
272
+ mappings = ImmutableOpenMap .builder (indexTemplateMetaData .mappings );
255
273
aliases = ImmutableOpenMap .builder (indexTemplateMetaData .aliases ());
256
274
}
257
275
@@ -371,41 +389,18 @@ private static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData,
371
389
indexTemplateMetaData .settings ().toXContent (builder , params );
372
390
builder .endObject ();
373
391
374
- if (params .paramAsBoolean ("reduce_mappings" , false )) {
375
- // The parameter include_type_name is only ever used in the REST API, where reduce_mappings is
376
- // always set to true. We therefore only check for include_type_name in this branch.
377
- if (includeTypeName == false ) {
378
- Map <String , Object > documentMapping = null ;
379
- for (ObjectObjectCursor <String , CompressedXContent > cursor : indexTemplateMetaData .mappings ()) {
380
- assert documentMapping == null ;
381
- byte [] mappingSource = cursor .value .uncompressed ();
382
- Map <String , Object > mapping = XContentHelper .convertToMap (new BytesArray (mappingSource ), true ).v2 ();
383
- documentMapping = reduceMapping (cursor .key , mapping );
384
- }
392
+ includeTypeName &= (params .paramAsBoolean ("reduce_mappings" , false ) == false );
385
393
386
- if (documentMapping != null ) {
387
- builder .field ("mappings" , documentMapping );
388
- } else {
389
- builder .startObject ("mappings" ).endObject ();
390
- }
391
- } else {
392
- builder .startObject ("mappings" );
393
- for (ObjectObjectCursor <String , CompressedXContent > cursor : indexTemplateMetaData .mappings ()) {
394
- byte [] mappingSource = cursor .value .uncompressed ();
395
- Map <String , Object > mapping = XContentHelper .convertToMap (new BytesArray (mappingSource ), true ).v2 ();
396
- mapping = reduceMapping (cursor .key , mapping );
397
- builder .field (cursor .key );
398
- builder .map (mapping );
399
- }
400
- builder .endObject ();
394
+ CompressedXContent m = indexTemplateMetaData .mappings ();
395
+ if (m != null ) {
396
+ Map <String , Object > documentMapping = XContentHelper .convertToMap (new BytesArray (m .uncompressed ()), true ).v2 ();
397
+ if (includeTypeName == false ) {
398
+ documentMapping = reduceMapping (documentMapping );
401
399
}
400
+ builder .field ("mappings" );
401
+ builder .map (documentMapping );
402
402
} else {
403
- builder .startArray ("mappings" );
404
- for (ObjectObjectCursor <String , CompressedXContent > cursor : indexTemplateMetaData .mappings ()) {
405
- byte [] data = cursor .value .uncompressed ();
406
- builder .map (XContentHelper .convertToMap (new BytesArray (data ), true ).v2 ());
407
- }
408
- builder .endArray ();
403
+ builder .startObject ("mappings" ).endObject ();
409
404
}
410
405
411
406
builder .startObject ("aliases" );
@@ -416,13 +411,9 @@ private static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData,
416
411
}
417
412
418
413
@ SuppressWarnings ("unchecked" )
419
- private static Map <String , Object > reduceMapping (String type , Map <String , Object > mapping ) {
420
- if (mapping .size () == 1 && mapping .containsKey (type )) {
421
- // the type name is the root value, reduce it
422
- return (Map <String , Object >) mapping .get (type );
423
- } else {
424
- return mapping ;
425
- }
414
+ private static Map <String , Object > reduceMapping (Map <String , Object > mapping ) {
415
+ assert mapping .keySet ().size () == 1 ;
416
+ return (Map <String , Object >) mapping .values ().iterator ().next ();
426
417
}
427
418
428
419
public static IndexTemplateMetaData fromXContent (XContentParser parser , String templateName ) throws IOException {
0 commit comments