|
@@ -25,12 +25,16 @@ import org.elasticsearch.cluster.ClusterState;
|
|
|
import org.elasticsearch.cluster.metadata.MetadataCreateDataStreamService.CreateDataStreamClusterStateUpdateRequest;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
+import org.elasticsearch.index.MapperTestUtils;
|
|
|
+import org.elasticsearch.index.mapper.MapperService;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static org.elasticsearch.cluster.DataStreamTestHelper.createFirstBackingIndex;
|
|
|
+import static org.elasticsearch.cluster.metadata.MetadataCreateDataStreamService.validateTimestampFieldMapping;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
@@ -146,6 +150,59 @@ public class MetadataCreateDataStreamServiceTests extends ESTestCase {
|
|
|
return MetadataCreateDataStreamService.createDataStream(metadataCreateIndexService, cs, req);
|
|
|
}
|
|
|
|
|
|
+ public void testValidateTimestampFieldMapping() throws Exception {
|
|
|
+ String mapping = generateMapping("@timestamp", "date");
|
|
|
+ validateTimestampFieldMapping("@timestamp", createMapperService(mapping));
|
|
|
+ mapping = generateMapping("@timestamp", "date_nanos");
|
|
|
+ validateTimestampFieldMapping("@timestamp", createMapperService(mapping));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testValidateTimestampFieldMappingNoFieldMapping() {
|
|
|
+ Exception e = expectThrows(IllegalArgumentException.class,
|
|
|
+ () -> validateTimestampFieldMapping("@timestamp", createMapperService("{}")));
|
|
|
+ assertThat(e.getMessage(),
|
|
|
+ equalTo("expected timestamp field [@timestamp], but found no timestamp field"));
|
|
|
+
|
|
|
+ String mapping = generateMapping("@timestamp2", "date");
|
|
|
+ e = expectThrows(IllegalArgumentException.class,
|
|
|
+ () -> validateTimestampFieldMapping("@timestamp", createMapperService(mapping)));
|
|
|
+ assertThat(e.getMessage(),
|
|
|
+ equalTo("expected timestamp field [@timestamp], but found no timestamp field"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testValidateTimestampFieldMappingInvalidFieldType() {
|
|
|
+ String mapping = generateMapping("@timestamp", "keyword");
|
|
|
+ Exception e = expectThrows(IllegalArgumentException.class,
|
|
|
+ () -> validateTimestampFieldMapping("@timestamp", createMapperService(mapping)));
|
|
|
+ assertThat(e.getMessage(), equalTo("expected timestamp field [@timestamp] to be of types [date, date_nanos], " +
|
|
|
+ "but instead found type [keyword]"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testValidateNestedTimestampFieldMapping() throws Exception {
|
|
|
+ String fieldType = randomBoolean() ? "date" : "date_nanos";
|
|
|
+ String mapping = "{\n" +
|
|
|
+ " \"properties\": {\n" +
|
|
|
+ " \"event\": {\n" +
|
|
|
+ " \"properties\": {\n" +
|
|
|
+ " \"@timestamp\": {\n" +
|
|
|
+ " \"type\": \"" + fieldType + "\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"another_field\": {\n" +
|
|
|
+ " \"type\": \"keyword\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }";
|
|
|
+ MapperService mapperService = createMapperService(mapping);
|
|
|
+
|
|
|
+ validateTimestampFieldMapping("event.@timestamp", mapperService);
|
|
|
+ Exception e = expectThrows(IllegalArgumentException.class,
|
|
|
+ () -> validateTimestampFieldMapping("event.another_field", mapperService));
|
|
|
+ assertThat(e.getMessage(), equalTo("expected timestamp field [event.another_field] to be of types [date, date_nanos], " +
|
|
|
+ "but instead found type [keyword]"));
|
|
|
+ }
|
|
|
+
|
|
|
private static MetadataCreateIndexService getMetadataCreateIndexService() throws Exception {
|
|
|
MetadataCreateIndexService s = mock(MetadataCreateIndexService.class);
|
|
|
when(s.applyCreateIndexRequest(any(ClusterState.class), any(CreateIndexClusterStateUpdateRequest.class), anyBoolean()))
|
|
@@ -159,6 +216,7 @@ public class MetadataCreateDataStreamServiceTests extends ESTestCase {
|
|
|
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
|
|
|
.put(request.settings())
|
|
|
.build())
|
|
|
+ .putMapping(generateMapping("@timestamp"))
|
|
|
.numberOfShards(1)
|
|
|
.numberOfReplicas(1)
|
|
|
.build(), false);
|
|
@@ -168,4 +226,32 @@ public class MetadataCreateDataStreamServiceTests extends ESTestCase {
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
+ public static String generateMapping(String timestampFieldName) {
|
|
|
+ return generateMapping(timestampFieldName, "date");
|
|
|
+ }
|
|
|
+
|
|
|
+ static String generateMapping(String timestampFieldName, String type) {
|
|
|
+ return "{\n" +
|
|
|
+ " \"properties\": {\n" +
|
|
|
+ " \"" + timestampFieldName + "\": {\n" +
|
|
|
+ " \"type\": \"" + type + "\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }";
|
|
|
+ }
|
|
|
+
|
|
|
+ MapperService createMapperService(String mapping) throws IOException {
|
|
|
+ String indexName = "test";
|
|
|
+ IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
|
|
|
+ .settings(Settings.builder()
|
|
|
+ .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
|
|
|
+ .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
|
|
|
+ .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1))
|
|
|
+ .putMapping(mapping)
|
|
|
+ .build();
|
|
|
+ MapperService mapperService =
|
|
|
+ MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), Settings.EMPTY, indexName);
|
|
|
+ mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_UPDATE);
|
|
|
+ return mapperService;
|
|
|
+ }
|
|
|
}
|