Skip to content

Commit 5fec253

Browse files
authored
[ML] Docs and HRLC for datafeed runtime mappings (#65810) (#66007)
For the changes in #65606
1 parent 32776bc commit 5fec253

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Arrays;
4242
import java.util.Collections;
4343
import java.util.Comparator;
44+
import java.util.HashMap;
4445
import java.util.List;
4546
import java.util.Map;
4647
import java.util.Objects;
@@ -95,6 +96,7 @@ public class DatafeedConfig implements ToXContentObject {
9596
PARSER.declareObject(Builder::setIndicesOptions,
9697
(p, c) -> IndicesOptions.fromMap(p.map(), new IndicesOptions(IndicesOptions.Option.NONE, IndicesOptions.WildcardStates.NONE)),
9798
INDICES_OPTIONS);
99+
PARSER.declareObject(Builder::setRuntimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD);
98100
}
99101

100102
private static BytesReference parseBytes(XContentParser parser) throws IOException {
@@ -116,11 +118,12 @@ private static BytesReference parseBytes(XContentParser parser) throws IOExcepti
116118
private final DelayedDataCheckConfig delayedDataCheckConfig;
117119
private final Integer maxEmptySearches;
118120
private final IndicesOptions indicesOptions;
121+
private final Map<String, Object> runtimeMappings;
119122

120123
private DatafeedConfig(String id, String jobId, TimeValue queryDelay, TimeValue frequency, List<String> indices, BytesReference query,
121124
BytesReference aggregations, List<SearchSourceBuilder.ScriptField> scriptFields, Integer scrollSize,
122125
ChunkingConfig chunkingConfig, DelayedDataCheckConfig delayedDataCheckConfig,
123-
Integer maxEmptySearches, IndicesOptions indicesOptions) {
126+
Integer maxEmptySearches, IndicesOptions indicesOptions, Map<String, Object> runtimeMappings) {
124127
this.id = id;
125128
this.jobId = jobId;
126129
this.queryDelay = queryDelay;
@@ -134,6 +137,7 @@ private DatafeedConfig(String id, String jobId, TimeValue queryDelay, TimeValue
134137
this.delayedDataCheckConfig = delayedDataCheckConfig;
135138
this.maxEmptySearches = maxEmptySearches;
136139
this.indicesOptions = indicesOptions;
140+
this.runtimeMappings = Collections.unmodifiableMap(runtimeMappings);
137141
}
138142

139143
public String getId() {
@@ -188,6 +192,10 @@ public IndicesOptions getIndicesOptions() {
188192
return indicesOptions;
189193
}
190194

195+
public Map<String, Object> getRuntimeMappings() {
196+
return runtimeMappings;
197+
}
198+
191199
@Override
192200
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
193201
builder.startObject();
@@ -232,6 +240,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
232240
indicesOptions.toXContent(builder, params);
233241
builder.endObject();
234242
}
243+
if (runtimeMappings.isEmpty() == false) {
244+
builder.field(SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName(), runtimeMappings);
245+
}
235246

236247
builder.endObject();
237248
return builder;
@@ -274,7 +285,8 @@ public boolean equals(Object other) {
274285
&& Objects.equals(this.chunkingConfig, that.chunkingConfig)
275286
&& Objects.equals(this.delayedDataCheckConfig, that.delayedDataCheckConfig)
276287
&& Objects.equals(this.maxEmptySearches, that.maxEmptySearches)
277-
&& Objects.equals(this.indicesOptions, that.indicesOptions);
288+
&& Objects.equals(this.indicesOptions, that.indicesOptions)
289+
&& Objects.equals(this.runtimeMappings, that.runtimeMappings);
278290
}
279291

280292
/**
@@ -285,7 +297,7 @@ public boolean equals(Object other) {
285297
@Override
286298
public int hashCode() {
287299
return Objects.hash(id, jobId, frequency, queryDelay, indices, asMap(query), scrollSize, asMap(aggregations), scriptFields,
288-
chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions);
300+
chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings);
289301
}
290302

291303
public static Builder builder(String id, String jobId) {
@@ -294,8 +306,8 @@ public static Builder builder(String id, String jobId) {
294306

295307
public static class Builder {
296308

297-
private String id;
298-
private String jobId;
309+
private final String id;
310+
private final String jobId;
299311
private TimeValue queryDelay;
300312
private TimeValue frequency;
301313
private List<String> indices;
@@ -307,6 +319,7 @@ public static class Builder {
307319
private DelayedDataCheckConfig delayedDataCheckConfig;
308320
private Integer maxEmptySearches;
309321
private IndicesOptions indicesOptions;
322+
private Map<String, Object> runtimeMappings = Collections.emptyMap();
310323

311324
public Builder(String id, String jobId) {
312325
this.id = Objects.requireNonNull(id, ID.getPreferredName());
@@ -327,6 +340,7 @@ public Builder(DatafeedConfig config) {
327340
this.delayedDataCheckConfig = config.getDelayedDataCheckConfig();
328341
this.maxEmptySearches = config.getMaxEmptySearches();
329342
this.indicesOptions = config.indicesOptions;
343+
this.runtimeMappings = new HashMap<>(config.runtimeMappings);
330344
}
331345

332346
public Builder setIndices(List<String> indices) {
@@ -419,9 +433,15 @@ public Builder setIndicesOptions(IndicesOptions indicesOptions) {
419433
return this;
420434
}
421435

436+
public Builder setRuntimeMappings(Map<String, Object> runtimeMappings) {
437+
this.runtimeMappings = Objects.requireNonNull(runtimeMappings,
438+
SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName());
439+
return this;
440+
}
441+
422442
public DatafeedConfig build() {
423443
return new DatafeedConfig(id, jobId, queryDelay, frequency, indices, query, aggregations, scriptFields, scrollSize,
424-
chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions);
444+
chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings);
425445
}
426446

427447
private static BytesReference xContentToBytes(ToXContentObject object) throws IOException {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,16 @@ public void testPutDatafeed() throws Exception {
711711
datafeedBuilder.setScrollSize(1000); // <1>
712712
// end::put-datafeed-config-set-scroll-size
713713

714+
// tag::put-datafeed-config-set-runtime-mappings
715+
Map<String, Object> fieldProperties = new HashMap<>();
716+
fieldProperties.put("type", "keyword");
717+
fieldProperties.put("script", "emit(params._source.agent.toLowerCase())");
718+
Map<String, Object> runtimeMappings = new HashMap<>();
719+
runtimeMappings.put("agent_lowercase", fieldProperties);
720+
721+
datafeedBuilder.setRuntimeMappings(runtimeMappings); // <1>
722+
// end::put-datafeed-config-set-runtime-mappings
723+
714724
// tag::put-datafeed-request
715725
PutDatafeedRequest request = new PutDatafeedRequest(datafeedBuilder.build()); // <1>
716726
// end::put-datafeed-request

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedConfigTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636

3737
import java.io.IOException;
3838
import java.util.ArrayList;
39+
import java.util.HashMap;
3940
import java.util.List;
41+
import java.util.Map;
4042

4143
public class DatafeedConfigTests extends AbstractXContentTestCase<DatafeedConfig> {
4244

@@ -117,6 +119,14 @@ public static DatafeedConfig.Builder createRandomBuilder() {
117119
randomBoolean(),
118120
randomBoolean()));
119121
}
122+
if (randomBoolean()) {
123+
Map<String, Object> settings = new HashMap<>();
124+
settings.put("type", "keyword");
125+
settings.put("script", "");
126+
Map<String, Object> field = new HashMap<>();
127+
field.put("runtime_field_foo", settings);
128+
builder.setRuntimeMappings(field);
129+
}
120130
return builder;
121131
}
122132

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ include-tagged::{doc-tests-file}[{api}-config-set-scroll-size]
8989
--------------------------------------------------
9090
<1> The `size` parameter used in the searches.
9191

92+
["source","java",subs="attributes,callouts,macros"]
93+
--------------------------------------------------
94+
include-tagged::{doc-tests-file}[{api}-config-set-runtime-mappings]
95+
--------------------------------------------------
96+
<1> Set the runtime mappings used in the searches.
97+
9298
include::../execution.asciidoc[]
9399

94100
[id="{upid}-{api}-response"]

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Instantiates a {dfeed}.
1818
[[ml-put-datafeed-prereqs]]
1919
== {api-prereq-title}
2020

21-
* You must create an {anomaly-job} before you create a {dfeed}.
21+
* You must create an {anomaly-job} before you create a {dfeed}.
2222
* If {es} {security-features} are enabled, you must have `manage_ml` or `manage`
2323
cluster privileges to use this API. See <<security-privileges>> and
2424
{ml-docs-setup-privileges}.
@@ -105,6 +105,9 @@ include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=scroll-size]
105105
(Optional, object)
106106
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=indices-options]
107107

108+
`runtime_mappings`::
109+
(Optional, object)
110+
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=runtime-mappings]
108111

109112
[[ml-put-datafeed-example]]
110113
== {api-examples-title}

docs/reference/ml/ml-shared.asciidoc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,25 @@ For more information about these options, see <<multi-index>>.
765765
--
766766
end::indices-options[]
767767

768+
tag::runtime-mappings[]
769+
Specifies runtime fields for the datafeed search.
770+
+
771+
--
772+
For example:
773+
```
774+
{
775+
"day_of_week": {
776+
"type": "keyword",
777+
"script": {
778+
"source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
779+
}
780+
}
781+
}
782+
```
783+
784+
--
785+
end::runtime-mappings[]
786+
768787
tag::inference-config-classification-num-top-classes[]
769788
Specifies the number of top class predictions to return. Defaults to 0.
770789
end::inference-config-classification-num-top-classes[]
@@ -992,7 +1011,7 @@ There are three available modes:
9921011
--
9931012
* `auto`: The chunk size is dynamically calculated. This is the default and
9941013
recommended value when the {dfeed} does not use aggregations.
995-
* `manual`: Chunking is applied according to the specified `time_span`. Use this
1014+
* `manual`: Chunking is applied according to the specified `time_span`. Use this
9961015
mode when the {dfeed} uses aggregations.
9971016
* `off`: No chunking is applied.
9981017
--
@@ -1297,8 +1316,8 @@ and <<script-fields,Script fields>>.
12971316
end::script-fields[]
12981317

12991318
tag::scroll-size[]
1300-
The `size` parameter that is used in {es} searches when the {dfeed} does not use
1301-
aggregations. The default value is `1000`. The maximum value is the value of
1319+
The `size` parameter that is used in {es} searches when the {dfeed} does not use
1320+
aggregations. The default value is `1000`. The maximum value is the value of
13021321
`index.max_result_window` which is 10,000 by default.
13031322
end::scroll-size[]
13041323

0 commit comments

Comments
 (0)