Skip to content

Commit 7bd6e78

Browse files
authored
[ML] adding for_export flag for ml plugin GET resource APIs (#63092)
This adds the new `for_export` flag to the following APIs: - GET _ml/anomaly_detection/<job_id> - GET _ml/datafeeds/<datafeed_id> - GET _ml/data_frame/analytics/<analytics_id> The flag is designed for cloning or exporting configuration objects to later be put into the same cluster or a separate cluster. The following fields are not returned in the objects: - any field that is not user settable (e.g. version, create_time) - any field that is a calculated default value (e.g. datafeed chunking_config) - any field that would effectively require changing to be of use (e.g. datafeed job_id) - any field that is automatically set via another Elastic stack process (e.g. anomaly job custom_settings.created_by) closes #63055
1 parent d87c2ca commit 7bd6e78

File tree

30 files changed

+568
-111
lines changed

30 files changed

+568
-111
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ static Request getJob(GetJobRequest getJobRequest) {
121121

122122
RequestConverters.Params params = new RequestConverters.Params();
123123
if (getJobRequest.getAllowNoMatch() != null) {
124-
params.putParam("allow_no_match", Boolean.toString(getJobRequest.getAllowNoMatch()));
124+
params.putParam(GetJobRequest.ALLOW_NO_MATCH.getPreferredName(), Boolean.toString(getJobRequest.getAllowNoMatch()));
125+
}
126+
if (getJobRequest.getForExport() != null) {
127+
params.putParam(GetJobRequest.FOR_EXPORT, Boolean.toString(getJobRequest.getForExport()));
125128
}
126129
request.addParameters(params.asMap());
127130
return request;
@@ -270,6 +273,9 @@ static Request getDatafeed(GetDatafeedRequest getDatafeedRequest) {
270273
params.putParam(GetDatafeedRequest.ALLOW_NO_MATCH.getPreferredName(),
271274
Boolean.toString(getDatafeedRequest.getAllowNoMatch()));
272275
}
276+
if (getDatafeedRequest.getForExport() != null) {
277+
params.putParam(GetDatafeedRequest.FOR_EXPORT, Boolean.toString(getDatafeedRequest.getForExport()));
278+
}
273279
request.addParameters(params.asMap());
274280
return request;
275281
}
@@ -645,7 +651,10 @@ static Request getDataFrameAnalytics(GetDataFrameAnalyticsRequest getRequest) {
645651
}
646652
}
647653
if (getRequest.getAllowNoMatch() != null) {
648-
params.putParam(GetDataFrameAnalyticsRequest.ALLOW_NO_MATCH.getPreferredName(), Boolean.toString(getRequest.getAllowNoMatch()));
654+
params.putParam(GetDataFrameAnalyticsRequest.ALLOW_NO_MATCH, Boolean.toString(getRequest.getAllowNoMatch()));
655+
}
656+
if (getRequest.getForExport() != null) {
657+
params.putParam(GetDataFrameAnalyticsRequest.FOR_EXPORT, Boolean.toString(getRequest.getForExport()));
649658
}
650659
request.addParameters(params.asMap());
651660
return request;

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsRequest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.client.ValidationException;
2424
import org.elasticsearch.client.core.PageParams;
2525
import org.elasticsearch.common.Nullable;
26-
import org.elasticsearch.common.ParseField;
2726

2827
import java.util.Arrays;
2928
import java.util.List;
@@ -32,11 +31,13 @@
3231

