Handle pre-6.x time fields (#34373)

In ccb9ab5717 we changed how we deal with time
fields to support the `DateTime`-format fields added in 6.0, but dropped
support for pre-6.x `Long`-format fields. This change reinstates this support
for cases where pre-6.x data is made available to ML (e.g. in a mixed-version
CCS setup or after an upgrade).
This commit is contained in:
David Turner 2018-10-11 15:33:09 +01:00 committed by GitHub
parent 6d99d7dafc
commit 7352f0da60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 7 deletions

View File

@ -107,7 +107,7 @@ abstract class ExtractedField {
value[0] = Long.parseLong((String) value[0]); value[0] = Long.parseLong((String) value[0]);
} else if (value[0] instanceof BaseDateTime) { // script field } else if (value[0] instanceof BaseDateTime) { // script field
value[0] = ((BaseDateTime) value[0]).getMillis(); value[0] = ((BaseDateTime) value[0]).getMillis();
} else { } else if (value[0] instanceof Long == false) { // pre-6.0 field
throw new IllegalStateException("Unexpected value for a time field: " + value[0].getClass()); throw new IllegalStateException("Unexpected value for a time field: " + value[0].getClass());
} }
return value; return value;

View File

@ -13,6 +13,7 @@ import org.joda.time.DateTime;
import java.util.Arrays; import java.util.Arrays;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
public class ExtractedFieldTests extends ESTestCase { public class ExtractedFieldTests extends ESTestCase {
@ -96,11 +97,32 @@ public class ExtractedFieldTests extends ESTestCase {
} }
public void testValueGivenTimeField() { public void testValueGivenTimeField() {
SearchHit hit = new SearchHitBuilder(42).addField("time", new DateTime(123456789L)).build(); final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", new DateTime(millis)).build();
final ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE);
assertThat(timeField.value(hit), equalTo(new Object[] { millis }));
}
ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE); public void testValueGivenStringTimeField() {
final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", Long.toString(millis)).build();
final ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE);
assertThat(timeField.value(hit), equalTo(new Object[] { millis }));
}
assertThat(timeField.value(hit), equalTo(new Object[] { 123456789L })); public void testValueGivenPre6xTimeField() {
// Prior to 6.x, timestamps were simply `long` milliseconds-past-the-epoch values
final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", millis).build();
final ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE);
assertThat(timeField.value(hit), equalTo(new Object[] { millis }));
}
public void testValueGivenUnknownFormatTimeField() {
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", new Object()).build();
final ExtractedField timeField = ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.DOC_VALUE);
assertThat(expectThrows(IllegalStateException.class, () -> timeField.value(hit)).getMessage(),
startsWith("Unexpected value for a time field"));
} }
public void testAliasVersusName() { public void testAliasVersusName() {

View File

@ -64,11 +64,25 @@ public class ExtractedFieldsTests extends ESTestCase {
} }
public void testTimeFieldValue() { public void testTimeFieldValue() {
SearchHit hit = new SearchHitBuilder(1).addField("time", new DateTime(1000L)).build(); final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", new DateTime(millis)).build();
final ExtractedFields extractedFields = new ExtractedFields(timeField, Collections.singletonList(timeField));
assertThat(extractedFields.timeFieldValue(hit), equalTo(millis));
}
ExtractedFields extractedFields = new ExtractedFields(timeField, Arrays.asList(timeField)); public void testStringTimeFieldValue() {
final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", Long.toString(millis)).build();
final ExtractedFields extractedFields = new ExtractedFields(timeField, Collections.singletonList(timeField));
assertThat(extractedFields.timeFieldValue(hit), equalTo(millis));
}
assertThat(extractedFields.timeFieldValue(hit), equalTo(1000L)); public void testPre6xTimeFieldValue() {
// Prior to 6.x, timestamps were simply `long` milliseconds-past-the-epoch values
final long millis = randomLong();
final SearchHit hit = new SearchHitBuilder(randomInt()).addField("time", millis).build();
final ExtractedFields extractedFields = new ExtractedFields(timeField, Collections.singletonList(timeField));
assertThat(extractedFields.timeFieldValue(hit), equalTo(millis));
} }
public void testTimeFieldValueGivenEmptyArray() { public void testTimeFieldValueGivenEmptyArray() {