25
25
import org .apache .http .nio .entity .NStringEntity ;
26
26
import org .elasticsearch .Version ;
27
27
import org .elasticsearch .action .ActionRequestValidationException ;
28
+ import org .elasticsearch .action .ActionRestRequest ;
28
29
import org .elasticsearch .action .DocumentRequest ;
29
30
import org .elasticsearch .action .WriteConsistencyLevel ;
30
31
import org .elasticsearch .action .index .IndexRequest ;
32
+ import org .elasticsearch .action .search .SearchRequest ;
31
33
import org .elasticsearch .action .support .replication .ReplicationType ;
32
34
import org .elasticsearch .action .support .single .instance .InstanceShardOperationRequest ;
33
35
import org .elasticsearch .common .Nullable ;
41
43
import org .elasticsearch .common .util .UriBuilder ;
42
44
import org .elasticsearch .common .xcontent .*;
43
45
import org .elasticsearch .index .VersionType ;
46
+ import org .elasticsearch .index .query .ScriptFilterParser ;
44
47
import org .elasticsearch .rest .RestRequest ;
45
48
import org .elasticsearch .script .ScriptService ;
46
49
47
50
import java .io .IOException ;
48
51
import java .nio .charset .StandardCharsets ;
49
52
import java .util .HashMap ;
53
+ import java .util .LinkedHashMap ;
50
54
import java .util .Locale ;
51
55
import java .util .Map ;
52
56
@@ -206,7 +210,9 @@ public String script() {
206
210
return this .script ;
207
211
}
208
212
209
- public ScriptService .ScriptType scriptType () { return this .scriptType ; }
213
+ public ScriptService .ScriptType scriptType () {
214
+ return this .scriptType ;
215
+ }
210
216
211
217
public Map <String , Object > scriptParams () {
212
218
return this .scriptParams ;
@@ -573,6 +579,7 @@ public UpdateRequest source(byte[] source, int offset, int length) throws Except
573
579
574
580
/**
575
581
* Should this update attempt to detect if it is a noop?
582
+ *
576
583
* @return this for chaining
577
584
*/
578
585
public UpdateRequest detectNoop (boolean detectNoop ) {
@@ -632,15 +639,15 @@ public boolean docAsUpsert() {
632
639
public void docAsUpsert (boolean shouldUpsertDoc ) {
633
640
this .docAsUpsert = shouldUpsertDoc ;
634
641
}
635
-
636
- public boolean scriptedUpsert (){
642
+
643
+ public boolean scriptedUpsert () {
637
644
return this .scriptedUpsert ;
638
645
}
639
-
646
+
640
647
public void scriptedUpsert (boolean scriptedUpsert ) {
641
648
this .scriptedUpsert = scriptedUpsert ;
642
649
}
643
-
650
+
644
651
645
652
@ Override
646
653
public void readFrom (StreamInput in ) throws IOException {
@@ -651,7 +658,7 @@ public void readFrom(StreamInput in) throws IOException {
651
658
id = in .readString ();
652
659
routing = in .readOptionalString ();
653
660
script = in .readOptionalString ();
654
- if (Strings .hasLength (script )) {
661
+ if (Strings .hasLength (script )) {
655
662
if (in .getVersion ().onOrAfter (Version .V_1_3_0 )) {
656
663
scriptType = ScriptService .ScriptType .readFrom (in );
657
664
} else {
@@ -745,6 +752,16 @@ public String getEndPoint() {
745
752
return UriBuilder .newBuilder ().slash (index (), type (), id (), "_update" ).build ();
746
753
}
747
754
755
+ @ Override
756
+ public ActionRestRequest getActionRestRequest (Version version ) {
757
+ ActionRestRequest actionRestRequest = super .getActionRestRequest (version );
758
+ if (version .id >= Version .V_5_0_0_ID ) {
759
+ return new UpdateRequestV5 ();
760
+ } else {
761
+ return actionRestRequest ;
762
+ }
763
+ }
764
+
748
765
@ Override
749
766
public RestRequest .Method getMethod () {
750
767
return RestRequest .Method .POST ;
@@ -783,8 +800,7 @@ private Map<String, Object> getPayload() {
783
800
if (this .detectNoop ) {
784
801
payload .put ("detect_noop" , Boolean .TRUE );
785
802
}
786
- }
787
- else if (Strings .hasLength (script )) {
803
+ } else if (Strings .hasLength (script )) {
788
804
payload .putIfNotNull ("lang" , this .scriptLang );
789
805
payload .putIf ("scripted_upsert" , Boolean .TRUE , this .scriptedUpsert );
790
806
payload .put ("script" , this .script );
@@ -799,4 +815,69 @@ else if (Strings.hasLength(script)) {
799
815
}
800
816
return payload .map ();
801
817
}
818
+
819
+ private class UpdateRequestV5 implements ActionRestRequest {
820
+
821
+ public RestRequest .Method getMethod () {
822
+ return UpdateRequest .this .getMethod ();
823
+ }
824
+
825
+ public String getEndPoint () {
826
+ return UpdateRequest .this .getEndPoint ();
827
+ }
828
+
829
+ @ Override
830
+ public Map <String , String > getParams () {
831
+ return UpdateRequest .this .getParams ();
832
+ }
833
+
834
+ @ Override
835
+ public HttpEntity getEntity () throws IOException {
836
+ Map <String , Object > payload = getPayload ();
837
+ String json = XContentHelper .convertToJson (payload , false );
838
+ return new NStringEntity (json , StandardCharsets .UTF_8 );
839
+
840
+ }
841
+
842
+ private Map <String , Object > getPayload () {
843
+ MapBuilder <String , Object > payload = MapBuilder .newMapBuilder ();
844
+ if (UpdateRequest .this .doc != null ) {
845
+ payload .put ("doc" , UpdateRequest .this .doc .sourceAsMap ());
846
+ if (UpdateRequest .this .docAsUpsert ) {
847
+ payload .put ("doc_as_upsert" , Boolean .TRUE );
848
+ }
849
+ if (UpdateRequest .this .detectNoop ) {
850
+ payload .put ("detect_noop" , Boolean .TRUE );
851
+ }
852
+ } else if (Strings .hasLength (script )) {
853
+ Map <String , Object > scriptObj = new LinkedHashMap <>();
854
+ scriptObj .put (UpdateRequest .this .scriptType .name ().toLowerCase (Locale .ROOT ), script );
855
+ scriptObj .put ("lang" , scriptLang );
856
+ scriptObj .put ("params" , scriptParams );
857
+ payload .put ("script" , scriptObj );
858
+ }
859
+ if (UpdateRequest .this .upsertRequest != null ) {
860
+ payload .put ("upsert" , UpdateRequest .this .upsertRequest .sourceAsMap ());
861
+ }
862
+
863
+ if (payload .isEmpty ()) {
864
+ throw new IllegalStateException ("Nothing to update. No doc, script or upsert provided" );
865
+ }
866
+ return payload .map ();
867
+ }
868
+
869
+ public HttpEntity getBulkEntity () throws IOException {
870
+ Map <String , Object > payload = Maps .newLinkedHashMap ();
871
+ Map <String , Object > actionMetadata = Maps .newLinkedHashMap ();
872
+ actionMetadata .put ("_index" , index );
873
+ actionMetadata .put ("_type" , type );
874
+ actionMetadata .put ("_id" , id );
875
+ payload .put (BULK_TYPE , actionMetadata );
876
+ String json = XContentHelper .convertToJson (payload , false );
877
+
878
+ String payloadJson = XContentHelper .convertToJson (getPayload (), false );
879
+ String fullPayload = Strings .join (json , "\n " , payloadJson , "\n " );
880
+ return new NStringEntity (fullPayload , StandardCharsets .UTF_8 );
881
+ }
882
+ }
802
883
}
0 commit comments