14
14
import java .util .List ;
15
15
import java .util .Map ;
16
16
17
- import static org .elasticsearch .xpack .vectors .mapper .VectorEncoderDecoder .sortSparseDimsDoubleValues ;
17
+ import static org .elasticsearch .xpack .vectors .mapper .VectorEncoderDecoder .sortSparseDimsFloatValues ;
18
18
19
19
public class ScoreScriptUtils {
20
20
@@ -37,7 +37,7 @@ public static double l1norm(List<Number> queryVector, VectorScriptDocValues.Dens
37
37
Iterator <Number > queryVectorIter = queryVector .iterator ();
38
38
double l1norm = 0 ;
39
39
for (int dim = 0 ; dim < docVector .length ; dim ++){
40
- l1norm += Math .abs (queryVectorIter .next ().doubleValue () - docVector [dim ]);
40
+ l1norm += Math .abs (queryVectorIter .next ().floatValue () - docVector [dim ]);
41
41
}
42
42
return l1norm ;
43
43
}
@@ -59,7 +59,7 @@ public static double l2norm(List<Number> queryVector, VectorScriptDocValues.Dens
59
59
Iterator <Number > queryVectorIter = queryVector .iterator ();
60
60
double l2norm = 0 ;
61
61
for (int dim = 0 ; dim < docVector .length ; dim ++){
62
- double diff = queryVectorIter .next ().doubleValue () - docVector [dim ];
62
+ double diff = queryVectorIter .next ().floatValue () - docVector [dim ];
63
63
l2norm += diff * diff ;
64
64
}
65
65
return Math .sqrt (l2norm );
@@ -97,11 +97,11 @@ public static final class CosineSimilarity {
97
97
// calculate queryVectorMagnitude once per query execution
98
98
public CosineSimilarity (List <Number > queryVector ) {
99
99
this .queryVector = queryVector ;
100
- double doubleValue ;
100
+
101
101
double dotProduct = 0 ;
102
102
for (Number value : queryVector ) {
103
- doubleValue = value .doubleValue ();
104
- dotProduct += doubleValue * doubleValue ;
103
+ float floatValue = value .floatValue ();
104
+ dotProduct += floatValue * floatValue ;
105
105
}
106
106
this .queryVectorMagnitude = Math .sqrt (dotProduct );
107
107
}
@@ -130,7 +130,7 @@ private static double intDotProduct(List<Number> v1, float[] v2){
130
130
double v1v2DotProduct = 0 ;
131
131
Iterator <Number > v1Iter = v1 .iterator ();
132
132
for (int dim = 0 ; dim < v2 .length ; dim ++) {
133
- v1v2DotProduct += v1Iter .next ().doubleValue () * v2 [dim ];
133
+ v1v2DotProduct += v1Iter .next ().floatValue () * v2 [dim ];
134
134
}
135
135
return v1v2DotProduct ;
136
136
}
@@ -139,15 +139,15 @@ private static double intDotProduct(List<Number> v1, float[] v2){
139
139
//**************FUNCTIONS FOR SPARSE VECTORS
140
140
141
141
public static class VectorSparseFunctions {
142
- final double [] queryValues ;
142
+ final float [] queryValues ;
143
143
final int [] queryDims ;
144
144
145
145
// prepare queryVector once per script execution
146
146
// queryVector represents a map of dimensions to values
147
147
public VectorSparseFunctions (Map <String , Number > queryVector ) {
148
148
//break vector into two arrays dims and values
149
149
int n = queryVector .size ();
150
- queryValues = new double [n ];
150
+ queryValues = new float [n ];
151
151
queryDims = new int [n ];
152
152
int i = 0 ;
153
153
for (Map .Entry <String , Number > dimValue : queryVector .entrySet ()) {
@@ -156,11 +156,11 @@ public VectorSparseFunctions(Map<String, Number> queryVector) {
156
156
} catch (final NumberFormatException e ) {
157
157
throw new IllegalArgumentException ("Failed to parse a query vector dimension, it must be an integer!" , e );
158
158
}
159
- queryValues [i ] = dimValue .getValue ().doubleValue ();
159
+ queryValues [i ] = dimValue .getValue ().floatValue ();
160
160
i ++;
161
161
}
162
162
// Sort dimensions in the ascending order and sort values in the same order as their corresponding dimensions
163
- sortSparseDimsDoubleValues (queryDims , queryValues , n );
163
+ sortSparseDimsFloatValues (queryDims , queryValues , n );
164
164
}
165
165
}
166
166
@@ -317,7 +317,7 @@ public double cosineSimilaritySparse(VectorScriptDocValues.SparseVectorScriptDoc
317
317
}
318
318
}
319
319
320
- private static double intDotProductSparse (double [] v1Values , int [] v1Dims , float [] v2Values , int [] v2Dims ) {
320
+ private static double intDotProductSparse (float [] v1Values , int [] v1Dims , float [] v2Values , int [] v2Dims ) {
321
321
double v1v2DotProduct = 0 ;
322
322
int v1Index = 0 ;
323
323
int v2Index = 0 ;
0 commit comments