Skip to content

Commit

Permalink
Avoid deprecation warning when running the ML datafeed extractor. (#3…
Browse files Browse the repository at this point in the history
…1463)

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.
  • Loading branch information
jpountz committed Jun 22, 2018
1 parent c72eea5 commit ec6dd09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ public Object[] value(SearchHit hit) {
if (value.length != 1) {
return value;
}
value[0] = ((BaseDateTime) value[0]).getMillis();
if (value[0] instanceof String) { // doc_value field with the epoch_millis format
value[0] = Long.parseLong((String) value[0]);
} else if (value[0] instanceof BaseDateTime) { // script field
value[0] = ((BaseDateTime) value[0]).getMillis();
} else {
throw new IllegalStateException("Unexpected value for a time field: " + value[0].getClass());
}
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.script.Script;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.StoredFieldsContext;
import org.elasticsearch.search.fetch.subphase.DocValueFieldsContext;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.xpack.core.ClientHelper;
import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor;
Expand Down Expand Up @@ -46,6 +47,7 @@ class ScrollDataExtractor implements DataExtractor {

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

private final Client client;
private final ScrollDataExtractorContext context;
Expand Down Expand Up @@ -114,7 +116,11 @@ private SearchRequestBuilder buildSearchRequest(long start) {
context.query, context.extractedFields.timeField(), start, context.end));

for (String docValueField : context.extractedFields.getDocValueFields()) {
searchRequestBuilder.addDocValueField(docValueField);
if (docValueField.equals(context.extractedFields.timeField())) {
searchRequestBuilder.addDocValueField(docValueField, EPOCH_MILLIS_FORMAT);
} else {
searchRequestBuilder.addDocValueField(docValueField, DocValueFieldsContext.USE_DEFAULT_FORMAT);
}
}
String[] sourceFields = context.extractedFields.getSourceFields();
if (sourceFields.length == 0) {
Expand Down

0 comments on commit ec6dd09

Please sign in to comment.