3332
public class GetDataFrameAnalyticsRequest implements Validatable {
3433

35-
public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match");
34+
public static final String ALLOW_NO_MATCH = "allow_no_match";
35+
public static final String FOR_EXPORT = "for_export";
3636

3737
private final List<String> ids;
3838
private Boolean allowNoMatch;
3939
private PageParams pageParams;
40+
private Boolean forExport;
4041

4142
/**
4243
* Helper method to create a request that will get ALL Data Frame Analytics
@@ -58,6 +59,22 @@ public Boolean getAllowNoMatch() {
5859
return allowNoMatch;
5960
}
6061

62+
/**
63+
* Setting this flag to `true` removes certain fields from the configuration on retrieval.
64+
*
65+
* This is useful when getting the configuration and wanting to put it in another cluster.
66+
*
67+
* Default value is false.
68+
* @param forExport Boolean value indicating if certain fields should be removed
69+
*/
70+
public void setForExport(boolean forExport) {
71+
this.forExport = forExport;
72+
}
73+
74+
public Boolean getForExport() {
75+
return forExport;
76+
}
77+
6178
/**
6279
* Whether to ignore if a wildcard expression matches no data frame analytics.
6380
*
@@ -94,11 +111,12 @@ public boolean equals(Object o) {
94111
GetDataFrameAnalyticsRequest other = (GetDataFrameAnalyticsRequest) o;
95112
return Objects.equals(ids, other.ids)
96113
&& Objects.equals(allowNoMatch, other.allowNoMatch)
114+
&& Objects.equals(forExport, other.forExport)
97115
&& Objects.equals(pageParams, other.pageParams);
98116
}
99117

100118
@Override
101119
public int hashCode() {
102-
return Objects.hash(ids, allowNoMatch, pageParams);
120+
return Objects.hash(ids, allowNoMatch, forExport, pageParams);
103121
}
104122
}

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedRequest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ public class GetDatafeedRequest implements Validatable, ToXContentObject {
4141

4242
public static final ParseField DATAFEED_IDS = new ParseField("datafeed_ids");
4343
public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match");
44+
public static final String FOR_EXPORT = "for_export";
4445

4546
private static final String ALL_DATAFEEDS = "_all";
4647
private final List<String> datafeedIds;
4748
private Boolean allowNoMatch;
49+
private Boolean forExport;
4850

4951
@SuppressWarnings("unchecked")
5052
public static final ConstructingObjectParser<GetDatafeedRequest, Void> PARSER = new ConstructingObjectParser<>(
@@ -100,9 +102,25 @@ public Boolean getAllowNoMatch() {
100102
return allowNoMatch;
101103
}
102104

105+
/**
106+
* Setting this flag to `true` removes certain fields from the configuration on retrieval.
107+
*
108+
* This is useful when getting the configuration and wanting to put it in another cluster.
109+
*
110+
* Default value is false.
111+
* @param forExport Boolean value indicating if certain fields should be removed
112+
*/
113+
public void setForExport(boolean forExport) {
114+
this.forExport = forExport;
115+
}
116+
117+
public Boolean getForExport() {
118+
return forExport;
119+
}
120+
103121
@Override
104122
public int hashCode() {
105-
return Objects.hash(datafeedIds, allowNoMatch);
123+
return Objects.hash(datafeedIds, forExport, allowNoMatch);
106124
}
107125

108126
@Override
@@ -117,7 +135,8 @@ public boolean equals(Object other) {
117135

118136
GetDatafeedRequest that = (GetDatafeedRequest) other;
119137
return Objects.equals(datafeedIds, that.datafeedIds) &&
120-
Objects.equals(allowNoMatch, that.allowNoMatch);
138+
Objects.equals(allowNoMatch, that.allowNoMatch) &&
139+
Objects.equals(forExport, that.forExport);
121140
}
122141

123142
@Override

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobRequest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ public class GetJobRequest implements Validatable, ToXContentObject {
4242

4343
public static final ParseField JOB_IDS = new ParseField("job_ids");
4444
public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match");
45+
public static final String FOR_EXPORT = "for_export";
4546

4647
private static final String ALL_JOBS = "_all";
4748
private final List<String> jobIds;
4849
private Boolean allowNoMatch;
50+
private Boolean forExport;
4951

5052
@SuppressWarnings("unchecked")
5153
public static final ConstructingObjectParser<GetJobRequest, Void> PARSER = new ConstructingObjectParser<>(
@@ -100,9 +102,25 @@ public Boolean getAllowNoMatch() {
100102
return allowNoMatch;
101103
}
102104

105+
/**
106+
* Setting this flag to `true` removes certain fields from the configuration on retrieval.
107+
*
108+
* This is useful when getting the configuration and wanting to put it in another cluster.
109+
*
110+
* Default value is false.
111+
* @param forExport Boolean value indicating if certain fields should be removed
112+
*/
113+
public void setForExport(boolean forExport) {
114+
this.forExport = forExport;
115+
}
116+
117+
public Boolean getForExport() {
118+
return forExport;
119+
}
120+
103121
@Override
104122
public int hashCode() {
105-
return Objects.hash(jobIds, allowNoMatch);
123+
return Objects.hash(jobIds, forExport, allowNoMatch);
106124
}
107125

108126
@Override
@@ -117,6 +135,7 @@ public boolean equals(Object other) {
117135

118136
GetJobRequest that = (GetJobRequest) other;
119137
return Objects.equals(jobIds, that.jobIds) &&
138+
Objects.equals(forExport, that.forExport) &&
120139
Objects.equals(allowNoMatch, that.allowNoMatch);
121140
}
122141

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public void testGetJob() throws Exception {
340340
// tag::get-job-request
341341
GetJobRequest request = new GetJobRequest("get-machine-learning-job1", "get-machine-learning-job*"); // <1>
342342
request.setAllowNoMatch(true); // <2>
343+
request.setForExport(false); // <3>
343344
// end::get-job-request
344345

345346
// tag::get-job-execute
@@ -836,6 +837,7 @@ public void testGetDatafeed() throws Exception {
836837
// tag::get-datafeed-request
837838
GetDatafeedRequest request = new GetDatafeedRequest(datafeedId); // <1>
838839
request.setAllowNoMatch(true); // <2>
840+
request.setForExport(false); // <3>
839841
// end::get-datafeed-request
840842

841843
// tag::get-datafeed-execute
@@ -2861,6 +2863,7 @@ public void testGetDataFrameAnalytics() throws Exception {
28612863
{
28622864
// tag::get-data-frame-analytics-request
28632865
GetDataFrameAnalyticsRequest request = new GetDataFrameAnalyticsRequest("my-analytics-config"); // <1>
2866+
request.setForExport(false); // <2>
28642867
// end::get-data-frame-analytics-request
28652868

28662869
// tag::get-data-frame-analytics-execute

docs/java-rest/high-level/ml/get-data-frame-analytics.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ IDs, or the special wildcard `_all` to get all {dfanalytics-jobs}.
2121
include-tagged::{doc-tests-file}[{api}-request]
2222
--------------------------------------------------
2323
<1> Constructing a new GET request referencing an existing {dfanalytics-job}
24+
<2> Optional boolean value for requesting the {dfanalytics-job} in a format that can
25+
then be put into another cluster. Certain fields that can only be set when
26+
the {dfanalytics-job} is created are removed.
2427

2528
include::../execution.asciidoc[]
2629

docs/java-rest/high-level/ml/get-datafeed.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ include-tagged::{doc-tests-file}[{api}-request]
2525
contain wildcards.
2626
<2> Whether to ignore if a wildcard expression matches no datafeeds.
2727
(This includes `_all` string or when no datafeeds have been specified).
28+
<3> Optional boolean value for requesting the datafeed in a format that can
29+
then be put into another cluster. Certain fields that can only be set when
30+
the datafeed is created are removed.
2831

2932
[id="{upid}-{api}-response"]
3033
==== Get datafeed response

docs/java-rest/high-level/ml/get-job.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ include-tagged::{doc-tests-file}[{api}-request]
2525
wildcards.
2626
<2> Whether to ignore if a wildcard expression matches no {anomaly-jobs}.
2727
(This includes `_all` string or when no jobs have been specified).
28+
<3> Optional boolean value for requesting the job in a format that can
29+
then be put into another cluster. Certain fields that can only be set when
30+
the job is created are removed.
2831

2932
[id="{upid}-{api}-response"]
3033
==== Get {anomaly-jobs} response

docs/reference/ml/anomaly-detection/apis/get-datafeed.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ all {dfeeds}.
5757
(Optional, boolean)
5858
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=allow-no-datafeeds]
5959

60+
`for_export`::
61+
(Optional, boolean)
62+
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=for-export]
63+
6064
[[ml-get-datafeed-results]]
6165
== {api-response-body-title}
6266

docs/reference/ml/anomaly-detection/apis/get-job.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=job-id-anomaly-detection-defaul
5050
(Optional, boolean)
5151
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=allow-no-jobs]
5252

53+
`for_export`::
54+
(Optional, boolean)
55+
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=for-export]
5356

5457
[[ml-get-job-results]]
5558
== {api-response-body-title}

0 commit comments

Comments
 (0)