|
67 | 67 | import static org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase.randomNormalizedVector; |
68 | 68 | import static org.elasticsearch.index.codec.vectors.IVFVectorsFormat.DYNAMIC_NPROBE; |
69 | 69 | import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DEFAULT_OVERSAMPLE; |
| 70 | +import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.INDEXED_BY_DEFAULT_INDEX_VERSION; |
70 | 71 | import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.IVF_FORMAT; |
71 | 72 | import static org.hamcrest.Matchers.containsString; |
72 | 73 | import static org.hamcrest.Matchers.equalTo; |
@@ -106,7 +107,7 @@ private void indexMapping(XContentBuilder b, IndexVersion indexVersion) throws I |
106 | 107 | if (elementType != ElementType.FLOAT) { |
107 | 108 | b.field("element_type", elementType.toString()); |
108 | 109 | } |
109 | | - if (indexVersion.onOrAfter(DenseVectorFieldMapper.INDEXED_BY_DEFAULT_INDEX_VERSION) || indexed) { |
| 110 | + if (indexVersion.onOrAfter(INDEXED_BY_DEFAULT_INDEX_VERSION) || indexed) { |
110 | 111 | // Serialize if it's new index version, or it was not the default for previous indices |
111 | 112 | b.field("index", indexed); |
112 | 113 | } |
@@ -2206,27 +2207,23 @@ public void testDefaultParamsIndexByDefault() throws Exception { |
2206 | 2207 | assertEquals(VectorSimilarity.COSINE, denseVectorFieldType.getSimilarity()); |
2207 | 2208 | } |
2208 | 2209 |
|
2209 | | - public void testValidateOnBuild() { |
| 2210 | + public void testSetIndexed() { |
2210 | 2211 | final MapperBuilderContext context = MapperBuilderContext.root(false, false); |
2211 | 2212 |
|
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()); |
2217 | 2219 |
|
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()); |
2230 | 2227 | } |
2231 | 2228 |
|
2232 | 2229 | private static float[] decodeDenseVector(IndexVersion indexVersion, BytesRef encodedVector) { |
|
0 commit comments