Skip to content

Commit 7168cd6

Browse files
committed
Add a focused test for the mapping
1 parent 196e057 commit 7168cd6

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/TransportDownsampleActionTests.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,47 @@
77

88
package org.elasticsearch.xpack.downsample;
99

10+
import org.elasticsearch.action.downsample.DownsampleConfig;
1011
import org.elasticsearch.cluster.metadata.IndexMetadata;
1112
import org.elasticsearch.cluster.routing.allocation.DataTier;
1213
import org.elasticsearch.common.settings.IndexScopedSettings;
1314
import org.elasticsearch.common.settings.Settings;
15+
import org.elasticsearch.common.xcontent.XContentHelper;
1416
import org.elasticsearch.index.IndexSettings;
1517
import org.elasticsearch.index.IndexVersion;
1618
import org.elasticsearch.index.mapper.TimeSeriesParams;
19+
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
1720
import org.elasticsearch.test.ESTestCase;
21+
import org.elasticsearch.xcontent.json.JsonXContent;
1822
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
1923

24+
import java.io.IOException;
2025
import java.util.List;
2126
import java.util.Map;
2227
import java.util.UUID;
2328

29+
import static org.hamcrest.Matchers.equalTo;
2430
import static org.hamcrest.Matchers.is;
2531

2632
public class TransportDownsampleActionTests extends ESTestCase {
33+
private static final Map<String, Object> METRIC_COUNTER_FIELD = Map.of("type", "long", "time_series_metric", "counter");
34+
private static final Map<String, Object> METRIC_GAUGE_FIELD = Map.of("type", "double", "time_series_metric", "gauge");
35+
private static final Map<String, Object> DIMENSION_FIELD = Map.of("type", "keyword", "time_series_dimension", true);
36+
private static final Map<String, Object> FLOAT_FIELD = Map.of("type", "float", "scaling_factor", 1000.0);
37+
private static final Map<String, Object> DATE_FIELD = Map.of("type", "date");
38+
private static final Map<String, Object> FLATTENED_FIELD = Map.of("type", "flattened");
39+
private static final Map<String, Object> PASSTHROUGH_FIELD = Map.of("type", "passthrough", "time_series_dimension", true);
40+
private static final Map<String, Object> AGGREGATE_METRIC_DOUBLE_FIELD = Map.of(
41+
"type",
42+
"aggregate_metric_double",
43+
"time_series_metric",
44+
"gauge",
45+
"metrics",
46+
List.of("max", "min", "value_count", "sum"),
47+
"default_metric",
48+
"max"
49+
);
50+
2751
public void testCopyIndexMetadata() {
2852
// GIVEN
2953
final List<String> tiers = List.of(DataTier.DATA_HOT, DataTier.DATA_WARM, DataTier.DATA_COLD, DataTier.DATA_CONTENT);
@@ -132,4 +156,47 @@ public void testGetSupportedMetrics() {
132156
assertThat(supported.defaultMetric(), is("max"));
133157
assertThat(supported.supportedMetrics(), is(List.of(metricType.supportedAggs())));
134158
}
159+
160+
@SuppressWarnings("unchecked")
161+
public void testDownsampledMapping() throws IOException {
162+
TimeseriesFieldTypeHelper helper = new TimeseriesFieldTypeHelper.Builder(null).build("@timestamp");
163+
String downsampledMappingStr = TransportDownsampleAction.createDownsampleIndexMapping(
164+
helper,
165+
new DownsampleConfig(new DateHistogramInterval("5m")),
166+
Map.of(
167+
"properties",
168+
Map.of(
169+
"my-dimension",
170+
DIMENSION_FIELD,
171+
"my-passthrough",
172+
PASSTHROUGH_FIELD,
173+
"my-multi-field",
174+
Map.of("type", "keyword", "fields", Map.of("my-gauge", METRIC_GAUGE_FIELD)),
175+
"my-object",
176+
Map.of("type", "text", "properties", Map.of("my-float", FLOAT_FIELD, "my-counter", METRIC_COUNTER_FIELD)),
177+
"my-flattened",
178+
FLATTENED_FIELD,
179+
"@timestamp",
180+
DATE_FIELD
181+
)
182+
)
183+
);
184+
Map<String, Object> downsampledMapping = XContentHelper.convertToMap(JsonXContent.jsonXContent, downsampledMappingStr, false);
185+
assertThat(downsampledMapping.containsKey("properties"), is(true));
186+
Map<String, Object> properties = (Map<String, Object>) downsampledMapping.get("properties");
187+
assertThat(properties.get("my-dimension"), equalTo(DIMENSION_FIELD));
188+
assertThat(properties.get("my-passthrough"), equalTo(PASSTHROUGH_FIELD));
189+
assertThat(properties.get("my-flattened"), equalTo(FLATTENED_FIELD));
190+
Map<String, Object> timestamp = (Map<String, Object>) properties.get("@timestamp");
191+
assertThat(timestamp.get("type"), equalTo("date"));
192+
assertThat(timestamp.get("meta"), equalTo(Map.of("fixed_interval", "5m", "time_zone", "UTC")));
193+
Map<String, Object> multiField = (Map<String, Object>) properties.get("my-multi-field");
194+
assertThat(multiField.get("type"), equalTo("keyword"));
195+
assertThat(((Map<String, Object>) multiField.get("fields")).get("my-gauge"), equalTo(AGGREGATE_METRIC_DOUBLE_FIELD));
196+
Map<String, Object> objectField = (Map<String, Object>) properties.get("my-object");
197+
assertThat(objectField.get("type"), equalTo("text"));
198+
Map<String, Object> subObjectsProperties = (Map<String, Object>) objectField.get("properties");
199+
assertThat(subObjectsProperties.get("my-float"), equalTo(FLOAT_FIELD));
200+
assertThat(subObjectsProperties.get("my-counter"), equalTo(METRIC_COUNTER_FIELD));
201+
}
135202
}

0 commit comments

Comments
 (0)