Skip to content

Commit 89e5e89

Browse files
committed
Add stats params for Count API (#67528)
1 parent 8893871 commit 89e5e89

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
"terminate_after":{
104104
"type":"number",
105105
"description":"The maximum count for each shard, upon reaching which the query execution will terminate early"
106+
},
107+
"stats":{
108+
"type":"list",
109+
"description":"Specific 'tag' of the request for logging and statistical purposes"
106110
}
107111
},
108112
"body":{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
setup:
2+
- do:
3+
indices.create:
4+
index: test
5+
- do:
6+
index:
7+
index: test
8+
id: "1"
9+
body: { foo: bar }
10+
- do:
11+
indices.refresh:
12+
index: [test]
13+
14+
---
15+
"count with single stat":
16+
- do:
17+
count:
18+
index: test
19+
body:
20+
query:
21+
match_all: {}
22+
stats: ["stat_1"]
23+
24+
- match: {count : 1}
25+
26+
---
27+
"count with multiple stats":
28+
- do:
29+
count:
30+
index: test
31+
body:
32+
query:
33+
match_all: {}
34+
stats: ["stat_1","stat_2"]
35+
36+
- match: {count : 1}

server/src/main/java/org/elasticsearch/rest/action/RestActions.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package org.elasticsearch.rest.action;
1111

12+
import java.util.Set;
13+
1214
import org.elasticsearch.ExceptionsHelper;
1315
import org.elasticsearch.action.FailedNodeException;
1416
import org.elasticsearch.action.ShardOperationFailedException;
@@ -264,6 +266,13 @@ public RestResponse buildResponse(NodesResponse response, XContentBuilder builde
264266
* Parses a top level query including the query element that wraps it
265267
*/
266268
public static QueryBuilder getQueryContent(XContentParser parser) {
269+
return getQueryContent(parser, Set.of());
270+
}
271+
272+
/**
273+
* Parses a top level query including the query element that wraps it. Ignoring explicitly supplied ignored tokens when validating.
274+
*/
275+
public static QueryBuilder getQueryContent(XContentParser parser, Set<String> ignoredTokens) {
267276
try {
268277
QueryBuilder queryBuilder = null;
269278
XContentParser.Token first = parser.nextToken();
@@ -281,7 +290,7 @@ public static QueryBuilder getQueryContent(XContentParser parser) {
281290
String currentName = parser.currentName();
282291
if ("query".equals(currentName)) {
283292
queryBuilder = parseTopLevelQuery(parser);
284-
} else {
293+
} else if (ignoredTokens.contains(currentName) == false) {
285294
throw new ParsingException(parser.getTokenLocation(), "request does not support [" + parser.currentName() + "]");
286295
}
287296
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import org.elasticsearch.xcontent.XContentBuilder;
2828

2929
import java.io.IOException;
30+
import java.util.Arrays;
3031
import java.util.List;
32+
import java.util.Set;
3133

3234
import static org.elasticsearch.rest.RestRequest.Method.GET;
3335
import static org.elasticsearch.rest.RestRequest.Method.POST;
@@ -67,14 +69,18 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
6769
searchSourceBuilder.query(queryBuilder);
6870
}
6971
} else {
70-
searchSourceBuilder.query(RestActions.getQueryContent(parser));
72+
searchSourceBuilder.query(RestActions.getQueryContent(parser, Set.of("stats")));
7173
}
7274
});
7375
countRequest.routing(request.param("routing"));
7476
float minScore = request.paramAsFloat("min_score", -1f);
7577
if (minScore != -1f) {
7678
searchSourceBuilder.minScore(minScore);
7779
}
80+
String sStats = request.param("stats");
81+
if (sStats != null) {
82+
searchSourceBuilder.stats(Arrays.asList(Strings.splitStringByCommaToArray(sStats)));
83+
}
7884

7985
countRequest.preference(request.param("preference"));
8086

0 commit comments

Comments
 (0)