Skip to content

Commit eb3e82e

Browse files
committed
Add rest_total_hits_as_int in the search APIs
This change adds the support for a search query parameter named `rest_total_hits_as_int`. This parameter will be used in the next major version (7.0) to restore the total hits as a number in the response. This param is added in this version (6.x) to handle mixed cluster queries where nodes are in multiple versions (7.0 and 6.latest). The param does not change anything in 6x since total hits is always a number in 6x so it is just ignored.
1 parent c011a1f commit eb3e82e

File tree

17 files changed

+277
-4
lines changed

17 files changed

+277
-4
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,24 @@
2929
import org.elasticsearch.rest.action.search.RestSearchAction;
3030

3131
import java.io.IOException;
32+
import java.util.Arrays;
3233
import java.util.Collections;
34+
import java.util.HashSet;
3335
import java.util.Set;
3436

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

3840
public class RestMultiSearchTemplateAction extends BaseRestHandler {
3941

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

4251
private final boolean allowExplicitIndex;
4352

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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 27 additions & 0 deletions
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

Lines changed: 44 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

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

0 commit comments

Comments
 (0)