[ML] Fix POST version of get categories API (elastic/x-pack-elasticsearch#1184)

Also improves PageParams parsing to fill in defaults.

relates elastic/x-pack-elasticsearch#1180

Original commit: elastic/x-pack-elasticsearch@fccd7795ca
This commit is contained in:
Dimitris Athanasiou 2017-04-25 14:44:10 +01:00 committed by GitHub
parent 415d40e6fc
commit 7f64f37c46
3 changed files with 55 additions and 7 deletions

View File

@ -26,14 +26,14 @@ public class PageParams extends ToXContentToBytes implements Writeable {
public static final int DEFAULT_SIZE = 100;
public static final ConstructingObjectParser<PageParams, Void> PARSER = new ConstructingObjectParser<>(
PAGE.getPreferredName(), a -> new PageParams((int) a[0], (int) a[1]));
public static final ConstructingObjectParser<PageParams, Void> PARSER = new ConstructingObjectParser<>(PAGE.getPreferredName(),
a -> new PageParams(a[0] == null ? DEFAULT_FROM : (int) a[0], a[1] == null ? DEFAULT_SIZE : (int) a[1]));
public static final int MAX_FROM_SIZE_SUM = 10000;
static {
PARSER.declareInt(ConstructingObjectParser.constructorArg(), FROM);
PARSER.declareInt(ConstructingObjectParser.constructorArg(), SIZE);
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), FROM);
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), SIZE);
}
private final int from;

View File

@ -49,9 +49,10 @@ public class RestGetCategoriesAction extends BaseRestHandler {
if (bodyBytes != null && bodyBytes.length() > 0) {
XContentParser parser = restRequest.contentParser();
request = GetCategoriesAction.Request.parseRequest(jobId, parser);
if (!Strings.isNullOrEmpty(categoryId)) {
request.setCategoryId(categoryId);
}
} else {
request = new Request(jobId);
if (!Strings.isNullOrEmpty(categoryId)) {
request.setCategoryId(categoryId);

View File

@ -49,7 +49,54 @@ setup:
- match: { categories.1.category_id: 2 }
---
"Test result category api":
"Test get categories with pagination":
- do:
xpack.ml.get_categories:
job_id: "farequote"
size: 1
- length: { categories: 1 }
- match: { categories.0.job_id: farequote }
- match: { categories.0.category_id: 1 }
- do:
xpack.ml.get_categories:
job_id: "farequote"
from: 1
size: 2
- length: { categories: 1 }
- match: { categories.0.job_id: farequote }
- match: { categories.0.category_id: 2 }
---
"Test post get categories with pagination":
- do:
xpack.ml.get_categories:
job_id: "farequote"
body: >
{
"page": { "size": 1}
}
- length: { categories: 1 }
- match: { categories.0.job_id: farequote }
- match: { categories.0.category_id: 1 }
- do:
xpack.ml.get_categories:
job_id: "farequote"
body: >
{
"page": { "from":1, "size": 1}
}
- length: { categories: 1 }
- match: { categories.0.job_id: farequote }
- match: { categories.0.category_id: 2 }
---
"Test get category by id":
- do:
xpack.ml.get_categories:
job_id: "farequote"