Skip to content

Commit 36826a4

Browse files
iveraseQuentin18
andauthored
Fix moving function linear weighted avg (#118516) (#118751)
Fix moving function linear weighted avg Co-authored-by: Quentin Deschamps <[email protected]>
1 parent aa1a6d9 commit 36826a4

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ tasks.named("yamlRestTestV7CompatTransform").configure { task ->
5757

5858
// Something has changed with response codes
5959
task.skipTest("search.aggregation/20_terms/IP test", "Hybrid t-digest produces different results.")
60+
// Maths changed
61+
task.skipTest("aggregations/moving_fn/linearWeightedAvg", "math was wrong in previous versions")
6062

6163
task.addAllowedWarningRegex("\\[types removal\\].*")
6264
}

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
@@ -40,6 +40,8 @@ private SearchCapabilities() {}
4040
private static final String RANK_VECTORS_SCRIPT_ACCESS = "rank_vectors_script_access";
4141
/** Initial support for rank-vectors maxSim functions access. */
4242
private static final String RANK_VECTORS_SCRIPT_MAX_SIM = "rank_vectors_script_max_sim_with_bugfix";
43+
/** Fixed the math in {@code moving_fn}'s {@code linearWeightedAvg}. */
44+
private static final String MOVING_FN_RIGHT_MATH = "moving_fn_right_math";
4345

4446
private static final String RANDOM_SAMPLER_WITH_SCORED_SUBAGGS = "random_sampler_with_scored_subaggs";
4547
private static final String OPTIMIZED_SCALAR_QUANTIZATION_BBQ = "optimized_scalar_quantization_bbq";
@@ -56,6 +58,7 @@ private SearchCapabilities() {}
5658
capabilities.add(RANDOM_SAMPLER_WITH_SCORED_SUBAGGS);
5759
capabilities.add(OPTIMIZED_SCALAR_QUANTIZATION_BBQ);
5860
capabilities.add(KNN_QUANTIZED_VECTOR_RESCORE);
61+
capabilities.add(MOVING_FN_RIGHT_MATH);
5962
if (RankVectorsFieldMapper.FEATURE_FLAG.isEnabled()) {
6063
capabilities.add(RANK_VECTORS_FIELD_MAPPER);
6164
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)