@@ -40,8 +40,8 @@ public class WatcherSearchTemplateRequest implements ToXContentObject {
40
40
private final SearchType searchType ;
41
41
private final IndicesOptions indicesOptions ;
42
42
private final Script template ;
43
-
44
43
private final BytesReference searchSource ;
44
+ private boolean restTotalHitsAsInt ;
45
45
46
46
public WatcherSearchTemplateRequest (String [] indices , String [] types , SearchType searchType , IndicesOptions indicesOptions ,
47
47
BytesReference searchSource ) {
@@ -72,6 +72,7 @@ public WatcherSearchTemplateRequest(WatcherSearchTemplateRequest original, Bytes
72
72
this .indicesOptions = original .indicesOptions ;
73
73
this .searchSource = source ;
74
74
this .template = original .template ;
75
+ this .restTotalHitsAsInt = original .restTotalHitsAsInt ;
75
76
}
76
77
77
78
private WatcherSearchTemplateRequest (String [] indices , String [] types , SearchType searchType , IndicesOptions indicesOptions ,
@@ -105,6 +106,19 @@ public IndicesOptions getIndicesOptions() {
105
106
return indicesOptions ;
106
107
}
107
108
109
+ public boolean isRestTotalHitsAsint () {
110
+ return restTotalHitsAsInt ;
111
+ }
112
+
113
+ /**
114
+ * Indicates whether the total hits in the response should be
115
+ * serialized as number (<code>true</code>) or as an object (<code>false</code>).
116
+ * Defaults to false.
117
+ */
118
+ public void setRestTotalHitsAsInt (boolean value ) {
119
+ this .restTotalHitsAsInt = restTotalHitsAsInt ;
120
+ }
121
+
108
122
public BytesReference getSearchSource () {
109
123
return searchSource ;
110
124
}
@@ -129,6 +143,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
129
143
if (types != null ) {
130
144
builder .array (TYPES_FIELD .getPreferredName (), types );
131
145
}
146
+ if (restTotalHitsAsInt ) {
147
+ builder .field (REST_TOTAL_HITS_AS_INT_FIELD .getPreferredName (), restTotalHitsAsInt );
148
+ }
132
149
if (searchSource != null && searchSource .length () > 0 ) {
133
150
try (InputStream stream = searchSource .streamInput ()) {
134
151
builder .rawField (BODY_FIELD .getPreferredName (), stream );
@@ -167,6 +184,7 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S
167
184
IndicesOptions indicesOptions = DEFAULT_INDICES_OPTIONS ;
168
185
BytesReference searchSource = null ;
169
186
Script template = null ;
187
+ boolean totalHitsAsInt = false ;
170
188
171
189
XContentParser .Token token ;
172
190
String currentFieldName = null ;
@@ -263,10 +281,19 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S
263
281
types .addAll (Arrays .asList (Strings .delimitedListToStringArray (typesStr , "," , " \t " )));
264
282
} else if (SEARCH_TYPE_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
265
283
searchType = SearchType .fromString (parser .text ().toLowerCase (Locale .ROOT ));
284
+ } else if (REST_TOTAL_HITS_AS_INT_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
285
+ totalHitsAsInt = parser .booleanValue ();
266
286
} else {
267
287
throw new ElasticsearchParseException ("could not read search request. unexpected string field [" +
268
288
currentFieldName + "]" );
269
289
}
290
+ } else if (token == XContentParser .Token .VALUE_BOOLEAN ) {
291
+ if (REST_TOTAL_HITS_AS_INT_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
292
+ totalHitsAsInt = parser .booleanValue ();
293
+ } else {
294
+ throw new ElasticsearchParseException ("could not read search request. unexpected boolean field [" +
295
+ currentFieldName + "]" );
296
+ }
270
297
} else {
271
298
throw new ElasticsearchParseException ("could not read search request. unexpected token [" + token + "]" );
272
299
}
@@ -276,8 +303,10 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S
276
303
searchSource = BytesArray .EMPTY ;
277
304
}
278
305
279
- return new WatcherSearchTemplateRequest (indices .toArray (new String [0 ]), types .toArray (new String [0 ]), searchType ,
280
- indicesOptions , searchSource , template );
306
+ WatcherSearchTemplateRequest request = new WatcherSearchTemplateRequest (indices .toArray (new String [0 ]),
307
+ types .toArray (new String [0 ]), searchType , indicesOptions , searchSource , template );
308
+ request .setRestTotalHitsAsInt (totalHitsAsInt );
309
+ return request ;
281
310
}
282
311
283
312
@ Override
@@ -291,13 +320,14 @@ public boolean equals(Object o) {
291
320
Objects .equals (searchType , other .searchType ) &&
292
321
Objects .equals (indicesOptions , other .indicesOptions ) &&
293
322
Objects .equals (searchSource , other .searchSource ) &&
294
- Objects .equals (template , other .template );
323
+ Objects .equals (template , other .template ) &&
324
+ Objects .equals (restTotalHitsAsInt , other .restTotalHitsAsInt );
295
325
296
326
}
297
327
298
328
@ Override
299
329
public int hashCode () {
300
- return Objects .hash (indices , types , searchType , indicesOptions , searchSource , template );
330
+ return Objects .hash (indices , types , searchType , indicesOptions , searchSource , template , restTotalHitsAsInt );
301
331
}
302
332
303
333
private static final ParseField INDICES_FIELD = new ParseField ("indices" );
@@ -309,6 +339,7 @@ public int hashCode() {
309
339
private static final ParseField IGNORE_UNAVAILABLE_FIELD = new ParseField ("ignore_unavailable" );
310
340
private static final ParseField ALLOW_NO_INDICES_FIELD = new ParseField ("allow_no_indices" );
311
341
private static final ParseField TEMPLATE_FIELD = new ParseField ("template" );
342
+ private static final ParseField REST_TOTAL_HITS_AS_INT_FIELD = new ParseField ("rest_total_hits_as_int" );
312
343
313
344
public static final IndicesOptions DEFAULT_INDICES_OPTIONS = IndicesOptions .lenientExpandOpen ();
314
345
}
0 commit comments