Skip to content

Commit b763bcc

Browse files
committed
Merge branch 'non-issue/esql-dense-vector-support-normalization' into non-issue/esql-dense-vector-byte-element-support
# Conflicts: # server/src/main/java/org/elasticsearch/index/mapper/BlockDocValuesReader.java # x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/DenseVectorFieldTypeIT.java # x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/KnnFunctionIT.java
2 parents a4aca14 + 7d2625c commit b763bcc

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

server/src/main/java/org/elasticsearch/index/mapper/BlockDocValuesReader.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,10 @@ public AllReader reader(LeafReaderContext context) throws IOException {
536536
FloatVectorValues floatVectorValues = context.reader().getFloatVectorValues(fieldName);
537537
if (floatVectorValues != null) {
538538
if (fieldType.isNormalized()) {
539-
return new FloatDenseVectorNormalizedValuesBlockReader(
540-
floatVectorValues,
541-
dimensions,
542-
context.reader().getNumericDocValues(fieldType.name() + COSINE_MAGNITUDE_FIELD_SUFFIX)
543-
);
539+
NumericDocValues magnitudeDocValues = context.reader()
540+
.getNumericDocValues(fieldType.name() + COSINE_MAGNITUDE_FIELD_SUFFIX);
541+
return new FloatDenseVectorNormalizedValuesBlockReader(floatVectorValues, dimensions, magnitudeDocValues);
542+
}
544543
}
545544
return new FloatDenseVectorValuesBlockReader(floatVectorValues, dimensions);
546545
}
@@ -644,8 +643,12 @@ protected void appendDoc(BlockLoader.FloatBuilder builder) throws IOException {
644643
assert floats.length == dimensions
645644
: "unexpected dimensions for vector value; expected " + dimensions + " but got " + floats.length;
646645

647-
assert magnitudeDocValues.advanceExact(iterator.docID());
648-
float magnitude = Float.intBitsToFloat((int) magnitudeDocValues.longValue());
646+
float magnitude = 1.0f;
647+
// If all vectors are normalized, no doc values will be present. The vector may be normalized already, so we may not have a
648+
// stored magnitude for all docs
649+
if ((magnitudeDocValues != null) && magnitudeDocValues.advanceExact(iterator.docID())) {
650+
magnitude = Float.intBitsToFloat((int) magnitudeDocValues.longValue());
651+
}
649652
for (float aFloat : floats) {
650653
builder.appendFloat(aFloat * magnitude);
651654
}
@@ -654,6 +657,7 @@ protected void appendDoc(BlockLoader.FloatBuilder builder) throws IOException {
654657
@Override
655658
public String toString() {
656659
return "BlockDocValuesReader.FloatDenseVectorNormalizedValuesBlockReader";
660+
return "BlockDocValuesReader.FloatDenseVectorNormalizedValuesBlockReader";
657661
}
658662
}
659663

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/DenseVectorFieldTypeIT.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void setup() throws IOException {
219219
default -> throw new IllegalArgumentException("Unexpected element type: " + elementType);
220220
}
221221
}
222-
if (similarity == DenseVectorFieldMapper.VectorSimilarity.DOT_PRODUCT) {
222+
if (similarity == DenseVectorFieldMapper.VectorSimilarity.DOT_PRODUCT || rarely()) {
223223
// Normalize the vector
224224
float magnitude = DenseVector.getMagnitude(vector);
225225
switch (elementType) {
@@ -248,10 +248,7 @@ private void createIndexWithDenseVector(String indexName) throws IOException {
248248
.field("element_type", elementType.toString().toLowerCase(Locale.ROOT))
249249
.field("index", index);
250250
if (index) {
251-
mapping.field(
252-
"similarity",
253-
similarity.name().toLowerCase(Locale.ROOT)
254-
);
251+
mapping.field("similarity", similarity.name().toLowerCase(Locale.ROOT));
255252
}
256253
if (indexType != null) {
257254
mapping.startObject("index_options").field("type", indexType).endObject();

0 commit comments

Comments
 (0)