23
23
import org .elasticsearch .ElasticsearchParseException ;
24
24
import org .elasticsearch .action .IndicesRequest ;
25
25
import org .elasticsearch .action .admin .indices .alias .Alias ;
26
- import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequest ;
27
26
import org .elasticsearch .action .support .ActiveShardCount ;
28
27
import org .elasticsearch .action .support .IndicesOptions ;
29
28
import org .elasticsearch .client .TimedRequest ;
34
33
import org .elasticsearch .common .bytes .BytesReference ;
35
34
import org .elasticsearch .common .settings .Settings ;
36
35
import org .elasticsearch .common .xcontent .DeprecationHandler ;
37
- import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
38
36
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
39
37
import org .elasticsearch .common .xcontent .ToXContentObject ;
40
38
import org .elasticsearch .common .xcontent .XContentBuilder ;
41
39
import org .elasticsearch .common .xcontent .XContentFactory ;
42
40
import org .elasticsearch .common .xcontent .XContentHelper ;
43
41
import org .elasticsearch .common .xcontent .XContentParser ;
44
42
import org .elasticsearch .common .xcontent .XContentType ;
45
- import org .elasticsearch .index .mapper .MapperService ;
46
43
47
44
import java .io .IOException ;
48
45
import java .io .InputStream ;
49
- import java .io .UncheckedIOException ;
50
- import java .util .HashMap ;
51
46
import java .util .HashSet ;
52
47
import java .util .Map ;
53
48
import java .util .Objects ;
@@ -65,7 +60,10 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, Ind
65
60
66
61
private final String index ;
67
62
private Settings settings = EMPTY_SETTINGS ;
68
- private final Map <String , String > mappings = new HashMap <>();
63
+
64
+ private BytesReference mappings ;
65
+ private XContentType mappingsXContentType ;
66
+
69
67
private final Set <Alias > aliases = new HashSet <>();
70
68
71
69
private ActiveShardCount waitForActiveShards = ActiveShardCount .DEFAULT ;
@@ -153,6 +151,8 @@ public CreateIndexRequest settings(Map<String, ?> source) {
153
151
/**
154
152
* Adds mapping that will be added when the index gets created.
155
153
*
154
+ * Note that the definition should *not* be nested under a type name.
155
+ *
156
156
* @param source The mapping source
157
157
* @param xContentType The content type of the source
158
158
*/
@@ -163,22 +163,7 @@ public CreateIndexRequest mapping(String source, XContentType xContentType) {
163
163
/**
164
164
* Adds mapping that will be added when the index gets created.
165
165
*
166
- * @param source The mapping source
167
- * @param xContentType the content type of the mapping source
168
- */
169
- private CreateIndexRequest mapping (BytesReference source , XContentType xContentType ) {
170
- Objects .requireNonNull (xContentType );
171
- try {
172
- mappings .put (MapperService .SINGLE_MAPPING_NAME ,
173
- XContentHelper .convertToJson (source , false , false , xContentType ));
174
- return this ;
175
- } catch (IOException e ) {
176
- throw new UncheckedIOException ("failed to convert to json" , e );
177
- }
178
- }
179
-
180
- /**
181
- * Adds mapping that will be added when the index gets created.
166
+ * Note that the definition should *not* be nested under a type name.
182
167
*
183
168
* @param source The mapping source
184
169
*/
@@ -189,26 +174,43 @@ public CreateIndexRequest mapping(XContentBuilder source) {
189
174
/**
190
175
* Adds mapping that will be added when the index gets created.
191
176
*
177
+ * Note that the definition should *not* be nested under a type name.
178
+ *
192
179
* @param source The mapping source
193
180
*/
194
181
public CreateIndexRequest mapping (Map <String , ?> source ) {
195
182
try {
196
183
XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
197
184
builder .map (source );
198
- return mapping (builder );
185
+ return mapping (BytesReference . bytes ( builder ), builder . contentType () );
199
186
} catch (IOException e ) {
200
187
throw new ElasticsearchGenerationException ("Failed to generate [" + source + "]" , e );
201
188
}
202
189
}
203
-
190
+
191
+ /**
192
+ * Adds mapping that will be added when the index gets created.
193
+ *
194
+ * Note that the definition should *not* be nested under a type name.
195
+ *
196
+ * @param source The mapping source
197
+ * @param xContentType the content type of the mapping source
198
+ */
199
+ private CreateIndexRequest mapping (BytesReference source , XContentType xContentType ) {
200
+ Objects .requireNonNull (xContentType );
201
+ mappings = source ;
202
+ mappingsXContentType = xContentType ;
203
+ return this ;
204
+ }
205
+
204
206
/**
205
207
* Sets the aliases that will be associated with the index when it gets created
206
208
*/
207
209
public CreateIndexRequest aliases (Map <String , ?> source ) {
208
210
try {
209
211
XContentBuilder builder = XContentFactory .jsonBuilder ();
210
212
builder .map (source );
211
- return aliases (BytesReference .bytes (builder ));
213
+ return aliases (BytesReference .bytes (builder ), builder . contentType () );
212
214
} catch (IOException e ) {
213
215
throw new ElasticsearchGenerationException ("Failed to generate [" + source + "]" , e );
214
216
}
@@ -218,23 +220,23 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
218
220
* Sets the aliases that will be associated with the index when it gets created
219
221
*/
220
222
public CreateIndexRequest aliases (XContentBuilder source ) {
221
- return aliases (BytesReference .bytes (source ));
223
+ return aliases (BytesReference .bytes (source ), source . contentType () );
222
224
}
223
225
224
226
/**
225
227
* Sets the aliases that will be associated with the index when it gets created
226
228
*/
227
- public CreateIndexRequest aliases (String source ) {
228
- return aliases (new BytesArray (source ));
229
+ public CreateIndexRequest aliases (String source , XContentType contentType ) {
230
+ return aliases (new BytesArray (source ), contentType );
229
231
}
230
232
231
233
/**
232
234
* Sets the aliases that will be associated with the index when it gets created
233
235
*/
234
- public CreateIndexRequest aliases (BytesReference source ) {
236
+ public CreateIndexRequest aliases (BytesReference source , XContentType contentType ) {
235
237
// EMPTY is safe here because we never call namedObject
236
- try (XContentParser parser = XContentHelper
237
- . createParser ( NamedXContentRegistry . EMPTY , LoggingDeprecationHandler . INSTANCE , source )) {
238
+ try (XContentParser parser = XContentHelper . createParser ( NamedXContentRegistry . EMPTY ,
239
+ DeprecationHandler . THROW_UNSUPPORTED_OPERATION , source , contentType )) {
238
240
//move to the first alias
239
241
parser .nextToken ();
240
242
while ((parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
@@ -256,43 +258,56 @@ public CreateIndexRequest alias(Alias alias) {
256
258
257
259
/**
258
260
* Sets the settings and mappings as a single source.
261
+ *
262
+ * Note that the mapping definition should *not* be nested under a type name.
259
263
*/
260
264
public CreateIndexRequest source (String source , XContentType xContentType ) {
261
265
return source (new BytesArray (source ), xContentType );
262
266
}
263
267
264
268
/**
265
269
* Sets the settings and mappings as a single source.
270
+ *
271
+ * Note that the mapping definition should *not* be nested under a type name.
266
272
*/
267
273
public CreateIndexRequest source (XContentBuilder source ) {
268
274
return source (BytesReference .bytes (source ), source .contentType ());
269
275
}
270
276
271
277
/**
272
278
* Sets the settings and mappings as a single source.
279
+ *
280
+ * Note that the mapping definition should *not* be nested under a type name.
273
281
*/
274
282
public CreateIndexRequest source (byte [] source , XContentType xContentType ) {
275
283
return source (source , 0 , source .length , xContentType );
276
284
}
277
285
278
286
/**
279
287
* Sets the settings and mappings as a single source.
288
+ *
289
+ * Note that the mapping definition should *not* be nested under a type name.
280
290
*/
281
291
public CreateIndexRequest source (byte [] source , int offset , int length , XContentType xContentType ) {
282
292
return source (new BytesArray (source , offset , length ), xContentType );
283
293
}
284
294
285
295
/**
286
296
* Sets the settings and mappings as a single source.
297
+ *
298
+ * Note that the mapping definition should *not* be nested under a type name.
287
299
*/
288
300
public CreateIndexRequest source (BytesReference source , XContentType xContentType ) {
289
301
Objects .requireNonNull (xContentType );
290
- source (XContentHelper .convertToMap (source , false , xContentType ).v2 (), LoggingDeprecationHandler .INSTANCE );
302
+ source (XContentHelper .convertToMap (source , false , xContentType ).v2 (),
303
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION );
291
304
return this ;
292
305
}
293
306
294
307
/**
295
308
* Sets the settings and mappings as a single source.
309
+ *
310
+ * Note that the mapping definition should *not* be nested under a type name.
296
311
*/
297
312
@ SuppressWarnings ("unchecked" )
298
313
public CreateIndexRequest source (Map <String , ?> source , DeprecationHandler deprecationHandler ) {
@@ -301,17 +316,20 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
301
316
if (SETTINGS .match (name , deprecationHandler )) {
302
317
settings ((Map <String , Object >) entry .getValue ());
303
318
} else if (MAPPINGS .match (name , deprecationHandler )) {
304
- Map <String , Object > mappings = (Map <String , Object >) entry .getValue ();
305
- mapping (mappings );
319
+ mapping ((Map <String , Object >) entry .getValue ());
306
320
} else if (ALIASES .match (name , deprecationHandler )) {
307
321
aliases ((Map <String , Object >) entry .getValue ());
308
322
}
309
323
}
310
324
return this ;
311
325
}
312
326
313
- public Map <String , String > mappings () {
314
- return this .mappings ;
327
+ public BytesReference mappings () {
328
+ return mappings ;
329
+ }
330
+
331
+ public XContentType mappingsXContentType () {
332
+ return mappingsXContentType ;
315
333
}
316
334
317
335
public Set <Alias > aliases () {
@@ -358,10 +376,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
358
376
settings .toXContent (builder , params );
359
377
builder .endObject ();
360
378
361
- if (!mappings .isEmpty ()) {
362
- String mapping = mappings .get (MapperService .SINGLE_MAPPING_NAME );
363
- try (InputStream stream = new BytesArray (mapping ).streamInput ()) {
364
- builder .rawField (MAPPINGS .getPreferredName (), stream , XContentType .JSON );
379
+ if (mappings != null ) {
380
+ try (InputStream stream = mappings .streamInput ()) {
381
+ builder .rawField (MAPPINGS .getPreferredName (), stream , mappingsXContentType );
365
382
}
366
383
}
367
384
0 commit comments