Skip to content

Commit

Permalink
Add expression support for median aggregation.
Browse files Browse the repository at this point in the history
Original Pull Request: #4515
  • Loading branch information
christophstrobl committed Sep 28, 2023
1 parent d506cd0 commit f84a62b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,16 @@ void shouldRenderPercentile() {
.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);
}
Expand Down

0 comments on commit f84a62b

Please sign in to comment.