Skip to content

Commit d572fd7

Browse files
committed
add densevectormapper indexed ut
1 parent c2cef25 commit d572fd7

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

server/src/test/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapperTests.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase.randomNormalizedVector;
6868
import static org.elasticsearch.index.codec.vectors.IVFVectorsFormat.DYNAMIC_NPROBE;
6969
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DEFAULT_OVERSAMPLE;
70+
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.INDEXED_BY_DEFAULT_INDEX_VERSION;
7071
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.IVF_FORMAT;
7172
import static org.hamcrest.Matchers.containsString;
7273
import static org.hamcrest.Matchers.equalTo;
@@ -106,7 +107,7 @@ private void indexMapping(XContentBuilder b, IndexVersion indexVersion) throws I
106107
if (elementType != ElementType.FLOAT) {
107108
b.field("element_type", elementType.toString());
108109
}
109-
if (indexVersion.onOrAfter(DenseVectorFieldMapper.INDEXED_BY_DEFAULT_INDEX_VERSION) || indexed) {
110+
if (indexVersion.onOrAfter(INDEXED_BY_DEFAULT_INDEX_VERSION) || indexed) {
110111
// Serialize if it's new index version, or it was not the default for previous indices
111112
b.field("index", indexed);
112113
}
@@ -2206,27 +2207,23 @@ public void testDefaultParamsIndexByDefault() throws Exception {
22062207
assertEquals(VectorSimilarity.COSINE, denseVectorFieldType.getSimilarity());
22072208
}
22082209

2209-
public void testValidateOnBuild() {
2210+
public void testSetIndexed() {
22102211
final MapperBuilderContext context = MapperBuilderContext.root(false, false);
22112212

2212-
int dimensions = randomIntBetween(64, 1024);
2213-
// Build a dense vector field mapper with float element type, which will trigger int8 HNSW index options
2214-
DenseVectorFieldMapper mapper = new DenseVectorFieldMapper.Builder("test", IndexVersion.current(), false).elementType(
2215-
ElementType.FLOAT
2216-
).dimensions(dimensions).build(context);
2213+
DenseVectorFieldMapper mapper = new DenseVectorFieldMapper.Builder("test",
2214+
IndexVersionUtils.getPreviousVersion(INDEXED_BY_DEFAULT_INDEX_VERSION), false)
2215+
.elementType(ElementType.FLOAT)
2216+
.build(context);
2217+
DenseVectorFieldType denseVectorFieldType = mapper.fieldType();
2218+
assertFalse(denseVectorFieldType.isIndexed());
22172219

2218-
// Change the element type to byte, which is incompatible with int8 HNSW index options
2219-
DenseVectorFieldMapper.Builder builder = (DenseVectorFieldMapper.Builder) mapper.getMergeBuilder();
2220-
builder.elementType(ElementType.BYTE);
2221-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.build(context));
2222-
assertThat(
2223-
e.getMessage(),
2224-
containsString(
2225-
dimensions >= DenseVectorFieldMapper.BBQ_DIMS_DEFAULT_THRESHOLD
2226-
? "[element_type] cannot be [byte] when using index type [bbq_hnsw]"
2227-
: "[element_type] cannot be [byte] when using index type [int8_hnsw]"
2228-
)
2229-
);
2220+
mapper = new DenseVectorFieldMapper.Builder("test",
2221+
IndexVersionUtils.getPreviousVersion(INDEXED_BY_DEFAULT_INDEX_VERSION), false)
2222+
.elementType(ElementType.FLOAT)
2223+
.indexed(true).similarity(VectorSimilarity.COSINE).build(context);
2224+
2225+
denseVectorFieldType = mapper.fieldType();
2226+
assertTrue(denseVectorFieldType.isIndexed());
22302227
}
22312228

22322229
private static float[] decodeDenseVector(IndexVersion indexVersion, BytesRef encodedVector) {

0 commit comments

Comments
 (0)