Skip to content

Commit 8a7f3f7

Browse files
authored
Add support for rest_total_hits_as_int (#36051)
The support for rest_total_hits_as_int has already been merged to 6x in #35848 so this change adds this new option to master. The plan was to add this new option as part of #35848 but we've decided to wait a few days before merging this breaking change so this commit just handles the new option as a noop exactly like 6x for now. This will allow users to migrate to this parameter before #35848 is merged. Relates #33028
1 parent 8006068 commit 8a7f3f7

File tree

17 files changed

+279
-4
lines changed

17 files changed

+279
-4
lines changed

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.rest.action.search.RestSearchAction;
3232

3333
import java.io.IOException;
34+
import java.util.Arrays;
3435
import java.util.Collections;
36+
import java.util.HashSet;
3537
import java.util.Set;
3638

3739
import static org.elasticsearch.rest.RestRequest.Method.GET;
@@ -40,7 +42,15 @@
4042
public class RestMultiSearchTemplateAction extends BaseRestHandler {
4143
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
4244
LogManager.getLogger(RestMultiSearchAction.class));
43-
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TYPED_KEYS_PARAM);
45+
46+
private static final Set<String> RESPONSE_PARAMS;
47+
48+
static {
49+
final Set<String> responseParams = new HashSet<>(
50+
Arrays.asList(RestSearchAction.TYPED_KEYS_PARAM, RestSearchAction.TOTAL_HIT_AS_INT_PARAM)
51+
);
52+
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams);
53+
}
4454

4555
private final boolean allowExplicitIndex;
4656

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,24 @@
3030
import org.elasticsearch.rest.action.search.RestSearchAction;
3131

3232
import java.io.IOException;
33+
import java.util.Arrays;
3334
import java.util.Collections;
35+
import java.util.HashSet;
3436
import java.util.Set;
3537

3638
import static org.elasticsearch.rest.RestRequest.Method.GET;
3739
import static org.elasticsearch.rest.RestRequest.Method.POST;
3840

3941
public class RestSearchTemplateAction extends BaseRestHandler {
4042

41-
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TYPED_KEYS_PARAM);
43+
private static final Set<String> RESPONSE_PARAMS;
44+
45+
static {
46+
final Set<String> responseParams = new HashSet<>(
47+
Arrays.asList(RestSearchAction.TYPED_KEYS_PARAM, RestSearchAction.TOTAL_HIT_AS_INT_PARAM)
48+
);
49+
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams);
50+
}
4251

4352
public RestSearchTemplateAction(Settings settings, RestController controller) {
4453
super(settings);

modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/30_search_template.yml

+27
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,30 @@
124124
- match: { hits.total: 1 }
125125
- length: { hits.hits: 1 }
126126
- length: { profile: 1 }
127+
128+
---
129+
"Test with rest_total_hits_as_int":
130+
- skip:
131+
version: " - 6.5.99"
132+
reason: rest_total_hits_as_int was introduced in 6.6.0
133+
134+
- do:
135+
index:
136+
index: test
137+
type: type
138+
id: 1
139+
body: {}
140+
141+
- do:
142+
put_script:
143+
id: "template_1"
144+
body: { "script": { "lang": "mustache", "source": { "query": { "match_all": {} } } } }
145+
146+
- match: { acknowledged: true }
147+
148+
- do:
149+
search_template:
150+
rest_total_hits_as_int: true
151+
body: { "id": "template_1", "params": {} }
152+
153+
- match: { hits.total: 0 }

modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml

+44
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,47 @@ setup:
156156
- match: { responses.0.hits.total: 2 }
157157
- match: { responses.1.hits.total: 1 }
158158
- match: { responses.2.hits.total: 1 }
159+
160+
161+
- do:
162+
put_script:
163+
id: stored_template_1
164+
body: { "script": { "lang" : "mustache", "source": { "query": {"match": {"{{field}}": "{{value}}" }}}}}
165+
- match: { acknowledged: true }
166+
167+
---
168+
"Test with rest_total_hits_as_int":
169+
- skip:
170+
version: " - 6.5.99"
171+
reason: rest_total_hits_as_int was introduced in 6.6.0
172+
173+
- do:
174+
put_script:
175+
id: stored_template_1
176+
body: { "script": { "lang": "mustache", "source": { "query": {"match": {"{{field}}": "{{value}}" }}}}}
177+
- match: { acknowledged: true }
178+
179+
- do:
180+
msearch_template:
181+
rest_total_hits_as_int: true
182+
body:
183+
- index: index_*
184+
- id: stored_template_1
185+
params:
186+
field: "foo"
187+
value: "foo"
188+
- index: _all
189+
- id: stored_template_1
190+
params:
191+
field: "foo"
192+
value: "bar"
193+
- index: index_2
194+
- id: stored_template_1
195+
params:
196+
field: "foo"
197+
value: "foo"
198+
199+
- match: { responses.0.hits.total: 2 }
200+
- match: { responses.1.hits.total: 1 }
201+
- match: { responses.2.hits.total: 1 }
202+

rest-api-spec/src/main/resources/rest-api-spec/api/msearch.json

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
"type" : "number",
3939
"description" : "The number of concurrent shard requests each sub search executes concurrently. This value should be used to limit the impact of the search on the cluster in order to limit the number of concurrent shard requests",
4040
"default" : "The default grows with the number of nodes in the cluster but is at most 256."
41+
},
42+
"rest_total_hits_as_int" : {
43+
"type" : "boolean",
44+
"description" : "This parameter is ignored in this version. It is used in the next major version to control whether the rest response should render the total.hits as an object or a number",
45+
"default" : false
4146
}
4247
}
4348
},

