66package org .elasticsearch .xpack .watcher .actions .index ;
77
88import org .elasticsearch .ElasticsearchParseException ;
9+ import org .elasticsearch .action .DocWriteRequest ;
910import org .elasticsearch .action .support .WriteRequest .RefreshPolicy ;
1011import org .elasticsearch .common .Nullable ;
1112import org .elasticsearch .common .ParseField ;
13+ import org .elasticsearch .common .collect .List ;
1214import org .elasticsearch .common .logging .DeprecationLogger ;
1315import org .elasticsearch .common .time .DateUtils ;
1416import org .elasticsearch .common .unit .TimeValue ;
@@ -31,6 +33,7 @@ public class IndexAction implements Action {
3133 @ Nullable @ Deprecated final String docType ;
3234 @ Nullable final String index ;
3335 @ Nullable final String docId ;
36+ @ Nullable final DocWriteRequest .OpType opType ;
3437 @ Nullable final String executionTimeField ;
3538 @ Nullable final TimeValue timeout ;
3639 @ Nullable final ZoneId dynamicNameTimeZone ;
@@ -42,18 +45,20 @@ public class IndexAction implements Action {
4245 public IndexAction (@ Nullable String index , @ Nullable String docId ,
4346 @ Nullable String executionTimeField ,
4447 @ Nullable TimeValue timeout , @ Nullable ZoneId dynamicNameTimeZone , @ Nullable RefreshPolicy refreshPolicy ) {
45- this (index , null , docId , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
48+ this (index , null , docId , null , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
4649 }
50+
4751 /**
4852 * Document types are deprecated, use constructor without docType
4953 */
5054 @ Deprecated
51- public IndexAction (@ Nullable String index , @ Nullable String docType , @ Nullable String docId ,
52- @ Nullable String executionTimeField ,
53- @ Nullable TimeValue timeout , @ Nullable ZoneId dynamicNameTimeZone , @ Nullable RefreshPolicy refreshPolicy ) {
55+ public IndexAction (@ Nullable String index , @ Nullable String docType , @ Nullable String docId , @ Nullable DocWriteRequest . OpType opType ,
56+ @ Nullable String executionTimeField , @ Nullable TimeValue timeout , @ Nullable ZoneId dynamicNameTimeZone ,
57+ @ Nullable RefreshPolicy refreshPolicy ) {
5458 this .index = index ;
5559 this .docType = docType ;
5660 this .docId = docId ;
61+ this .opType = opType ;
5762 this .executionTimeField = executionTimeField ;
5863 this .timeout = timeout ;
5964 this .dynamicNameTimeZone = dynamicNameTimeZone ;
@@ -77,6 +82,10 @@ public String getDocId() {
7782 return docId ;
7883 }
7984
85+ public DocWriteRequest .OpType getOpType () {
86+ return opType ;
87+ }
88+
8089 public String getExecutionTimeField () {
8190 return executionTimeField ;
8291 }
@@ -96,7 +105,10 @@ public boolean equals(Object o) {
96105
97106 IndexAction that = (IndexAction ) o ;
98107
99- return Objects .equals (index , that .index ) && Objects .equals (docType , that .docType ) && Objects .equals (docId , that .docId )
108+ return Objects .equals (index , that .index )
109+ && Objects .equals (docType , that .docType )
110+ && Objects .equals (docId , that .docId )
111+ && Objects .equals (opType , that .opType )
100112 && Objects .equals (executionTimeField , that .executionTimeField )
101113 && Objects .equals (timeout , that .timeout )
102114 && Objects .equals (dynamicNameTimeZone , that .dynamicNameTimeZone )
@@ -105,7 +117,7 @@ public boolean equals(Object o) {
105117
106118 @ Override
107119 public int hashCode () {
108- return Objects .hash (index , docType , docId , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
120+ return Objects .hash (index , docType , docId , opType , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
109121 }
110122
111123 @ Override
@@ -120,6 +132,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
120132 if (docId != null ) {
121133 builder .field (Field .DOC_ID .getPreferredName (), docId );
122134 }
135+ if (opType != null ) {
136+ builder .field (Field .OP_TYPE .getPreferredName (), opType );
137+ }
123138 if (executionTimeField != null ) {
124139 builder .field (Field .EXECUTION_TIME_FIELD .getPreferredName (), executionTimeField );
125140 }
@@ -139,6 +154,7 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
139154 String index = null ;
140155 String docType = null ;
141156 String docId = null ;
157+ DocWriteRequest .OpType opType = null ;
142158 String executionTimeField = null ;
143159 TimeValue timeout = null ;
144160 ZoneId dynamicNameTimeZone = null ;
@@ -169,6 +185,17 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
169185 docType = parser .text ();
170186 } else if (Field .DOC_ID .match (currentFieldName , parser .getDeprecationHandler ())) {
171187 docId = parser .text ();
188+ } else if (Field .OP_TYPE .match (currentFieldName , parser .getDeprecationHandler ())) {
189+ try {
190+ opType = DocWriteRequest .OpType .fromString (parser .text ());
191+ if (List .of (DocWriteRequest .OpType .CREATE , DocWriteRequest .OpType .INDEX ).contains (opType ) == false ) {
192+ throw new ElasticsearchParseException ("could not parse [{}] action [{}/{}]. op_type value for field [{}] " +
193+ "must be [index] or [create]" , TYPE , watchId , actionId , currentFieldName );
194+ }
195+ } catch (IllegalArgumentException e ) {
196+ throw new ElasticsearchParseException ("could not parse [{}] action [{}/{}]. failed to parse op_type value for " +
197+ "field [{}]" , TYPE , watchId , actionId , currentFieldName );
198+ }
172199 } else if (Field .EXECUTION_TIME_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
173200 executionTimeField = parser .text ();
174201 } else if (Field .TIMEOUT_HUMAN .match (currentFieldName , parser .getDeprecationHandler ())) {
@@ -193,7 +220,7 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
193220 }
194221 }
195222
196- return new IndexAction (index , docType , docId , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
223+ return new IndexAction (index , docType , docId , opType , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
197224 }
198225
199226 /**
@@ -289,6 +316,7 @@ public static class Builder implements Action.Builder<IndexAction> {
289316 final String index ;
290317 final String docType ;
291318 String docId ;
319+ DocWriteRequest .OpType opType ;
292320 String executionTimeField ;
293321 TimeValue timeout ;
294322 ZoneId dynamicNameTimeZone ;
@@ -313,6 +341,11 @@ public Builder setDocId(String docId) {
313341 return this ;
314342 }
315343
344+ public Builder setOpType (DocWriteRequest .OpType opType ) {
345+ this .opType = opType ;
346+ return this ;
347+ }
348+
316349 public Builder setExecutionTimeField (String executionTimeField ) {
317350 this .executionTimeField = executionTimeField ;
318351 return this ;
@@ -335,14 +368,15 @@ public Builder setRefreshPolicy(RefreshPolicy refreshPolicy) {
335368
336369 @ Override
337370 public IndexAction build () {
338- return new IndexAction (index , docType , docId , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
371+ return new IndexAction (index , docType , docId , opType , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
339372 }
340373 }
341374
342375 interface Field {
343376 ParseField INDEX = new ParseField ("index" );
344377 ParseField DOC_TYPE = new ParseField ("doc_type" );
345378 ParseField DOC_ID = new ParseField ("doc_id" );
379+ ParseField OP_TYPE = new ParseField ("op_type" );
346380 ParseField EXECUTION_TIME_FIELD = new ParseField ("execution_time_field" );
347381 ParseField SOURCE = new ParseField ("source" );
348382 ParseField RESPONSE = new ParseField ("response" );
0 commit comments