1+ /*
2+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+ * or more contributor license agreements. Licensed under the Elastic License
4+ * 2.0; you may not use this file except in compliance with the Elastic License
5+ * 2.0.
6+ */
7+
8+ package org .elasticsearch .xpack .rank .linear ;
9+
10+ import org .apache .lucene .search .ScoreDoc ;
11+ import org .junit .Assert ;
12+ import org .junit .Test ;
13+
14+ public class L2ScoreNormalizerTests {
15+
16+ @ Test
17+ public void testNormalizeTypicalVector () {
18+ ScoreDoc [] docs = {
19+ new ScoreDoc (1 , 3.0f , 0 ),
20+ new ScoreDoc (2 , 4.0f , 0 )
21+ };
22+ ScoreDoc [] normalized = L2ScoreNormalizer .INSTANCE .normalizeScores (docs );
23+ Assert .assertEquals (0.6f , normalized [0 ].score , 1e-5 );
24+ Assert .assertEquals (0.8f , normalized [1 ].score , 1e-5 );
25+ }
26+
27+ @ Test
28+ public void testAllZeros () {
29+ ScoreDoc [] docs = {
30+ new ScoreDoc (1 , 0.0f , 0 ),
31+ new ScoreDoc (2 , 0.0f , 0 )
32+ };
33+ ScoreDoc [] normalized = L2ScoreNormalizer .INSTANCE .normalizeScores (docs );
34+ Assert .assertEquals (0.0f , normalized [0 ].score , 0.0f );
35+ Assert .assertEquals (0.0f , normalized [1 ].score , 0.0f );
36+ }
37+
38+ @ Test
39+ public void testAllNaN () {
40+ ScoreDoc [] docs = {
41+ new ScoreDoc (1 , Float .NaN , 0 ),
42+ new ScoreDoc (2 , Float .NaN , 0 )
43+ };
44+ ScoreDoc [] normalized = L2ScoreNormalizer .INSTANCE .normalizeScores (docs );
45+ Assert .assertTrue (Float .isNaN (normalized [0 ].score ));
46+ Assert .assertTrue (Float .isNaN (normalized [1 ].score ));
47+ }
48+
49+ @ Test
50+ public void testMixedZeroAndNaN () {
51+ ScoreDoc [] docs = {
52+ new ScoreDoc (1 , 0.0f , 0 ),
53+ new ScoreDoc (2 , Float .NaN , 0 )
54+ };
55+ ScoreDoc [] normalized = L2ScoreNormalizer .INSTANCE .normalizeScores (docs );
56+ Assert .assertEquals (0.0f , normalized [0 ].score , 0.0f );
57+ Assert .assertTrue (Float .isNaN (normalized [1 ].score ));
58+ }
59+
60+ @ Test
61+ public void testSingleElement () {
62+ ScoreDoc [] docs = {
63+ new ScoreDoc (1 , 42.0f , 0 )
64+ };
65+ ScoreDoc [] normalized = L2ScoreNormalizer .INSTANCE .normalizeScores (docs );
66+ Assert .assertEquals (1.0f , normalized [0 ].score , 1e-5 );
67+ }
68+
69+ @ Test
70+ public void testEmptyArray () {
71+ ScoreDoc [] docs = {};
72+ ScoreDoc [] normalized = L2ScoreNormalizer .INSTANCE .normalizeScores (docs );
73+ Assert .assertEquals (0 , normalized .length );
74+ }
75+ }
0 commit comments