Add expression support for median aggregation.

Original Pull Request: #4515
This commit is contained in:
Christoph Strobl
2023-09-28 09:05:17 +02:00
parent d506cd026f
commit f84a62b94d
2 changed files with 12 additions and 0 deletions

View File

@@ -231,6 +231,8 @@ public class MethodReferenceNode extends ExpressionNode {
.mappingParametersTo("n", "input"));
map.put("percentile", mapArgRef().forOperator("$percentile") //
.mappingParametersTo("input", "p", "method"));
map.put("median", mapArgRef().forOperator("$median") //
.mappingParametersTo("input", "method"));
// TYPE OPERATORS
map.put("type", singleArgRef().forOperator("$type"));

View File

@@ -1269,6 +1269,16 @@ public class SpelExpressionTransformerUnitTests {
.isEqualTo("{ $percentile : { input : \"$score\", p : [0.4, 0.85], method : \"approximate\" }}");
}
@Test // GH-4472
void shouldRenderMedian() {
assertThat(transform("median(new String[]{\"$scoreOne\", \"$scoreTwo\" }, \"approximate\")"))
.isEqualTo("{ $median : { input : [\"$scoreOne\", \"$scoreTwo\"], method : \"approximate\" }}");
assertThat(transform("median(score, \"approximate\")"))
.isEqualTo("{ $median : { input : \"$score\", method : \"approximate\" }}");
}
private Document transform(String expression, Object... params) {
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
}