Skip to content

Commit 908cf9a

Browse files
authored
Fix moving function linear weighted avg (#118516)
Fix moving function linear weighted avg
1 parent dcadb08 commit 908cf9a

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

docs/changelog/118516.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 118435
2+
summary: Fix moving function linear weighted avg
3+
area: Aggregations
4+
type: bug
5+
issues:
6+
- 113751

modules/aggregations/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ tasks.named("yamlRestCompatTestTransform").configure({ task ->
4848
task.skipTest("aggregations/date_agg_per_day_of_week/Date aggregartion per day of week", "week-date behaviour has changed")
4949
task.skipTest("aggregations/time_series/Configure with no synthetic source", "temporary until backport")
5050
task.skipTest("aggregations/percentiles_hdr_metric/Negative values test", "returned exception has changed")
51+
task.skipTest("aggregations/moving_fn/linearWeightedAvg", "math was wrong in previous versions")
5152
})

modules/aggregations/src/yamlRestTest/resources/rest-api-spec/test/aggregations/moving_fn.yml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@ linearWeightedAvg:
255255
- skip:
256256
features: close_to
257257

258+
- requires:
259+
test_runner_features: [capabilities]
260+
261+
- requires:
262+
capabilities:
263+
- method: POST
264+
path: /_search
265+
parameters: [method, path, parameters, capabilities]
266+
capabilities: [moving_fn_right_math]
267+
reason: "math not fixed yet"
268+
258269
- do:
259270
search:
260271
index: no_gaps
@@ -275,11 +286,11 @@ linearWeightedAvg:
275286
- match: { hits.total.value: 6 }
276287
- length: { [email protected]: 6 }
277288
- is_false: [email protected]
278-
- close_to: { [email protected]: { value: 0.500, error: 0.0005 } }
279-
- close_to: { [email protected]: { value: 1.250, error: 0.0005 } }
280-
- close_to: { [email protected]: { value: 1.000, error: 0.0005 } }
281-
- close_to: { [email protected]: { value: 2.250, error: 0.0005 } }
282-
- close_to: { [email protected]: { value: 3.500, error: 0.0005 } }
289+
- close_to: { [email protected]: { value: 1.000, error: 0.0005 } }
290+
- close_to: { [email protected]: { value: 1.667, error: 0.0005 } }
291+
- close_to: { [email protected]: { value: 1.333, error: 0.0005 } }
292+
- close_to: { [email protected]: { value: 3.000, error: 0.0005 } }
293+
- close_to: { [email protected]: { value: 4.667, error: 0.0005 } }
283294

284295
- do:
285296
search:
@@ -301,11 +312,11 @@ linearWeightedAvg:
301312
- match: { hits.total.value: 6 }
302313
- length: { [email protected]: 6 }
303314
- is_false: [email protected]
304-
- close_to: { [email protected]: { value: 0.500, error: 0.0005 } }
305-
- close_to: { [email protected]: { value: 1.250, error: 0.0005 } }
306-
- close_to: { [email protected]: { value: 1.143, error: 0.0005 } }
307-
- close_to: { [email protected]: { value: 2.286, error: 0.0005 } }
308-
- close_to: { [email protected]: { value: 3.429, error: 0.0005 } }
315+
- close_to: { [email protected]: { value: 1.000, error: 0.0005 } }
316+
- close_to: { [email protected]: { value: 1.667, error: 0.0005 } }
317+
- close_to: { [email protected]: { value: 1.333, error: 0.0005 } }
318+
- close_to: { [email protected]: { value: 2.667, error: 0.0005 } }
319+
- close_to: { [email protected]: { value: 4.000, error: 0.0005 } }
309320

310321
---
311322
ewma:

server/src/main/java/org/elasticsearch/rest/action/search/SearchCapabilities.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ private SearchCapabilities() {}
4242
private static final String RANK_VECTORS_SCRIPT_ACCESS = "rank_vectors_script_access";
4343
/** Initial support for rank-vectors maxSim functions access. */
4444
private static final String RANK_VECTORS_SCRIPT_MAX_SIM = "rank_vectors_script_max_sim_with_bugfix";
45+
/** Fixed the math in {@code moving_fn}'s {@code linearWeightedAvg}. */
46+
private static final String MOVING_FN_RIGHT_MATH = "moving_fn_right_math";
4547

4648
private static final String RANDOM_SAMPLER_WITH_SCORED_SUBAGGS = "random_sampler_with_scored_subaggs";
4749
private static final String OPTIMIZED_SCALAR_QUANTIZATION_BBQ = "optimized_scalar_quantization_bbq";
@@ -59,6 +61,7 @@ private SearchCapabilities() {}
5961
capabilities.add(RANDOM_SAMPLER_WITH_SCORED_SUBAGGS);
6062
capabilities.add(OPTIMIZED_SCALAR_QUANTIZATION_BBQ);
6163
capabilities.add(KNN_QUANTIZED_VECTOR_RESCORE);
64+
capabilities.add(MOVING_FN_RIGHT_MATH);
6265
if (RankVectorsFieldMapper.FEATURE_FLAG.isEnabled()) {
6366
capabilities.add(RANK_VECTORS_FIELD_MAPPER);
6467
capabilities.add(RANK_VECTORS_SCRIPT_ACCESS);

server/src/main/java/org/elasticsearch/search/aggregations/pipeline/MovingFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static double stdDev(double[] values, double avg) {
100100
*/
101101
public static double linearWeightedAvg(double[] values) {
102102
double avg = 0;
103-
long totalWeight = 1;
103+
long totalWeight = 0;
104104
long current = 1;
105105

106106
for (double v : values) {
@@ -110,7 +110,7 @@ public static double linearWeightedAvg(double[] values) {
110110
current += 1;
111111
}
112112
}
113-
return totalWeight == 1 ? Double.NaN : avg / totalWeight;
113+
return totalWeight == 0 ? Double.NaN : avg / totalWeight;
114114
}
115115

116116
/**

server/src/test/java/org/elasticsearch/search/aggregations/pipeline/MovFnWhitelistedFunctionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public void testLinearMovAvg() {
326326
}
327327

328328
double avg = 0;
329-
long totalWeight = 1;
329+
long totalWeight = 0;
330330
long current = 1;
331331

332332
for (double value : window) {

0 commit comments

Comments
 (0)