rest-api-spec/src/main/resources/rest-api-spec/api/msearch_template.json

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
"max_concurrent_searches" : {
2929
"type" : "number",
3030
"description" : "Controls the maximum number of concurrent searches the multi search api will execute"
31+
},
32+
"rest_total_hits_as_int" : {
33+
"type" : "boolean",
34+
"description" : "This parameter is ignored in this version. It is used in the next major version to control whether the rest response should render the total.hits as an object or a number",
35+
"default" : false
3136
}
3237
}
3338
},

rest-api-spec/src/main/resources/rest-api-spec/api/scroll.json

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"scroll_id": {
2020
"type" : "string",
2121
"description" : "The scroll ID for scrolled search"
22+
},
23+
"rest_total_hits_as_int" : {
24+
"type" : "boolean",
25+
"description" : "This parameter is ignored in this version. It is used in the next major version to control whether the rest response should render the total.hits as an object or a number",
26+
"default" : false
2227
}
2328
}
2429
},

rest-api-spec/src/main/resources/rest-api-spec/api/search.json

+5
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@
182182
"type" : "number",
183183
"description" : "A threshold that enforces a pre-filter roundtrip to prefilter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold. This filter roundtrip can limit the number of shards significantly if for instance a shard can not match any documents based on it's rewrite method ie. if date filters are mandatory to match but the shard bounds and the query are disjoint.",
184184
"default" : 128
185+
},
186+
"rest_total_hits_as_int" : {
187+
"type" : "boolean",
188+
"description" : "This parameter is ignored in this version. It is used in the next major version to control whether the rest response should render the total.hits as an object or a number",
189+
"default" : false
185190
}
186191
}
187192
},

rest-api-spec/src/main/resources/rest-api-spec/api/search_template.json

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
"typed_keys": {
6363
"type" : "boolean",
6464
"description" : "Specify whether aggregation and suggester names should be prefixed by their respective types in the response"
65+
},
66+
"rest_total_hits_as_int" : {
67+
"type" : "boolean",
68+
"description" : "This parameter is ignored in this version. It is used in the next major version to control whether the rest response should render the total.hits as an object or a number",
69+
"default" : false
6570
}
6671
}
6772
},

rest-api-spec/src/main/resources/rest-api-spec/test/msearch/10_basic.yml

+25
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,28 @@ setup:
9393
- match: { responses.3.error.root_cause.0.reason: "/no.such.index/" }
9494
- match: { responses.3.error.root_cause.0.index: index_3 }
9595
- match: { responses.4.hits.total: 4 }
96+
97+
---
98+
"Search with rest_total_hits_as_int":
99+
- skip:
100+
version: " - 6.5.99"
101+
reason: rest_total_hits_as_int was introduced in 6.6.0
102+
103+
- do:
104+
msearch:
105+
rest_total_hits_as_int: true
106+
body:
107+
- index: index_*
108+
- query:
109+
match: {foo: foo}
110+
- index: index_2
111+
- query:
112+
match_all: {}
113+
- index: index_1
114+
- query:
115+
match: {foo: foo}
116+
117+
- match: { responses.0.hits.total: 2 }
118+
- match: { responses.1.hits.total: 1 }
119+
- match: { responses.2.hits.total: 1 }
120+

rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml

+64
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,67 @@
281281

