2323import org .elasticsearch .ElasticsearchParseException ;
2424import org .elasticsearch .action .IndicesRequest ;
2525import org .elasticsearch .action .admin .indices .alias .Alias ;
26- import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequest ;
2726import org .elasticsearch .action .support .ActiveShardCount ;
2827import org .elasticsearch .action .support .IndicesOptions ;
2928import org .elasticsearch .client .TimedRequest ;
3433import org .elasticsearch .common .bytes .BytesReference ;
3534import org .elasticsearch .common .settings .Settings ;
3635import org .elasticsearch .common .xcontent .DeprecationHandler ;
37- import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
3836import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
3937import org .elasticsearch .common .xcontent .ToXContentObject ;
4038import org .elasticsearch .common .xcontent .XContentBuilder ;
4139import org .elasticsearch .common .xcontent .XContentFactory ;
4240import org .elasticsearch .common .xcontent .XContentHelper ;
4341import org .elasticsearch .common .xcontent .XContentParser ;
4442import org .elasticsearch .common .xcontent .XContentType ;
45- import org .elasticsearch .index .mapper .MapperService ;
4643
4744import java .io .IOException ;
4845import java .io .InputStream ;
49- import java .io .UncheckedIOException ;
50- import java .util .HashMap ;
5146import java .util .HashSet ;
5247import java .util .Map ;
5348import java .util .Objects ;
@@ -65,7 +60,10 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, Ind
6560
6661 private final String index ;
6762 private Settings settings = EMPTY_SETTINGS ;
68- private final Map <String , String > mappings = new HashMap <>();
63+
64+ private BytesReference mappings ;
65+ private XContentType mappingsXContentType ;
66+
6967 private final Set <Alias > aliases = new HashSet <>();
7068
7169 private ActiveShardCount waitForActiveShards = ActiveShardCount .DEFAULT ;
@@ -153,6 +151,8 @@ public CreateIndexRequest settings(Map<String, ?> source) {
153151 /**
154152 * Adds mapping that will be added when the index gets created.
155153 *
154+ * Note that the definition should *not* be nested under a type name.
155+ *
156156 * @param source The mapping source
157157 * @param xContentType The content type of the source
158158 */
@@ -163,22 +163,7 @@ public CreateIndexRequest mapping(String source, XContentType xContentType) {
163163 /**
164164 * Adds mapping that will be added when the index gets created.
165165 *
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.
182167 *
183168 * @param source The mapping source
184169 */
@@ -189,26 +174,43 @@ public CreateIndexRequest mapping(XContentBuilder source) {
189174 /**
190175 * Adds mapping that will be added when the index gets created.
191176 *
177+ * Note that the definition should *not* be nested under a type name.
178+ *
192179 * @param source The mapping source
193180 */
194181 public CreateIndexRequest mapping (Map <String , ?> source ) {
195182 try {
196183 XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON );
197184 builder .map (source );
198- return mapping (builder );
185+ return mapping (BytesReference . bytes ( builder ), builder . contentType () );
199186 } catch (IOException e ) {
200187 throw new ElasticsearchGenerationException ("Failed to generate [" + source + "]" , e );
201188 }
202189 }
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+
204206 /**
205207 * Sets the aliases that will be associated with the index when it gets created
206208 */
207209 public CreateIndexRequest aliases (Map <String , ?> source ) {
208210 try {
209211 XContentBuilder builder = XContentFactory .jsonBuilder ();
210212 builder .map (source );
211- return aliases (BytesReference .bytes (builder ));
213+ return aliases (BytesReference .bytes (builder ), builder . contentType () );
212214 } catch (IOException e ) {
213215 throw new ElasticsearchGenerationException ("Failed to generate [" + source + "]" , e );
214216 }
@@ -218,23 +220,23 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
218220 * Sets the aliases that will be associated with the index when it gets created
219221 */
220222 public CreateIndexRequest aliases (XContentBuilder source ) {
221- return aliases (BytesReference .bytes (source ));
223+ return aliases (BytesReference .bytes (source ), source . contentType () );
222224 }
223225
224226 /**
225227 * Sets the aliases that will be associated with the index when it gets created
226228 */
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 );
229231 }
230232
231233 /**
232234 * Sets the aliases that will be associated with the index when it gets created
233235 */
234- public CreateIndexRequest aliases (BytesReference source ) {
236+ public CreateIndexRequest aliases (BytesReference source , XContentType contentType ) {
235237 // 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 )) {
238240 //move to the first alias
239241 parser .nextToken ();
240242 while ((parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
@@ -256,43 +258,56 @@ public CreateIndexRequest alias(Alias alias) {
256258
257259 /**
258260 * Sets the settings and mappings as a single source.
261+ *
262+ * Note that the mapping definition should *not* be nested under a type name.
259263 */
260264 public CreateIndexRequest source (String source , XContentType xContentType ) {
261265 return source (new BytesArray (source ), xContentType );
262266 }
263267
264268 /**
265269 * Sets the settings and mappings as a single source.
270+ *
271+ * Note that the mapping definition should *not* be nested under a type name.
266272 */
267273 public CreateIndexRequest source (XContentBuilder source ) {
268274 return source (BytesReference .bytes (source ), source .contentType ());
269275 }
270276
271277 /**
272278 * Sets the settings and mappings as a single source.
279+ *
280+ * Note that the mapping definition should *not* be nested under a type name.
273281 */
274282 public CreateIndexRequest source (byte [] source , XContentType xContentType ) {
275283 return source (source , 0 , source .length , xContentType );
276284 }
277285
278286 /**
279287 * Sets the settings and mappings as a single source.
288+ *
289+ * Note that the mapping definition should *not* be nested under a type name.
280290 */
281291 public CreateIndexRequest source (byte [] source , int offset , int length , XContentType xContentType ) {
282292 return source (new BytesArray (source , offset , length ), xContentType );
283293 }
284294
285295 /**
286296 * Sets the settings and mappings as a single source.
297+ *
298+ * Note that the mapping definition should *not* be nested under a type name.
287299 */
288300 public CreateIndexRequest source (BytesReference source , XContentType xContentType ) {
289301 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 );
291304 return this ;
292305 }
293306
294307 /**
295308 * Sets the settings and mappings as a single source.
309+ *
310+ * Note that the mapping definition should *not* be nested under a type name.
296311 */
297312 @ SuppressWarnings ("unchecked" )
298313 public CreateIndexRequest source (Map <String , ?> source , DeprecationHandler deprecationHandler ) {
@@ -301,17 +316,20 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
301316 if (SETTINGS .match (name , deprecationHandler )) {
302317 settings ((Map <String , Object >) entry .getValue ());
303318 } 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 ());
306320 } else if (ALIASES .match (name , deprecationHandler )) {
307321 aliases ((Map <String , Object >) entry .getValue ());
308322 }
309323 }
310324 return this ;
311325 }
312326
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 ;
315333 }
316334
317335 public Set <Alias > aliases () {
@@ -358,10 +376,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
358376 settings .toXContent (builder , params );
359377 builder .endObject ();
360378
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 );
365382 }
366383 }
367384
0 commit comments