Skip to content

Commit

Permalink
[8.x] Fixing MultiDenseVectorScriptDocValuesTests tests (elastic#116940
Browse files Browse the repository at this point in the history
…) (elastic#116976)

* Fixing MultiDenseVectorScriptDocValuesTests tests (elastic#116940)

This fixes two test issues:

 - 1. Now the tests skip if the multi_dense_vector feature isn't enabled
 - 2. fixes silly bwc testing where we were testing for big-endian floats, which aren't possible.

closes: elastic#116862 closes:
elastic#116863
(cherry picked from commit 82c02de)

* fixing backport
  • Loading branch information
benwtrent authored Nov 18, 2024
1 parent 8d5615f commit ee11a00
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.script.field.vectors.MultiDenseVector;
import org.elasticsearch.script.field.vectors.MultiDenseVectorDocValuesField;
import org.elasticsearch.test.ESTestCase;
import org.junit.BeforeClass;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -28,13 +29,17 @@

public class MultiDenseVectorScriptDocValuesTests extends ESTestCase {

@BeforeClass
public static void setup() {
assumeTrue("Requires multi-dense vector support", MultiDenseVectorFieldMapper.FEATURE_FLAG.isEnabled());
}

public void testFloatGetVectorValueAndGetMagnitude() throws IOException {
int dims = 3;
float[][][] vectors = { { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }, { { 1, 0, 2 } } };
float[][] expectedMagnitudes = { { 1.7320f, 2.4495f, 3.3166f }, { 2.2361f } };

IndexVersion indexVersion = IndexVersion.current();
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT, indexVersion);
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT);
BinaryDocValues magnitudeValues = wrap(expectedMagnitudes);
MultiDenseVectorDocValuesField field = new FloatMultiDenseVectorDocValuesField(
docValues,
Expand Down Expand Up @@ -64,7 +69,7 @@ public void testByteGetVectorValueAndGetMagnitude() throws IOException {
float[][][] vectors = { { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }, { { 1, 0, 2 } } };
float[][] expectedMagnitudes = { { 1.7320f, 2.4495f, 3.3166f }, { 2.2361f } };

BinaryDocValues docValues = wrap(vectors, ElementType.BYTE, IndexVersion.current());
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE);
BinaryDocValues magnitudeValues = wrap(expectedMagnitudes);
MultiDenseVectorDocValuesField field = new ByteMultiDenseVectorDocValuesField(
docValues,
Expand All @@ -91,10 +96,9 @@ public void testByteGetVectorValueAndGetMagnitude() throws IOException {

public void testFloatMetadataAndIterator() throws IOException {
int dims = 3;
IndexVersion indexVersion = IndexVersion.current();
float[][][] vectors = new float[][][] { fill(new float[3][dims], ElementType.FLOAT), fill(new float[2][dims], ElementType.FLOAT) };
float[][] magnitudes = new float[][] { new float[3], new float[2] };
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT, indexVersion);
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT);
BinaryDocValues magnitudeValues = wrap(magnitudes);

MultiDenseVectorDocValuesField field = new FloatMultiDenseVectorDocValuesField(
Expand All @@ -120,10 +124,9 @@ public void testFloatMetadataAndIterator() throws IOException {

public void testByteMetadataAndIterator() throws IOException {
int dims = 3;
IndexVersion indexVersion = IndexVersion.current();
float[][][] vectors = new float[][][] { fill(new float[3][dims], ElementType.BYTE), fill(new float[2][dims], ElementType.BYTE) };
float[][] magnitudes = new float[][] { new float[3], new float[2] };
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE, indexVersion);
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE);
BinaryDocValues magnitudeValues = wrap(magnitudes);
MultiDenseVectorDocValuesField field = new ByteMultiDenseVectorDocValuesField(
docValues,
Expand Down Expand Up @@ -159,7 +162,7 @@ public void testFloatMissingValues() throws IOException {
int dims = 3;
float[][][] vectors = { { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }, { { 1, 0, 2 } } };
float[][] magnitudes = { { 1.7320f, 2.4495f, 3.3166f }, { 2.2361f } };
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT, IndexVersion.current());
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT);
BinaryDocValues magnitudeValues = wrap(magnitudes);
MultiDenseVectorDocValuesField field = new FloatMultiDenseVectorDocValuesField(
docValues,
Expand All @@ -183,7 +186,7 @@ public void testByteMissingValues() throws IOException {
int dims = 3;
float[][][] vectors = { { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }, { { 1, 0, 2 } } };
float[][] magnitudes = { { 1.7320f, 2.4495f, 3.3166f }, { 2.2361f } };
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE, IndexVersion.current());
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE);
BinaryDocValues magnitudeValues = wrap(magnitudes);
MultiDenseVectorDocValuesField field = new ByteMultiDenseVectorDocValuesField(
docValues,
Expand All @@ -207,7 +210,7 @@ public void testFloatGetFunctionIsNotAccessible() throws IOException {
int dims = 3;
float[][][] vectors = { { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }, { { 1, 0, 2 } } };
float[][] magnitudes = { { 1.7320f, 2.4495f, 3.3166f }, { 2.2361f } };
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT, IndexVersion.current());
BinaryDocValues docValues = wrap(vectors, ElementType.FLOAT);
BinaryDocValues magnitudeValues = wrap(magnitudes);
MultiDenseVectorDocValuesField field = new FloatMultiDenseVectorDocValuesField(
docValues,
Expand All @@ -233,7 +236,7 @@ public void testByteGetFunctionIsNotAccessible() throws IOException {
int dims = 3;
float[][][] vectors = { { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }, { { 1, 0, 2 } } };
float[][] magnitudes = { { 1.7320f, 2.4495f, 3.3166f }, { 2.2361f } };
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE, IndexVersion.current());
BinaryDocValues docValues = wrap(vectors, ElementType.BYTE);
BinaryDocValues magnitudeValues = wrap(magnitudes);
MultiDenseVectorDocValuesField field = new ByteMultiDenseVectorDocValuesField(
docValues,
Expand Down Expand Up @@ -303,7 +306,7 @@ public long cost() {
};
}

public static BinaryDocValues wrap(float[][][] vectors, ElementType elementType, IndexVersion indexVersion) {
public static BinaryDocValues wrap(float[][][] vectors, ElementType elementType) {
return new BinaryDocValues() {
int idx = -1;
int maxIdx = vectors.length;
Expand All @@ -313,7 +316,7 @@ public BytesRef binaryValue() {
if (idx >= maxIdx) {
throw new IllegalStateException("max index exceeded");
}
return mockEncodeDenseVector(vectors[idx], elementType, indexVersion);
return mockEncodeDenseVector(vectors[idx], elementType, IndexVersion.current());
}

@Override
Expand Down

0 comments on commit ee11a00

Please sign in to comment.