282282
- length: {hits.hits: 1 }
283283
- match: { hits.max_score: null }
284+
285+
---
286+
"Scroll with rest_total_as_int":
287+
- skip:
288+
version: " - 6.5.99"
289+
reason: rest_total_hits_as_int was introduced in 6.6.0
290+
- do:
291+
indices.create:
292+
index: test_scroll
293+
- do:
294+
index:
295+
index: test_scroll
296+
type: test
297+
id: 42
298+
body: { foo: 1 }
299+
300+
- do:
301+
index:
302+
index: test_scroll
303+
type: test
304+
id: 43
305+
body: { foo: 2 }
306+
307+
- do:
308+
indices.refresh: {}
309+
310+
- do:
311+
search:
312+
index: test_scroll
313+
size: 1
314+
scroll: 1m
315+
sort: foo
316+
rest_total_hits_as_int: true
317+
body:
318+
query:
319+
match_all: {}
320+
321+
- set: {_scroll_id: scroll_id}
322+
- match: {hits.total: 2 }
323+
- length: {hits.hits: 1 }
324+
- match: {hits.hits.0._id: "42" }
325+
326+
- do:
327+
scroll:
328+
rest_total_hits_as_int: true
329+
body: { "scroll_id": "$scroll_id", "scroll": "1m"}
330+
331+
- match: {hits.total: 2 }
332+
- length: {hits.hits: 1 }
333+
- match: {hits.hits.0._id: "43" }
334+
335+
- do:
336+
scroll:
337+
rest_total_hits_as_int: true
338+
scroll_id: $scroll_id
339+
scroll: 1m
340+
341+
- match: {hits.total: 2 }
342+
- length: {hits.hits: 0 }
343+
344+
- do:
345+
clear_scroll:
346+
scroll_id: $scroll_id
347+

rest-api-spec/src/main/resources/rest-api-spec/test/search/20_default_values.yml

+10
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ setup:
7272
match:
7373
foo: bar
7474

75+
---
76+
"Search with rest_total_hits_as_int":
77+
- skip:
78+
version: " - 6.5.99"
79+
reason: rest_total_hits_as_int was introduced in 6.6.0
80+
- do:
81+
search:
82+
rest_total_hits_as_int: true
83+
84+
- match: {hits.total: 2}

server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.elasticsearch.search.builder.SearchSourceBuilder;
4141

4242
import java.io.IOException;
43+
import java.util.Arrays;
4344
import java.util.Collections;
45+
import java.util.HashSet;
4446
import java.util.List;
4547
import java.util.Set;
4648

@@ -53,7 +55,14 @@ public class RestMultiSearchAction extends BaseRestHandler {
5355
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
5456
" Specifying types in multi search requests is deprecated.";
5557

56-
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TYPED_KEYS_PARAM);
58+
private static final Set<String> RESPONSE_PARAMS;
59+
60+
static {
61+
final Set<String> responseParams = new HashSet<>(
62+
Arrays.asList(RestSearchAction.TYPED_KEYS_PARAM, RestSearchAction.TOTAL_HIT_AS_INT_PARAM)
63+
);
64+
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams);
65+
}
5766

5867
private final boolean allowExplicitIndex;
5968

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.io.IOException;
4646
import java.util.Arrays;
4747
import java.util.Collections;
48+
import java.util.HashSet;
4849
import java.util.Set;
4950
import java.util.function.IntConsumer;
5051

@@ -55,7 +56,13 @@
5556

5657
public class RestSearchAction extends BaseRestHandler {
5758
public static final String TYPED_KEYS_PARAM = "typed_keys";
58-
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(TYPED_KEYS_PARAM);
59+
public static final String TOTAL_HIT_AS_INT_PARAM = "rest_total_hits_as_int";
60+
private static final Set<String> RESPONSE_PARAMS;
61+
62+
static {
63+
final Set<String> responseParams = new HashSet<>(Arrays.asList(TYPED_KEYS_PARAM, TOTAL_HIT_AS_INT_PARAM));
64+
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams);
65+
}
5966

6067
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestSearchAction.class));
6168
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchScrollAction.java

+9
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
import org.elasticsearch.search.Scroll;
3030

3131
import java.io.IOException;
32+
import java.util.Collections;
33+
import java.util.Set;
3234

3335
import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;
3436
import static org.elasticsearch.rest.RestRequest.Method.GET;
3537
import static org.elasticsearch.rest.RestRequest.Method.POST;
3638

3739
public class RestSearchScrollAction extends BaseRestHandler {
40+
Set<String> RESPONSE_PARAMS = Collections.singleton(RestSearchAction.TOTAL_HIT_AS_INT_PARAM);
41+
3842
public RestSearchScrollAction(Settings settings, RestController controller) {
3943
super(settings);
4044

@@ -70,4 +74,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
7074
}});
7175
return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel));
7276
}
77+
78+
@Override
79+
protected Set<String> responseParams() {
80+
return RESPONSE_PARAMS;
81+
}
7382
}

0 commit comments

Comments
 (0)