[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:
parent
415d40e6fc
commit
7f64f37c46
|
@ -26,14 +26,14 @@ public class PageParams extends ToXContentToBytes implements Writeable {
|
||||||
public static final int DEFAULT_SIZE = 100;
|
public static final int DEFAULT_SIZE = 100;
|
||||||
|
|
||||||
|
|
||||||
public static final ConstructingObjectParser<PageParams, Void> PARSER = new ConstructingObjectParser<>(
|
public static final ConstructingObjectParser<PageParams, Void> PARSER = new ConstructingObjectParser<>(PAGE.getPreferredName(),
|
||||||
PAGE.getPreferredName(), a -> new PageParams((int) a[0], (int) a[1]));
|
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;
|
public static final int MAX_FROM_SIZE_SUM = 10000;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
PARSER.declareInt(ConstructingObjectParser.constructorArg(), FROM);
|
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), FROM);
|
||||||
PARSER.declareInt(ConstructingObjectParser.constructorArg(), SIZE);
|
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final int from;
|
private final int from;
|
||||||
|
|
|
@ -49,9 +49,10 @@ public class RestGetCategoriesAction extends BaseRestHandler {
|
||||||
if (bodyBytes != null && bodyBytes.length() > 0) {
|
if (bodyBytes != null && bodyBytes.length() > 0) {
|
||||||
XContentParser parser = restRequest.contentParser();
|
XContentParser parser = restRequest.contentParser();
|
||||||
request = GetCategoriesAction.Request.parseRequest(jobId, parser);
|
request = GetCategoriesAction.Request.parseRequest(jobId, parser);
|
||||||
request.setCategoryId(categoryId);
|
if (!Strings.isNullOrEmpty(categoryId)) {
|
||||||
|
request.setCategoryId(categoryId);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
request = new Request(jobId);
|
request = new Request(jobId);
|
||||||
if (!Strings.isNullOrEmpty(categoryId)) {
|
if (!Strings.isNullOrEmpty(categoryId)) {
|
||||||
request.setCategoryId(categoryId);
|
request.setCategoryId(categoryId);
|
||||||
|
|
|
@ -49,7 +49,54 @@ setup:
|
||||||
- match: { categories.1.category_id: 2 }
|
- 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:
|
- do:
|
||||||
xpack.ml.get_categories:
|
xpack.ml.get_categories:
|
||||||
job_id: "farequote"
|
job_id: "farequote"
|
||||||
|
|
Loading…
Reference in New Issue