Skip to content

Commit dfa97a5

Browse files
committed
[ML] adding for_export flag for ml plugin GET resource APIs (elastic#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 elastic#63055
1 parent b06c617 commit dfa97a5

File tree

30 files changed

+567
-111
lines changed

30 files changed

+567
-111
lines changed

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

+11-2
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

+21-3
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

+21-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ public class GetDatafeedRequest extends ActionRequest implements ToXContentObjec
4242

4343
public static final ParseField DATAFEED_IDS = new ParseField("datafeed_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_DATAFEEDS = "_all";
4748
private final List<String> datafeedIds;
4849
private Boolean allowNoMatch;
50+
private Boolean forExport;
4951

5052
@SuppressWarnings("unchecked")
5153
public static final ConstructingObjectParser<GetDatafeedRequest, Void> PARSER = new ConstructingObjectParser<>(
@@ -101,14 +103,30 @@ public Boolean getAllowNoMatch() {
101103
return allowNoMatch;
102104
}
103105

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

109127
@Override
110128
public int hashCode() {
111-
return Objects.hash(datafeedIds, allowNoMatch);
129+
return Objects.hash(datafeedIds, forExport, allowNoMatch);
112130
}
113131

114132
@Override
@@ -123,7 +141,8 @@ public boolean equals(Object other) {
123141

124142
GetDatafeedRequest that = (GetDatafeedRequest) other;
125143
return Objects.equals(datafeedIds, that.datafeedIds) &&
126-
Objects.equals(allowNoMatch, that.allowNoMatch);
144+
Objects.equals(allowNoMatch, that.allowNoMatch) &&
145+
Objects.equals(forExport, that.forExport);
127146
}
128147

129148
@Override

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ public class GetJobRequest extends ActionRequest implements ToXContentObject {
4343

4444
public static final ParseField JOB_IDS = new ParseField("job_ids");
4545
public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match");
46+
public static final String FOR_EXPORT = "for_export";
4647

4748
private static final String ALL_JOBS = "_all";
4849
private final List<String> jobIds;
4950
private Boolean allowNoMatch;
51+
private Boolean forExport;
5052

5153
@SuppressWarnings("unchecked")
5254
public static final ConstructingObjectParser<GetJobRequest, Void> PARSER = new ConstructingObjectParser<>(
@@ -101,14 +103,30 @@ public Boolean getAllowNoMatch() {
101103
return allowNoMatch;
102104
}
103105

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

109127
@Override
110128
public int hashCode() {
111-
return Objects.hash(jobIds, allowNoMatch);
129+
return Objects.hash(jobIds, forExport, allowNoMatch);
112130
}
113131

114132
@Override
@@ -123,6 +141,7 @@ public boolean equals(Object other) {
123141

124142
GetJobRequest that = (GetJobRequest) other;
125143
return Objects.equals(jobIds, that.jobIds) &&
144+
Objects.equals(forExport, that.forExport) &&
126145
Objects.equals(allowNoMatch, that.allowNoMatch);
127146
}
128147

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

+3
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ public void testGetJob() throws Exception {
343343
// tag::get-job-request
344344
GetJobRequest request = new GetJobRequest("get-machine-learning-job1", "get-machine-learning-job*"); // <1>
345345
request.setAllowNoMatch(true); // <2>
346+
request.setForExport(false); // <3>
346347
// end::get-job-request
347348

348349
// tag::get-job-execute
@@ -839,6 +840,7 @@ public void testGetDatafeed() throws Exception {
839840
// tag::get-datafeed-request
840841
GetDatafeedRequest request = new GetDatafeedRequest(datafeedId); // <1>
841842
request.setAllowNoMatch(true); // <2>
843+
request.setForExport(false); // <3>
842844
// end::get-datafeed-request
843845

844846
// tag::get-datafeed-execute
@@ -2864,6 +2866,7 @@ public void testGetDataFrameAnalytics() throws Exception {
28642866
{
28652867
// tag::get-data-frame-analytics-request
28662868
GetDataFrameAnalyticsRequest request = new GetDataFrameAnalyticsRequest("my-analytics-config"); // <1>
2869+
request.setForExport(false); // <2>
28672870
// end::get-data-frame-analytics-request
28682871

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

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

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

2730
include::../execution.asciidoc[]
2831

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

+3
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 datafeeds response

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

+3
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

+4
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

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ 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]
56+
5357
[[ml-get-job-results]]
5458
== {api-response-body-title}
5559

docs/reference/ml/df-analytics/apis/get-dfanalytics.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=from]
7070
(Optional, integer)
7171
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=size]
7272

73+
`for_export`::
74+
(Optional, boolean)
75+
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=for-export]
76+
7377
[role="child_attributes"]
7478
[[ml-get-dfanalytics-results]]
7579
== {api-response-body-title}

docs/reference/ml/df-analytics/apis/get-trained-models.asciidoc

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ Specifies whether the included model definition should be returned as a JSON map
6767

6868
`for_export`::
6969
(Optional, boolean)
70-
Indicates if certain fields should be removed from the model configuration on
71-
retrieval. This allows the model to be in an acceptable format to be retrieved
72-
and then added to another cluster. Default is false.
70+
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=for-export]
7371

7472
`from`::
7573
(Optional, integer)

docs/reference/ml/ml-shared.asciidoc

+6
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ The number of individual forecasts currently available for the job. A value of
673673
`1` or more indicates that forecasts exist.
674674
end::forecast-total[]
675675

676+
tag::for-export[]
677+
Indicates if certain fields should be removed from the configuration on
678+
retrieval. This allows the configuration to be in an acceptable format to be retrieved
679+
and then added to another cluster. Default is false.
680+
end::for-export[]
681+
676682
tag::frequency[]
677683
The interval at which scheduled queries are made while the {dfeed} runs in real
678684
time. The default value is either the bucket span for short bucket spans, or,

0 commit comments

Comments
 (0)