8
8
import com .carrotsearch .randomizedtesting .generators .CodepointSetGenerator ;
9
9
10
10
import org .elasticsearch .ElasticsearchException ;
11
+ import org .elasticsearch .Version ;
12
+ import org .elasticsearch .common .bytes .BytesReference ;
13
+ import org .elasticsearch .common .io .stream .BytesStreamOutput ;
14
+ import org .elasticsearch .common .io .stream .NamedWriteableAwareStreamInput ;
11
15
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
16
+ import org .elasticsearch .common .io .stream .StreamInput ;
12
17
import org .elasticsearch .common .io .stream .Writeable ;
13
18
import org .elasticsearch .common .settings .Settings ;
14
19
import org .elasticsearch .common .unit .TimeValue ;
15
20
import org .elasticsearch .common .xcontent .DeprecationHandler ;
16
21
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
17
22
import org .elasticsearch .common .xcontent .XContentFactory ;
23
+ import org .elasticsearch .common .xcontent .XContentHelper ;
18
24
import org .elasticsearch .common .xcontent .XContentParseException ;
19
25
import org .elasticsearch .common .xcontent .XContentParser ;
20
26
import org .elasticsearch .common .xcontent .XContentType ;
21
27
import org .elasticsearch .index .query .BoolQueryBuilder ;
28
+ import org .elasticsearch .index .query .QueryBuilder ;
22
29
import org .elasticsearch .index .query .QueryBuilders ;
23
30
import org .elasticsearch .index .query .TermQueryBuilder ;
24
31
import org .elasticsearch .script .Script ;
25
32
import org .elasticsearch .search .SearchModule ;
26
33
import org .elasticsearch .search .aggregations .AggregationBuilders ;
27
34
import org .elasticsearch .search .aggregations .AggregatorFactories ;
35
+ import org .elasticsearch .search .aggregations .pipeline .PipelineAggregatorBuilders ;
28
36
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramAggregationBuilder ;
29
37
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramInterval ;
30
38
import org .elasticsearch .search .aggregations .bucket .terms .TermsAggregationBuilder ;
31
39
import org .elasticsearch .search .aggregations .metrics .avg .AvgAggregationBuilder ;
32
40
import org .elasticsearch .search .aggregations .metrics .max .MaxAggregationBuilder ;
41
+ import org .elasticsearch .search .aggregations .pipeline .bucketscript .BucketScriptPipelineAggregationBuilder ;
42
+ import org .elasticsearch .search .aggregations .pipeline .derivative .DerivativePipelineAggregationBuilder ;
33
43
import org .elasticsearch .search .builder .SearchSourceBuilder ;
34
44
import org .elasticsearch .search .builder .SearchSourceBuilder .ScriptField ;
35
45
import org .elasticsearch .test .AbstractSerializingTestCase ;
@@ -83,7 +93,7 @@ public static DatafeedConfig createRandomizedDatafeedConfig(String jobId, long b
83
93
if (randomBoolean () && addScriptFields == false ) {
84
94
// can only test with a single agg as the xcontent order gets randomized by test base class and then
85
95
// the actual xcontent isn't the same and test fail.
86
- // Testing with a single agg is ok as we don't have special list writeable / xconent logic
96
+ // Testing with a single agg is ok as we don't have special list writeable / xcontent logic
87
97
AggregatorFactories .Builder aggs = new AggregatorFactories .Builder ();
88
98
aggHistogramInterval = randomNonNegativeLong ();
89
99
aggHistogramInterval = aggHistogramInterval > bucketSpanMillis ? bucketSpanMillis : aggHistogramInterval ;
@@ -567,6 +577,98 @@ public void testDefaultFrequency_GivenAggregationsWithHistogramInterval_1_Hour()
567
577
assertEquals (TimeValue .timeValueHours (1 ), datafeed .defaultFrequency (TimeValue .timeValueHours (12 )));
568
578
}
569
579
580
+ public void testSerializationOfComplexAggs () throws IOException {
581
+ MaxAggregationBuilder maxTime = AggregationBuilders .max ("timestamp" ).field ("timestamp" );
582
+ AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders .avg ("bytes_in_avg" ).field ("system.network.in.bytes" );
583
+ DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder =
584
+ PipelineAggregatorBuilders .derivative ("bytes_in_derivative" , "bytes_in_avg" );
585
+ BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder =
586
+ PipelineAggregatorBuilders .bucketScript ("non_negative_bytes" ,
587
+ Collections .singletonMap ("bytes" , "bytes_in_derivative" ),
588
+ new Script ("params.bytes > 0 ? params.bytes : null" ));
589
+ DateHistogramAggregationBuilder dateHistogram =
590
+ AggregationBuilders .dateHistogram ("histogram_buckets" )
591
+ .field ("timestamp" ).interval (300000 ).timeZone (DateTimeZone .UTC )
592
+ .subAggregation (maxTime )
593
+ .subAggregation (avgAggregationBuilder )
594
+ .subAggregation (derivativePipelineAggregationBuilder )
595
+ .subAggregation (bucketScriptPipelineAggregationBuilder );
596
+ DatafeedConfig .Builder datafeedConfigBuilder = createDatafeedBuilderWithDateHistogram (dateHistogram );
597
+ QueryBuilder terms =
598
+ new BoolQueryBuilder ().filter (new TermQueryBuilder (randomAlphaOfLengthBetween (1 , 10 ), randomAlphaOfLengthBetween (1 , 10 )));
599
+ datafeedConfigBuilder .setParsedQuery (terms );
600
+ DatafeedConfig datafeedConfig = datafeedConfigBuilder .build ();
601
+ AggregatorFactories .Builder aggBuilder = new AggregatorFactories .Builder ().addAggregator (dateHistogram );
602
+
603
+
604
+ XContentType xContentType = XContentType .JSON ;
605
+ BytesReference bytes = XContentHelper .toXContent (datafeedConfig , xContentType , false );
606
+ XContentParser parser = XContentHelper .createParser (xContentRegistry (),
607
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
608
+ bytes ,
609
+ xContentType );
610
+
611
+ DatafeedConfig parsedDatafeedConfig = doParseInstance (parser );
612
+ assertEquals (datafeedConfig , parsedDatafeedConfig );
613
+
614
+ // Assert that the parsed versions of our aggs and queries work as well
615
+ assertEquals (aggBuilder , parsedDatafeedConfig .getParsedAggregations ());
616
+ assertEquals (terms , parsedDatafeedConfig .getParsedQuery ());
617
+
618
+ try (BytesStreamOutput output = new BytesStreamOutput ()) {
619
+ datafeedConfig .writeTo (output );
620
+ try (StreamInput streamInput = output .bytes ().streamInput ()) {
621
+ DatafeedConfig streamedDatafeedConfig = new DatafeedConfig (streamInput );
622
+ assertEquals (datafeedConfig , streamedDatafeedConfig );
623
+
624
+ // Assert that the parsed versions of our aggs and queries work as well
625
+ assertEquals (aggBuilder , streamedDatafeedConfig .getParsedAggregations ());
626
+ assertEquals (terms , streamedDatafeedConfig .getParsedQuery ());
627
+ }
628
+ }
629
+ }
630
+
631
+ public void testSerializationOfComplexAggsBetweenVersions () throws IOException {
632
+ MaxAggregationBuilder maxTime = AggregationBuilders .max ("timestamp" ).field ("timestamp" );
633
+ AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders .avg ("bytes_in_avg" ).field ("system.network.in.bytes" );
634
+ DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder =
635
+ PipelineAggregatorBuilders .derivative ("bytes_in_derivative" , "bytes_in_avg" );
636
+ BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder =
637
+ PipelineAggregatorBuilders .bucketScript ("non_negative_bytes" ,
638
+ Collections .singletonMap ("bytes" , "bytes_in_derivative" ),
639
+ new Script ("params.bytes > 0 ? params.bytes : null" ));
640
+ DateHistogramAggregationBuilder dateHistogram =
641
+ AggregationBuilders .dateHistogram ("histogram_buckets" )
642
+ .field ("timestamp" ).interval (300000 ).timeZone (DateTimeZone .UTC )
643
+ .subAggregation (maxTime )
644
+ .subAggregation (avgAggregationBuilder )
645
+ .subAggregation (derivativePipelineAggregationBuilder )
646
+ .subAggregation (bucketScriptPipelineAggregationBuilder );
647
+ DatafeedConfig .Builder datafeedConfigBuilder = createDatafeedBuilderWithDateHistogram (dateHistogram );
648
+ QueryBuilder terms =
649
+ new BoolQueryBuilder ().filter (new TermQueryBuilder (randomAlphaOfLengthBetween (1 , 10 ), randomAlphaOfLengthBetween (1 , 10 )));
650
+ datafeedConfigBuilder .setParsedQuery (terms );
651
+ DatafeedConfig datafeedConfig = datafeedConfigBuilder .build ();
652
+
653
+ SearchModule searchModule = new SearchModule (Settings .EMPTY , false , Collections .emptyList ());
654
+ NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry (searchModule .getNamedWriteables ());
655
+
656
+ try (BytesStreamOutput output = new BytesStreamOutput ()) {
657
+ output .setVersion (Version .V_6_0_0 );
658
+ datafeedConfig .writeTo (output );
659
+ try (StreamInput in = new NamedWriteableAwareStreamInput (output .bytes ().streamInput (), namedWriteableRegistry )) {
660
+ in .setVersion (Version .V_6_0_0 );
661
+ DatafeedConfig streamedDatafeedConfig = new DatafeedConfig (in );
662
+ assertEquals (datafeedConfig , streamedDatafeedConfig );
663
+
664
+ // Assert that the parsed versions of our aggs and queries work as well
665
+ assertEquals (new AggregatorFactories .Builder ().addAggregator (dateHistogram ),
666
+ streamedDatafeedConfig .getParsedAggregations ());
667
+ assertEquals (terms , streamedDatafeedConfig .getParsedQuery ());
668
+ }
669
+ }
670
+ }
671
+
570
672
public static String randomValidDatafeedId () {
571
673
CodepointSetGenerator generator = new CodepointSetGenerator ("abcdefghijklmnopqrstuvwxyz" .toCharArray ());
572
674
return generator .ofCodePointsLength (random (), 10 , 10 );
@@ -590,14 +692,18 @@ private static DatafeedConfig createDatafeedWithDateHistogram(Long interval) {
590
692
return createDatafeedWithDateHistogram (dateHistogram );
591
693
}
592
694
593
- private static DatafeedConfig createDatafeedWithDateHistogram (DateHistogramAggregationBuilder dateHistogram ) {
695
+ private static DatafeedConfig . Builder createDatafeedBuilderWithDateHistogram (DateHistogramAggregationBuilder dateHistogram ) {
594
696
DatafeedConfig .Builder builder = new DatafeedConfig .Builder ("datafeed1" , "job1" );
595
697
builder .setIndices (Collections .singletonList ("myIndex" ));
596
698
builder .setTypes (Collections .singletonList ("myType" ));
597
699
AggregatorFactories .Builder aggs = new AggregatorFactories .Builder ().addAggregator (dateHistogram );
598
700
DatafeedConfig .validateAggregations (aggs );
599
701
builder .setParsedAggregations (aggs );
600
- return builder .build ();
702
+ return builder ;
703
+ }
704
+
705
+ private static DatafeedConfig createDatafeedWithDateHistogram (DateHistogramAggregationBuilder dateHistogram ) {
706
+ return createDatafeedBuilderWithDateHistogram (dateHistogram ).build ();
601
707
}
602
708
603
709
@ Override
0 commit comments