Skip to content

Commit ec6dd09

Browse files
committed
Avoid deprecation warning when running the ML datafeed extractor. (#31463)
In #29639 we added a `format` option to doc-value fields and deprecated usage of doc-value fields without a format so that we could migrate doc-value fields to use the format that comes with the mappings by default. However I missed to fix the machine-learning datafeed extractor.
1 parent c72eea5 commit ec6dd09

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ExtractedField.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ public Object[] value(SearchHit hit) {
103103
if (value.length != 1) {
104104
return value;
105105
}
106-
value[0] = ((BaseDateTime) value[0]).getMillis();
106+
if (value[0] instanceof String) { // doc_value field with the epoch_millis format
107+
value[0] = Long.parseLong((String) value[0]);
108+
} else if (value[0] instanceof BaseDateTime) { // script field
109+
value[0] = ((BaseDateTime) value[0]).getMillis();
110+
} else {
111+
throw new IllegalStateException("Unexpected value for a time field: " + value[0].getClass());
112+
}
107113
return value;
108114
}
109115
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.script.Script;
2020
import org.elasticsearch.search.SearchHit;
2121
import org.elasticsearch.search.fetch.StoredFieldsContext;
22+
import org.elasticsearch.search.fetch.subphase.DocValueFieldsContext;
2223
import org.elasticsearch.search.sort.SortOrder;
2324
import org.elasticsearch.xpack.core.ClientHelper;
2425
import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor;
@@ -46,6 +47,7 @@ class ScrollDataExtractor implements DataExtractor {
4647

4748
private static final Logger LOGGER = Loggers.getLogger(ScrollDataExtractor.class);
4849
private static final TimeValue SCROLL_TIMEOUT = new TimeValue(30, TimeUnit.MINUTES);
50+
private static final String EPOCH_MILLIS_FORMAT = "epoch_millis";
4951

5052
private final Client client;
5153
private final ScrollDataExtractorContext context;
@@ -114,7 +116,11 @@ private SearchRequestBuilder buildSearchRequest(long start) {
114116
context.query, context.extractedFields.timeField(), start, context.end));
115117

116118
for (String docValueField : context.extractedFields.getDocValueFields()) {
117-
searchRequestBuilder.addDocValueField(docValueField);
119+
if (docValueField.equals(context.extractedFields.timeField())) {
120+
searchRequestBuilder.addDocValueField(docValueField, EPOCH_MILLIS_FORMAT);
121+
} else {
122+
searchRequestBuilder.addDocValueField(docValueField, DocValueFieldsContext.USE_DEFAULT_FORMAT);
123+
}
118124
}
119125
String[] sourceFields = context.extractedFields.getSourceFields();
120126
if (sourceFields.length == 0) {

0 commit comments

Comments
 (0)