@@ -52,7 +52,7 @@ public void teardown() {
52
52
}
53
53
54
54
public void testValidStarTree () throws IOException {
55
- MapperService mapperService = createMapperService (getExpandedMapping ("status" , "size" ));
55
+ MapperService mapperService = createMapperService (getExpandedMappingWithJustAvg ("status" , "size" ));
56
56
Set <CompositeMappedFieldType > compositeFieldTypes = mapperService .getCompositeFieldTypes ();
57
57
for (CompositeMappedFieldType type : compositeFieldTypes ) {
58
58
StarTreeMapper .StarTreeFieldType starTreeFieldType = (StarTreeMapper .StarTreeFieldType ) type ;
@@ -66,7 +66,37 @@ public void testValidStarTree() throws IOException {
66
66
assertEquals (expectedTimeUnits , dateDim .getIntervals ());
67
67
assertEquals ("status" , starTreeFieldType .getDimensions ().get (1 ).getField ());
68
68
assertEquals ("size" , starTreeFieldType .getMetrics ().get (0 ).getField ());
69
- List <MetricStat > expectedMetrics = Arrays .asList (MetricStat .SUM , MetricStat .AVG );
69
+
70
+ // Assert COUNT and SUM gets added when AVG is mentioned
71
+ List <MetricStat > expectedMetrics = Arrays .asList (MetricStat .COUNT , MetricStat .SUM );
72
+ assertEquals (expectedMetrics , starTreeFieldType .getMetrics ().get (0 ).getMetrics ());
73
+ assertEquals (100 , starTreeFieldType .getStarTreeConfig ().maxLeafDocs ());
74
+ assertEquals (StarTreeFieldConfiguration .StarTreeBuildMode .OFF_HEAP , starTreeFieldType .getStarTreeConfig ().getBuildMode ());
75
+ assertEquals (
76
+ new HashSet <>(Arrays .asList ("@timestamp" , "status" )),
77
+ starTreeFieldType .getStarTreeConfig ().getSkipStarNodeCreationInDims ()
78
+ );
79
+ }
80
+ }
81
+
82
+ public void testMetricsWithJustSum () throws IOException {
83
+ MapperService mapperService = createMapperService (getExpandedMappingWithJustSum ("status" , "size" ));
84
+ Set <CompositeMappedFieldType > compositeFieldTypes = mapperService .getCompositeFieldTypes ();
85
+ for (CompositeMappedFieldType type : compositeFieldTypes ) {
86
+ StarTreeMapper .StarTreeFieldType starTreeFieldType = (StarTreeMapper .StarTreeFieldType ) type ;
87
+ assertEquals ("@timestamp" , starTreeFieldType .getDimensions ().get (0 ).getField ());
88
+ assertTrue (starTreeFieldType .getDimensions ().get (0 ) instanceof DateDimension );
89
+ DateDimension dateDim = (DateDimension ) starTreeFieldType .getDimensions ().get (0 );
90
+ List <Rounding .DateTimeUnit > expectedTimeUnits = Arrays .asList (
91
+ Rounding .DateTimeUnit .DAY_OF_MONTH ,
92
+ Rounding .DateTimeUnit .MONTH_OF_YEAR
93
+ );
94
+ assertEquals (expectedTimeUnits , dateDim .getIntervals ());
95
+ assertEquals ("status" , starTreeFieldType .getDimensions ().get (1 ).getField ());
96
+ assertEquals ("size" , starTreeFieldType .getMetrics ().get (0 ).getField ());
97
+
98
+ // Assert COUNT gets added for any metric field
99
+ List <MetricStat > expectedMetrics = Arrays .asList (MetricStat .COUNT , MetricStat .SUM );
70
100
assertEquals (expectedMetrics , starTreeFieldType .getMetrics ().get (0 ).getMetrics ());
71
101
assertEquals (100 , starTreeFieldType .getStarTreeConfig ().maxLeafDocs ());
72
102
assertEquals (StarTreeFieldConfiguration .StarTreeBuildMode .OFF_HEAP , starTreeFieldType .getStarTreeConfig ().getBuildMode ());
@@ -92,13 +122,7 @@ public void testValidStarTreeDefaults() throws IOException {
92
122
assertEquals (expectedTimeUnits , dateDim .getIntervals ());
93
123
assertEquals ("status" , starTreeFieldType .getDimensions ().get (1 ).getField ());
94
124
assertEquals ("status" , starTreeFieldType .getMetrics ().get (0 ).getField ());
95
- List <MetricStat > expectedMetrics = Arrays .asList (
96
- MetricStat .AVG ,
97
- MetricStat .COUNT ,
98
- MetricStat .SUM ,
99
- MetricStat .MAX ,
100
- MetricStat .MIN
101
- );
125
+ List <MetricStat > expectedMetrics = Arrays .asList (MetricStat .COUNT , MetricStat .SUM );
102
126
assertEquals (expectedMetrics , starTreeFieldType .getMetrics ().get (0 ).getMetrics ());
103
127
assertEquals (10000 , starTreeFieldType .getStarTreeConfig ().maxLeafDocs ());
104
128
assertEquals (StarTreeFieldConfiguration .StarTreeBuildMode .OFF_HEAP , starTreeFieldType .getStarTreeConfig ().getBuildMode ());
@@ -109,15 +133,15 @@ public void testValidStarTreeDefaults() throws IOException {
109
133
public void testInvalidDim () {
110
134
MapperParsingException ex = expectThrows (
111
135
MapperParsingException .class ,
112
- () -> createMapperService (getExpandedMapping ("invalid" , "size" ))
136
+ () -> createMapperService (getExpandedMappingWithJustAvg ("invalid" , "size" ))
113
137
);
114
138
assertEquals ("Failed to parse mapping [_doc]: unknown dimension field [invalid]" , ex .getMessage ());
115
139
}
116
140
117
141
public void testInvalidMetric () {
118
142
MapperParsingException ex = expectThrows (
119
143
MapperParsingException .class ,
120
- () -> createMapperService (getExpandedMapping ("status" , "invalid" ))
144
+ () -> createMapperService (getExpandedMappingWithJustAvg ("status" , "invalid" ))
121
145
);
122
146
assertEquals ("Failed to parse mapping [_doc]: unknown metric field [invalid]" , ex .getMessage ());
123
147
}
@@ -232,6 +256,10 @@ public void testMetric() {
232
256
assertEquals (MetricStat .MIN , MetricStat .fromTypeName ("min" ));
233
257
assertEquals (MetricStat .SUM , MetricStat .fromTypeName ("sum" ));
234
258
assertEquals (MetricStat .AVG , MetricStat .fromTypeName ("avg" ));
259
+
260
+ assertEquals (Set .of (MetricStat .COUNT ), MetricStat .getRequiredMetrics ());
261
+ assertEquals (List .of (MetricStat .COUNT , MetricStat .SUM ), MetricStat .AVG .getDerivedFromMetrics ());
262
+
235
263
IllegalArgumentException ex = expectThrows (IllegalArgumentException .class , () -> MetricStat .fromTypeName ("invalid" ));
236
264
assertEquals ("Invalid metric stat: invalid" , ex .getMessage ());
237
265
}
@@ -310,7 +338,7 @@ public void testStarTreeField() {
310
338
}
311
339
312
340
public void testValidations () throws IOException {
313
- MapperService mapperService = createMapperService (getExpandedMapping ("status" , "size" ));
341
+ MapperService mapperService = createMapperService (getExpandedMappingWithJustAvg ("status" , "size" ));
314
342
Settings settings = Settings .builder ().put (CompositeIndexSettings .STAR_TREE_INDEX_ENABLED_SETTING .getKey (), true ).build ();
315
343
CompositeIndexSettings enabledCompositeIndexSettings = new CompositeIndexSettings (
316
344
settings ,
@@ -370,7 +398,7 @@ public void testValidations() throws IOException {
370
398
);
371
399
}
372
400
373
- private XContentBuilder getExpandedMapping (String dim , String metric ) throws IOException {
401
+ private XContentBuilder getExpandedMappingWithJustAvg (String dim , String metric ) throws IOException {
374
402
return topMapping (b -> {
375
403
b .startObject ("composite" );
376
404
b .startObject ("startree" );
@@ -399,7 +427,6 @@ private XContentBuilder getExpandedMapping(String dim, String metric) throws IOE
399
427
b .startObject ();
400
428
b .field ("name" , metric );
401
429
b .startArray ("stats" );
402
- b .value ("sum" );
403
430
b .value ("avg" );
404
431
b .endArray ();
405
432
b .endObject ();
@@ -421,6 +448,56 @@ private XContentBuilder getExpandedMapping(String dim, String metric) throws IOE
421
448
});
422
449
}
423
450
451
+ private XContentBuilder getExpandedMappingWithJustSum (String dim , String metric ) throws IOException {
452
+ return topMapping (b -> {
453
+ b .startObject ("composite" );
454
+ b .startObject ("startree" );
455
+ b .field ("type" , "star_tree" );
456
+ b .startObject ("config" );
457
+ b .field ("max_leaf_docs" , 100 );
458
+ b .startArray ("skip_star_node_creation_for_dimensions" );
459
+ {
460
+ b .value ("@timestamp" );
461
+ b .value ("status" );
462
+ }
463
+ b .endArray ();
464
+ b .startArray ("ordered_dimensions" );
465
+ b .startObject ();
466
+ b .field ("name" , "@timestamp" );
467
+ b .startArray ("calendar_intervals" );
468
+ b .value ("day" );
469
+ b .value ("month" );
470
+ b .endArray ();
471
+ b .endObject ();
472
+ b .startObject ();
473
+ b .field ("name" , dim );
474
+ b .endObject ();
475
+ b .endArray ();
476
+ b .startArray ("metrics" );
477
+ b .startObject ();
478
+ b .field ("name" , metric );
479
+ b .startArray ("stats" );
480
+ b .value ("sum" );
481
+ b .endArray ();
482
+ b .endObject ();
483
+ b .endArray ();
484
+ b .endObject ();
485
+ b .endObject ();
486
+ b .endObject ();
487
+ b .startObject ("properties" );
488
+ b .startObject ("@timestamp" );
489
+ b .field ("type" , "date" );
490
+ b .endObject ();
491
+ b .startObject ("status" );
492
+ b .field ("type" , "integer" );
493
+ b .endObject ();
494
+ b .startObject ("size" );
495
+ b .field ("type" , "integer" );
496
+ b .endObject ();
497
+ b .endObject ();
498
+ });
499
+ }
500
+
424
501
private XContentBuilder getMinMapping () throws IOException {
425
502
return getMinMapping (false , false , false , false );
426
503
}
0 commit comments