@@ -142,6 +142,36 @@ public StdDevSamp stdDevSamp() {
142
142
return usesFieldRef () ? StdDevSamp .stdDevSampOf (fieldReference ) : StdDevSamp .stdDevSampOf (expression );
143
143
}
144
144
145
+ /**
146
+ * Creates new {@link AggregationExpression} that calculates the exponential moving average of numeric values
147
+ * considering the given number of historical documents with significant weight.
148
+ *
149
+ * @param numberOfHistoricalDocuments the number of historical documents.
150
+ * @return new instance of {@link ExpMovingAvg}.
151
+ * @since 3.3
152
+ */
153
+ public ExpMovingAvg expMovingAvg (int numberOfHistoricalDocuments ) {
154
+
155
+ ExpMovingAvg expMovingAvg = usesFieldRef () ? ExpMovingAvg .expMovingAvgOf (fieldReference )
156
+ : ExpMovingAvg .expMovingAvgOf (expression );
157
+ return expMovingAvg .N (numberOfHistoricalDocuments );
158
+ }
159
+
160
+ /**
161
+ * Creates new {@link AggregationExpression} that calculates the exponential moving average of numeric values
162
+ * applying the given exponential decay value.
163
+ *
164
+ * @param exponentialDecayValue the decay value.
165
+ * @return new instance of {@link ExpMovingAvg}.
166
+ * @since 3.3
167
+ */
168
+ public ExpMovingAvg expMovingAvg (double exponentialDecayValue ) {
169
+
170
+ ExpMovingAvg expMovingAvg = usesFieldRef () ? ExpMovingAvg .expMovingAvgOf (fieldReference )
171
+ : ExpMovingAvg .expMovingAvgOf (expression );
172
+ return expMovingAvg .alpha (exponentialDecayValue );
173
+ }
174
+
145
175
private boolean usesFieldRef () {
146
176
return fieldReference != null ;
147
177
}
@@ -658,4 +688,65 @@ public Document toDocument(Object value, AggregationOperationContext context) {
658
688
return super .toDocument (value , context );
659
689
}
660
690
}
691
+
692
+ /**
693
+ * {@link ExpMovingAvg} calculates the exponential moving average of numeric values.
694
+ *
695
+ * @author Christoph Strobl
696
+ * @since 3.3
697
+ */
698
+ public static class ExpMovingAvg extends AbstractAggregationExpression {
699
+
700
+ private ExpMovingAvg (Object value ) {
701
+ super (value );
702
+ }
703
+
704
+ /**
705
+ * Create a new {@link ExpMovingAvg} by defining the field holding the value to be used as input.
706
+ *
707
+ * @param fieldReference must not be {@literal null}.
708
+ * @return new instance of {@link ExpMovingAvg}.
709
+ */
710
+ public static ExpMovingAvg expMovingAvgOf (String fieldReference ) {
711
+ return new ExpMovingAvg (Collections .singletonMap ("input" , Fields .field (fieldReference )));
712
+ }
713
+
714
+ /**
715
+ * Create a new {@link ExpMovingAvg} by defining the {@link AggregationExpression expression} to compute the value
716
+ * to be used as input.
717
+ *
718
+ * @param expression must not be {@literal null}.
719
+ * @return new instance of {@link ExpMovingAvg}.
720
+ */
721
+ public static ExpMovingAvg expMovingAvgOf (AggregationExpression expression ) {
722
+ return new ExpMovingAvg (Collections .singletonMap ("input" , expression ));
723
+ }
724
+
725
+ /**
726
+ * Define the number of historical documents with significant mathematical weight. <br />
727
+ * Specify either {@link #N(int) N} or {@link #alpha(double) aplha}. Not both!
728
+ *
729
+ * @param numberOfHistoricalDocuments
730
+ * @return new instance of {@link ExpMovingAvg}.
731
+ */
732
+ public ExpMovingAvg N /*umber of historical documents*/ (int numberOfHistoricalDocuments ) {
733
+ return new ExpMovingAvg (append ("N" , numberOfHistoricalDocuments ));
734
+ }
735
+
736
+ /**
737
+ * Define the exponential decay value. <br />
738
+ * Specify either {@link #alpha(double) aplha} or {@link #N(int) N}. Not both!
739
+ *
740
+ * @param exponentialDecayValue
741
+ * @return new instance of {@link ExpMovingAvg}.
742
+ */
743
+ public ExpMovingAvg alpha (double exponentialDecayValue ) {
744
+ return new ExpMovingAvg (append ("alpha" , exponentialDecayValue ));
745
+ }
746
+
747
+ @ Override
748
+ protected String getMongoMethod () {
749
+ return "$expMovingAvg" ;
750
+ }
751
+ }
661
752
}
0 commit comments