25
25
26
26
import co .elastic .clients .base .ElasticsearchError ;
27
27
import co .elastic .clients .base .Endpoint ;
28
+ import co .elastic .clients .base .SimpleEndpoint ;
29
+ import co .elastic .clients .elasticsearch ._core .bulk .Operation ;
28
30
import co .elastic .clients .elasticsearch ._types .RequestBase ;
29
- import co .elastic .clients .json .DelegatingDeserializer ;
30
31
import co .elastic .clients .json .JsonpDeserializable ;
31
32
import co .elastic .clients .json .JsonpDeserializer ;
32
33
import co .elastic .clients .json .JsonpMapper ;
33
34
import co .elastic .clients .json .JsonpSerializable ;
34
35
import co .elastic .clients .json .JsonpSerializer ;
36
+ import co .elastic .clients .json .JsonpUtils ;
37
+ import co .elastic .clients .json .NdJsonpSerializable ;
35
38
import co .elastic .clients .json .ObjectBuilderDeserializer ;
36
39
import co .elastic .clients .json .ObjectDeserializer ;
40
+ import co .elastic .clients .util .ModelTypeHelper ;
37
41
import co .elastic .clients .util .ObjectBuilder ;
38
42
import jakarta .json .JsonValue ;
39
43
import jakarta .json .stream .JsonGenerator ;
42
46
import java .util .ArrayList ;
43
47
import java .util .Arrays ;
44
48
import java .util .HashMap ;
49
+ import java .util .Iterator ;
45
50
import java .util .List ;
46
51
import java .util .Map ;
47
52
import java .util .Objects ;
48
- import java .util .function .Supplier ;
53
+ import java .util .function .Function ;
49
54
import java .util .stream .Collectors ;
50
55
import javax .annotation .Nullable ;
51
56
52
57
// typedef: _global.bulk.Request
53
58
54
- public final class BulkRequest <TSource > extends RequestBase implements JsonpSerializable {
59
+ public final class BulkRequest <TSource > extends RequestBase implements NdJsonpSerializable < Object >, JsonpSerializable {
55
60
@ Nullable
56
61
private final String index ;
57
62
@@ -85,7 +90,7 @@ public final class BulkRequest<TSource> extends RequestBase implements JsonpSeri
85
90
@ Nullable
86
91
private final Boolean requireAlias ;
87
92
88
- private final List <JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ > value ;
93
+ private final List <Object > operations ;
89
94
90
95
@ Nullable
91
96
private final JsonpSerializer <TSource > tSourceSerializer ;
@@ -100,16 +105,20 @@ public BulkRequest(Builder<TSource> builder) {
100
105
this .refresh = builder .refresh ;
101
106
this .routing = builder .routing ;
102
107
this .source = builder .source ;
103
- this .sourceExcludes = builder .sourceExcludes ;
104
- this .sourceIncludes = builder .sourceIncludes ;
108
+ this .sourceExcludes = ModelTypeHelper . unmodifiable ( builder .sourceExcludes ) ;
109
+ this .sourceIncludes = ModelTypeHelper . unmodifiable ( builder .sourceIncludes ) ;
105
110
this .timeout = builder .timeout ;
106
111
this .waitForActiveShards = builder .waitForActiveShards ;
107
112
this .requireAlias = builder .requireAlias ;
108
- this .value = Objects . requireNonNull (builder .value , "value " );
113
+ this .operations = ModelTypeHelper . unmodifiableNonNull (builder .operations , "_value_body " );
109
114
this .tSourceSerializer = builder .tSourceSerializer ;
110
115
111
116
}
112
117
118
+ public BulkRequest (Function <Builder <TSource >, Builder <TSource >> fn ) {
119
+ this (fn .apply (new Builder <>()));
120
+ }
121
+
113
122
/**
114
123
* Default index for items which don't provide one
115
124
* <p>
@@ -233,20 +242,24 @@ public Boolean requireAlias() {
233
242
/**
234
243
* Request body.
235
244
* <p>
236
- * API name: {@code value }
245
+ * API name: {@code _value_body }
237
246
*/
238
- public List <JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ > value () {
239
- return this .value ;
247
+ public List <Object > operations () {
248
+ return this .operations ;
249
+ }
250
+
251
+ @ Override
252
+ public Iterator <Object > iterator () {
253
+ return this .operations .iterator ();
240
254
}
241
255
242
256
/**
243
257
* Serialize this value to JSON.
244
258
*/
245
259
public void serialize (JsonGenerator generator , JsonpMapper mapper ) {
246
260
generator .writeStartArray ();
247
- for (JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ item0 : this .value ) {
248
- generator .write (item0 );
249
-
261
+ for (Object item0 : this .operations ) {
262
+ mapper .serialize (item0 , generator );
250
263
}
251
264
generator .writeEnd ();
252
265
@@ -291,7 +304,7 @@ public static class Builder<TSource> implements ObjectBuilder<BulkRequest<TSourc
291
304
@ Nullable
292
305
private Boolean requireAlias ;
293
306
294
- private List <JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ > value ;
307
+ private List <Object > operations ;
295
308
296
309
@ Nullable
297
310
private JsonpSerializer <TSource > tSourceSerializer ;
@@ -383,9 +396,9 @@ public Builder<TSource> sourceExcludes(String... value) {
383
396
}
384
397
385
398
/**
386
- * Add a value to {@link #sourceExcludes(List)}, creating the list if needed.
399
+ * Add a value to {@link #sourceExcludes(List)}, creating the list if needed. 4
387
400
*/
388
- public Builder <TSource > add_sourceExcludes (String value ) {
401
+ public Builder <TSource > addSourceExcludes (String value ) {
389
402
if (this .sourceExcludes == null ) {
390
403
this .sourceExcludes = new ArrayList <>();
391
404
}
@@ -416,9 +429,9 @@ public Builder<TSource> sourceIncludes(String... value) {
416
429
}
417
430
418
431
/**
419
- * Add a value to {@link #sourceIncludes(List)}, creating the list if needed.
432
+ * Add a value to {@link #sourceIncludes(List)}, creating the list if needed. 4
420
433
*/
421
- public Builder <TSource > add_sourceIncludes (String value ) {
434
+ public Builder <TSource > addSourceIncludes (String value ) {
422
435
if (this .sourceIncludes == null ) {
423
436
this .sourceIncludes = new ArrayList <>();
424
437
}
@@ -463,37 +476,52 @@ public Builder<TSource> requireAlias(@Nullable Boolean value) {
463
476
/**
464
477
* Request body.
465
478
* <p>
466
- * API name: {@code value }
479
+ * API name: {@code _value_body }
467
480
*/
468
- public Builder <TSource > value (
469
- List <JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ > value ) {
470
- this .value = value ;
481
+ public Builder <TSource > operations (List <Object > value ) {
482
+ this .operations = value ;
471
483
return this ;
472
484
}
473
485
474
486
/**
475
487
* Request body.
476
488
* <p>
477
- * API name: {@code value}
489
+ * API name: {@code _value_body}
490
+ */
491
+ public Builder <TSource > operations (Object ... value ) {
492
+ this .operations = Arrays .asList (value );
493
+ return this ;
494
+ }
495
+
496
+ /**
497
+ * Add an Operation to {@link #operations(List)}, creating the list if needed. 1
478
498
*/
479
- public Builder <TSource > value (
480
- JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ ... value ) {
481
- this .value = Arrays .asList (value );
499
+ public Builder <TSource > addOperation (Operation value ) {
500
+ if (this .operations == null ) {
501
+ this .operations = new ArrayList <>();
502
+ }
503
+ this .operations .add (value );
482
504
return this ;
483
505
}
484
506
485
507
/**
486
- * Add a value to {@link #value (List)}, creating the list if needed.
508
+ * Add a document to {@link #operations (List)}, creating the list if needed. 2
487
509
*/
488
- public Builder <TSource > addValue (
489
- JsonValue /* Union(_global.bulk.OperationContainer | _global.bulk.TSource) */ value ) {
490
- if (this .value == null ) {
491
- this .value = new ArrayList <>();
510
+ public Builder <TSource > addDocument (TSource value ) {
511
+ if (this .operations == null ) {
512
+ this .operations = new ArrayList <>();
492
513
}
493
- this .value .add (value );
514
+ this .operations .add (value );
494
515
return this ;
495
516
}
496
517
518
+ /**
519
+ * Add an Operation to {@link #operations(List)}, creating the list if needed. 3
520
+ */
521
+ public Builder <TSource > addOperation (Function <Operation .Builder , ObjectBuilder <Operation >> fn ) {
522
+ return this .addOperation (fn .apply (new Operation .Builder ()).build ());
523
+ }
524
+
497
525
/**
498
526
* Serializer for TSource. If not set, an attempt will be made to find a
499
527
* serializer from the JSON context.
@@ -517,28 +545,10 @@ public BulkRequest<TSource> build() {
517
545
518
546
// ---------------------------------------------------------------------------------------------
519
547
520
- /**
521
- * Create a json deserializer for BulkRequest
522
- */
523
- public static <TSource > JsonpDeserializer <BulkRequest <TSource >> createBulkRequestDeserializer (
524
- JsonpDeserializer <TSource > tSourceDeserializer ) {
525
- return ObjectBuilderDeserializer .createForValue ((Supplier <Builder <TSource >>) Builder ::new ,
526
- op -> BulkRequest .setupBulkRequestDeserializer (op , tSourceDeserializer ));
527
- };
528
-
529
- protected static <TSource > void setupBulkRequestDeserializer (
530
- DelegatingDeserializer <BulkRequest .Builder <TSource >> op , JsonpDeserializer <TSource > tSourceDeserializer ) {
531
-
532
- op .add (Builder ::value , JsonpDeserializer .arrayDeserializer (JsonpDeserializer .jsonValueDeserializer ()), "value" );
533
-
534
- }
535
-
536
- // ---------------------------------------------------------------------------------------------
537
-
538
548
/**
539
549
* Endpoint "{@code bulk}".
540
550
*/
541
- public static final Endpoint <BulkRequest <?>, BulkResponse , ElasticsearchError > ENDPOINT = new Endpoint . Simple <>(
551
+ public static final Endpoint <BulkRequest <?>, BulkResponse , ElasticsearchError > ENDPOINT = new SimpleEndpoint <>(
542
552
// Request method
543
553
request -> {
544
554
return "POST" ;
@@ -565,20 +575,20 @@ protected static <TSource> void setupBulkRequestDeserializer(
565
575
if (propsSet == (_index )) {
566
576
StringBuilder buf = new StringBuilder ();
567
577
buf .append ("/" );
568
- buf . append (request .index );
578
+ SimpleEndpoint . pathEncode (request .index , buf );
569
579
buf .append ("/_bulk" );
570
580
return buf .toString ();
571
581
}
572
582
if (propsSet == (_index | _type )) {
573
583
StringBuilder buf = new StringBuilder ();
574
584
buf .append ("/" );
575
- buf . append (request .index );
585
+ SimpleEndpoint . pathEncode (request .index , buf );
576
586
buf .append ("/" );
577
- buf . append (request .type );
587
+ SimpleEndpoint . pathEncode (request .type , buf );
578
588
buf .append ("/_bulk" );
579
589
return buf .toString ();
580
590
}
581
- throw Endpoint . Simple .noPathTemplateFound ("path" );
591
+ throw SimpleEndpoint .noPathTemplateFound ("path" );
582
592
583
593
},
584
594
@@ -589,13 +599,13 @@ protected static <TSource> void setupBulkRequestDeserializer(
589
599
params .put ("pipeline" , request .pipeline );
590
600
}
591
601
if (request .refresh != null ) {
592
- params .put ("refresh" , request . refresh . toString ());
602
+ params .put ("refresh" , JsonpUtils . toString (request . refresh ));
593
603
}
594
604
if (request .routing != null ) {
595
605
params .put ("routing" , request .routing );
596
606
}
597
607
if (request .source != null ) {
598
- params .put ("_source" , request . source . toString ());
608
+ params .put ("_source" , JsonpUtils . toString (request . source ));
599
609
}
600
610
if (request .sourceExcludes != null ) {
601
611
params .put ("_source_excludes" ,
@@ -609,12 +619,12 @@ protected static <TSource> void setupBulkRequestDeserializer(
609
619
params .put ("timeout" , request .timeout );
610
620
}
611
621
if (request .waitForActiveShards != null ) {
612
- params .put ("wait_for_active_shards" , request . waitForActiveShards . toString ());
622
+ params .put ("wait_for_active_shards" , JsonpUtils . toString (request . waitForActiveShards ));
613
623
}
614
624
if (request .requireAlias != null ) {
615
625
params .put ("require_alias" , String .valueOf (request .requireAlias ));
616
626
}
617
627
return params ;
618
628
619
- }, Endpoint . Simple .emptyMap (), true , BulkResponse ._DESERIALIZER );
629
+ }, SimpleEndpoint .emptyMap (), true , BulkResponse ._DESERIALIZER );
620
630
}
0 commit comments