|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.core.aggregation;
|
17 | 17 |
|
| 18 | +import java.util.Arrays; |
18 | 19 | import java.util.Collections;
|
| 20 | +import java.util.HashMap; |
19 | 21 | import java.util.List;
|
| 22 | +import java.util.Map; |
20 | 23 |
|
21 | 24 | import org.bson.Document;
|
22 | 25 | import org.springframework.util.Assert;
|
|
25 | 28 | * Gateway to {@literal accumulator} aggregation operations.
|
26 | 29 | *
|
27 | 30 | * @author Christoph Strobl
|
| 31 | + * @author Julia Lee |
28 | 32 | * @since 1.10
|
29 | 33 | * @soundtrack Rage Against The Machine - Killing In The Name
|
30 | 34 | */
|
@@ -52,6 +56,7 @@ public static AccumulatorOperatorFactory valueOf(AggregationExpression expressio
|
52 | 56 |
|
53 | 57 | /**
|
54 | 58 | * @author Christoph Strobl
|
| 59 | + * @author Julia Lee |
55 | 60 | */
|
56 | 61 | public static class AccumulatorOperatorFactory {
|
57 | 62 |
|
@@ -246,6 +251,20 @@ public ExpMovingAvg alpha(double exponentialDecayValue) {
|
246 | 251 | };
|
247 | 252 | }
|
248 | 253 |
|
| 254 | + /** |
| 255 | + * Creates new {@link AggregationExpression} that calculates the requested percentile(s) of the |
| 256 | + * associated numeric value expression. |
| 257 | + * |
| 258 | + * @return new instance of {@link Percentile}. |
| 259 | + * @param percentages must not be {@literal null}. |
| 260 | + * @since 4.2 |
| 261 | + */ |
| 262 | + public Percentile percentile(Double... percentages) { |
| 263 | + Percentile percentile = usesFieldRef() ? Percentile.percentileOf(fieldReference) |
| 264 | + : Percentile.percentileOf(expression); |
| 265 | + return percentile.percentages(percentages); |
| 266 | + } |
| 267 | + |
249 | 268 | private boolean usesFieldRef() {
|
250 | 269 | return fieldReference != null;
|
251 | 270 | }
|
@@ -977,4 +996,90 @@ protected String getMongoMethod() {
|
977 | 996 | return "$expMovingAvg";
|
978 | 997 | }
|
979 | 998 | }
|
| 999 | + |
| 1000 | + /** |
| 1001 | + * {@link AggregationExpression} for {@code $percentile}. |
| 1002 | + * |
| 1003 | + * @author Julia Lee |
| 1004 | + * @since 4.2 |
| 1005 | + */ |
| 1006 | + public static class Percentile extends AbstractAggregationExpression { |
| 1007 | + |
| 1008 | + private Percentile(Object value) { |
| 1009 | + super(value); |
| 1010 | + } |
| 1011 | + |
| 1012 | + /** |
| 1013 | + * Creates new {@link Percentile}. |
| 1014 | + * |
| 1015 | + * @param fieldReference must not be {@literal null}. |
| 1016 | + * @return new instance of {@link Percentile}. |
| 1017 | + */ |
| 1018 | + public static Percentile percentileOf(String fieldReference) { |
| 1019 | + |
| 1020 | + Assert.notNull(fieldReference, "FieldReference must not be null"); |
| 1021 | + Map<String, Object> fields = new HashMap<>(); |
| 1022 | + fields.put("input", Fields.field(fieldReference)); |
| 1023 | + fields.put("method", "approximate"); |
| 1024 | + return new Percentile(fields); |
| 1025 | + } |
| 1026 | + |
| 1027 | + /** |
| 1028 | + * Creates new {@link Percentile}. |
| 1029 | + * |
| 1030 | + * @param expression must not be {@literal null}. |
| 1031 | + * @return new instance of {@link Percentile}. |
| 1032 | + */ |
| 1033 | + public static Percentile percentileOf(AggregationExpression expression) { |
| 1034 | + |
| 1035 | + Assert.notNull(expression, "Expression must not be null"); |
| 1036 | + Map<String, Object> fields = new HashMap<>(); |
| 1037 | + fields.put("input", expression); |
| 1038 | + fields.put("method", "approximate"); |
| 1039 | + return new Percentile(fields); |
| 1040 | + } |
| 1041 | + |
| 1042 | + /** |
| 1043 | + * Define the percentile value(s) that must resolve to percentages in the range {@code 0.0 - 1.0} inclusive. |
| 1044 | + * |
| 1045 | + * @param percentages must not be {@literal null}. |
| 1046 | + * @return new instance of {@link Percentile}. |
| 1047 | + */ |
| 1048 | + public Percentile percentages(Double... percentages) { |
| 1049 | + |
| 1050 | + Assert.notEmpty(percentages, "Percentages must not be null or empty"); |
| 1051 | + return new Percentile(append("p", Arrays.asList(percentages))); |
| 1052 | + } |
| 1053 | + |
| 1054 | + /** |
| 1055 | + * Creates new {@link Percentile} with all previously added inputs appending the given one. <br /> |
| 1056 | + * <strong>NOTE:</strong> Only possible in {@code $project} stage. |
| 1057 | + * |
| 1058 | + * @param fieldReference must not be {@literal null}. |
| 1059 | + * @return new instance of {@link Percentile}. |
| 1060 | + */ |
| 1061 | + public Percentile and(String fieldReference) { |
| 1062 | + |
| 1063 | + Assert.notNull(fieldReference, "FieldReference must not be null"); |
| 1064 | + return new Percentile(appendTo("input", Fields.field(fieldReference))); |
| 1065 | + } |
| 1066 | + |
| 1067 | + /** |
| 1068 | + * Creates new {@link Percentile} with all previously added inputs appending the given one. <br /> |
| 1069 | + * <strong>NOTE:</strong> Only possible in {@code $project} stage. |
| 1070 | + * |
| 1071 | + * @param expression must not be {@literal null}. |
| 1072 | + * @return new instance of {@link Percentile}. |
| 1073 | + */ |
| 1074 | + public Percentile and(AggregationExpression expression) { |
| 1075 | + |
| 1076 | + Assert.notNull(expression, "Expression must not be null"); |
| 1077 | + return new Percentile(appendTo("input", expression)); |
| 1078 | + } |
| 1079 | + |
| 1080 | + @Override |
| 1081 | + protected String getMongoMethod() { |
| 1082 | + return "$percentile"; |
| 1083 | + } |
| 1084 | + } |
980 | 1085 | }
|
0 commit comments