6
6
package org .elasticsearch .xpack .watcher .actions .index ;
7
7
8
8
import org .elasticsearch .ElasticsearchParseException ;
9
+ import org .elasticsearch .action .DocWriteRequest ;
9
10
import org .elasticsearch .action .support .WriteRequest .RefreshPolicy ;
10
11
import org .elasticsearch .common .Nullable ;
11
12
import org .elasticsearch .common .ParseField ;
13
+ import org .elasticsearch .common .collect .List ;
12
14
import org .elasticsearch .common .logging .DeprecationLogger ;
13
15
import org .elasticsearch .common .time .DateUtils ;
14
16
import org .elasticsearch .common .unit .TimeValue ;
@@ -31,6 +33,7 @@ public class IndexAction implements Action {
31
33
@ Nullable @ Deprecated final String docType ;
32
34
@ Nullable final String index ;
33
35
@ Nullable final String docId ;
36
+ @ Nullable final DocWriteRequest .OpType opType ;
34
37
@ Nullable final String executionTimeField ;
35
38
@ Nullable final TimeValue timeout ;
36
39
@ Nullable final ZoneId dynamicNameTimeZone ;
@@ -42,18 +45,20 @@ public class IndexAction implements Action {
42
45
public IndexAction (@ Nullable String index , @ Nullable String docId ,
43
46
@ Nullable String executionTimeField ,
44
47
@ 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 );
46
49
}
50
+
47
51
/**
48
52
* Document types are deprecated, use constructor without docType
49
53
*/
50
54
@ 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 ) {
54
58
this .index = index ;
55
59
this .docType = docType ;
56
60
this .docId = docId ;
61
+ this .opType = opType ;
57
62
this .executionTimeField = executionTimeField ;
58
63
this .timeout = timeout ;
59
64
this .dynamicNameTimeZone = dynamicNameTimeZone ;
@@ -77,6 +82,10 @@ public String getDocId() {
77
82
return docId ;
78
83
}
79
84
85
+ public DocWriteRequest .OpType getOpType () {
86
+ return opType ;
87
+ }
88
+
80
89
public String getExecutionTimeField () {
81
90
return executionTimeField ;
82
91
}
@@ -96,7 +105,10 @@ public boolean equals(Object o) {
96
105
97
106
IndexAction that = (IndexAction ) o ;
98
107
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 )
100
112
&& Objects .equals (executionTimeField , that .executionTimeField )
101
113
&& Objects .equals (timeout , that .timeout )
102
114
&& Objects .equals (dynamicNameTimeZone , that .dynamicNameTimeZone )
@@ -105,7 +117,7 @@ public boolean equals(Object o) {
105
117
106
118
@ Override
107
119
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 );
109
121
}
110
122
111
123
@ Override
@@ -120,6 +132,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
120
132
if (docId != null ) {
121
133
builder .field (Field .DOC_ID .getPreferredName (), docId );
122
134
}
135
+ if (opType != null ) {
136
+ builder .field (Field .OP_TYPE .getPreferredName (), opType );
137
+ }
123
138
if (executionTimeField != null ) {
124
139
builder .field (Field .EXECUTION_TIME_FIELD .getPreferredName (), executionTimeField );
125
140
}
@@ -139,6 +154,7 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
139
154
String index = null ;
140
155
String docType = null ;
141
156
String docId = null ;
157
+ DocWriteRequest .OpType opType = null ;
142
158
String executionTimeField = null ;
143
159
TimeValue timeout = null ;
144
160
ZoneId dynamicNameTimeZone = null ;
@@ -169,6 +185,17 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
169
185
docType = parser .text ();
170
186
} else if (Field .DOC_ID .match (currentFieldName , parser .getDeprecationHandler ())) {
171
187
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
+ }
172
199
} else if (Field .EXECUTION_TIME_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
173
200
executionTimeField = parser .text ();
174
201
} else if (Field .TIMEOUT_HUMAN .match (currentFieldName , parser .getDeprecationHandler ())) {
@@ -193,7 +220,7 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
193
220
}
194
221
}
195
222
196
- return new IndexAction (index , docType , docId , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
223
+ return new IndexAction (index , docType , docId , opType , executionTimeField , timeout , dynamicNameTimeZone , refreshPolicy );
197
224
}
198
225
199
226
/**
@@ -289,6 +316,7 @@ public static class Builder implements Action.Builder<IndexAction> {
289
316
final String index ;
290
317
final String docType ;
291
318
String docId ;
319
+ DocWriteRequest .OpType opType ;
292
320
String executionTimeField ;
293
321
TimeValue timeout ;
294
322
ZoneId dynamicNameTimeZone ;
@@ -313,6 +341,11 @@ public Builder setDocId(String docId) {
313
341
return this ;
314
342
}
315
343
344
+ public Builder setOpType (DocWriteRequest .OpType opType ) {
345
+ this .opType = opType ;
346
+ return this ;
347
+ }
348
+
316
349
public Builder setExecutionTimeField (String executionTimeField ) {
317
350
this .executionTimeField = executionTimeField ;
318
351
return this ;
@@ -335,14 +368,15 @@ public Builder setRefreshPolicy(RefreshPolicy refreshPolicy) {
335
368
336
369
@ Override
337
370
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 );
339
372
}
340
373
}
341
374
342
375
interface Field {
343
376
ParseField INDEX = new ParseField ("index" );
344
377
ParseField DOC_TYPE = new ParseField ("doc_type" );
345
378
ParseField DOC_ID = new ParseField ("doc_id" );
379
+ ParseField OP_TYPE = new ParseField ("op_type" );
346
380
ParseField EXECUTION_TIME_FIELD = new ParseField ("execution_time_field" );
347
381
ParseField SOURCE = new ParseField ("source" );
348
382
ParseField RESPONSE = new ParseField ("response" );
0 commit comments