diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java index 12867013cfe..b8d68eec3c8 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java @@ -93,8 +93,6 @@ public class GetJobsAction extends Action PARSER = new ObjectParser<>(NAME, Request::new); public static final ParseField METRIC = new ParseField("metric"); - - static { PARSER.declareString(Request::setJobId, Job.ID); PARSER.declareObject(Request::setPageParams, PageParams.PARSER, PageParams.PAGE); @@ -104,20 +102,23 @@ public class GetJobsAction extends Action(Collections.singletonList(jobInfo), 1, Job.RESULTS_FIELD); - } else { + } else if (request.getPageParams() != null) { // Multiple Jobs - QueryPage jobsPage = jobManager.getJobs(request.pageParams.getFrom(), request.pageParams.getSize(), state); + + PageParams pageParams = request.getPageParams(); + QueryPage jobsPage = jobManager.getJobs(pageParams.getFrom(), pageParams.getSize(), state); List jobInfoList = new ArrayList<>(); for (Job job : jobsPage.results()) { Job jobConfig = request.config() ? job : null; @@ -523,6 +529,8 @@ public class GetJobsAction extends Action(jobInfoList, jobsPage.count(), Job.RESULTS_FIELD); + } else { + throw new IllegalStateException("Both jobId and pageParams are null"); } listener.onResponse(new Response(response)); diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java index d5e5d3f957a..7148a2a5059 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/job/RestGetJobsAction.java @@ -57,13 +57,21 @@ public class RestGetJobsAction extends BaseRestHandler { XContentParser parser = XContentFactory.xContent(bodyBytes).createParser(bodyBytes); request = GetJobsAction.Request.PARSER.apply(parser, () -> parseFieldMatcher); } else { + String jobId = restRequest.param(Job.ID.getPreferredName()); request = new GetJobsAction.Request(); - request.setJobId(restRequest.param(Job.ID.getPreferredName())); + if (jobId != null && !jobId.isEmpty()) { + request.setJobId(jobId); + } + if (restRequest.hasParam(PageParams.FROM.getPreferredName()) + || restRequest.hasParam(PageParams.SIZE.getPreferredName()) + || jobId == null) { + request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + } Set stats = Strings.splitStringByCommaToSet( restRequest.param(GetJobsAction.Request.METRIC.getPreferredName(), "config")); request.setStats(stats); - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + } return channel -> transportGetJobAction.execute(request, new RestStatusToXContentListener<>(channel)); diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java index 8fbeb0ed7e3..009c38b9428 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/action/GetJobActionRequestTests.java @@ -24,8 +24,7 @@ public class GetJobActionRequestTests extends AbstractStreamableTestCase