Skip to content

Commit 3a70439

Browse files
authored
Adding vector index shard key property, for DiskANN vector search (Azure#44007)
* Adding vector index shard key property * Updating changelog * Updating changelog * Updating tests * Resolving comments * Resolving comments * Updating tests * fixing build
1 parent 8ca8e2c commit 3a70439

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/VectorIndexTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ private void validateVectorIndexes(List<CosmosVectorIndexSpec> actual, List<Cosm
309309
if (Objects.equals(expected.get(i).getType(), CosmosVectorIndexType.QUANTIZED_FLAT.toString()) ||
310310
Objects.equals(expected.get(i).getType(), CosmosVectorIndexType.DISK_ANN.toString())) {
311311
assertThat(expected.get(i).getQuantizationSizeInBytes()).isEqualTo(actual.get(i).getQuantizationSizeInBytes());
312+
assertThat(expected.get(i).getVectorIndexShardKeys()).isEqualTo(actual.get(i).getVectorIndexShardKeys());
312313
}
313314
if (Objects.equals(expected.get(i).getType(), CosmosVectorIndexType.DISK_ANN.toString())) {
314315
assertThat(expected.get(i).getIndexingSearchListSize()).isEqualTo(actual.get(i).getIndexingSearchListSize());
@@ -326,12 +327,14 @@ private List<CosmosVectorIndexSpec> populateVectorIndexes() {
326327
cosmosVectorIndexSpec2.setPath("/vector2");
327328
cosmosVectorIndexSpec2.setType(CosmosVectorIndexType.QUANTIZED_FLAT.toString());
328329
cosmosVectorIndexSpec2.setQuantizationSizeInBytes(2);
330+
cosmosVectorIndexSpec2.setVectorIndexShardKeys(Arrays.asList("/zipCode"));
329331

330332
CosmosVectorIndexSpec cosmosVectorIndexSpec3 = new CosmosVectorIndexSpec();
331333
cosmosVectorIndexSpec3.setPath("/vector3");
332334
cosmosVectorIndexSpec3.setType(CosmosVectorIndexType.DISK_ANN.toString());
333335
cosmosVectorIndexSpec3.setQuantizationSizeInBytes(2);
334336
cosmosVectorIndexSpec3.setIndexingSearchListSize(30);
337+
cosmosVectorIndexSpec3.setVectorIndexShardKeys(Arrays.asList("/country/city"));
335338

336339
return Arrays.asList(cosmosVectorIndexSpec1, cosmosVectorIndexSpec2, cosmosVectorIndexSpec3);
337340
}

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#### Bugs Fixed
1010

1111
#### Other Changes
12+
* Added the `vectorIndexShardKeys` to the vectorIndexSpec for QuantizedFlat and DiskANN vector search. - [PR 44007](https://github.com/Azure/azure-sdk-for-java/pull/44007)
1213

1314
### 4.68.0 (2025-03-20)
1415

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public static final class Properties {
153153
public static final String DISTANCE_FUNCTION = "distanceFunction";
154154
public static final String VECTOR_QUANTIZATION_SIZE_IN_BYTES = "quantizationByteSize";
155155
public static final String VECTOR_INDEXING_SEARCH_LIST_SIZE = "indexingSearchListSize";
156+
public static final String VECTOR_INDEX_SHARD_KEYS = "vectorIndexShardKeys";
156157

157158
// Unique index.
158159
public static final String UNIQUE_KEY_POLICY = "uniqueKeyPolicy";

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/IndexProperty.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
public enum IndexProperty {
66
INDEXING_SEARCH_LIST_SIZE,
7-
QUANTIZATION_SIZE_IN_BYTES;
7+
QUANTIZATION_SIZE_IN_BYTES,
8+
VECTOR_INDEX_SHARD_KEYS;
89
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosVectorIndexSpec.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.fasterxml.jackson.annotation.JsonInclude;
1010
import com.fasterxml.jackson.annotation.JsonProperty;
1111

12+
import java.util.List;
13+
1214
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;
1315

1416
/**
@@ -22,6 +24,8 @@ public final class CosmosVectorIndexSpec {
2224
private Integer quantizationSizeInBytes;
2325
@JsonInclude(JsonInclude.Include.NON_NULL)
2426
private Integer indexingSearchListSize;
27+
@JsonInclude(JsonInclude.Include.NON_NULL)
28+
private List<String> vectorIndexShardKeys;
2529
private final JsonSerializable jsonSerializable;
2630

2731
/**
@@ -147,6 +151,38 @@ public CosmosVectorIndexSpec setIndexingSearchListSize(Integer indexingSearchLis
147151
return this;
148152
}
149153

154+
/**
155+
* Gets the vector indexing shard keys
156+
*
157+
* @return vectorIndexShardKeys the list of string containing the shard keys used for partitioning the
158+
* vector indexes. This applies to index types diskANN and quantizedFlat. The maximum allowed size for
159+
* this array is currently limited to 1 - that is, there is only one allowed path.
160+
*/
161+
public List<String> getVectorIndexShardKeys() {
162+
if (this.vectorIndexShardKeys == null) {
163+
this.vectorIndexShardKeys = this.jsonSerializable.getList(Constants.Properties.VECTOR_INDEX_SHARD_KEYS, String.class);
164+
}
165+
return this.vectorIndexShardKeys;
166+
}
167+
168+
/**
169+
* Sets the vector indexing shard keys
170+
*
171+
* @param vectorIndexShardKeys vectorIndexShardKeys the list of string containing the shard keys used for partitioning the
172+
* vector indexes. This applies to index types diskANN and quantizedFlat. The maximum allowed size for
173+
* this array is currently limited to 1 - that is, there is only one allowed path.
174+
* @return CosmosVectorIndexSpec
175+
*/
176+
public CosmosVectorIndexSpec setVectorIndexShardKeys(List<String> vectorIndexShardKeys) {
177+
if (validateIndexType(IndexProperty.VECTOR_INDEX_SHARD_KEYS) && vectorIndexShardKeys != null) {
178+
this.vectorIndexShardKeys = vectorIndexShardKeys;
179+
this.jsonSerializable.set(Constants.Properties.VECTOR_INDEX_SHARD_KEYS, this.vectorIndexShardKeys);
180+
} else {
181+
this.vectorIndexShardKeys = null;
182+
}
183+
return this;
184+
}
185+
150186
void populatePropertyBag() {
151187
this.jsonSerializable.populatePropertyBag();
152188
}
@@ -157,7 +193,7 @@ JsonSerializable getJsonSerializable() {
157193

158194
private Boolean validateIndexType(IndexProperty indexProperty) {
159195
String vectorIndexType = this.jsonSerializable.getString(Constants.Properties.VECTOR_INDEX_TYPE);
160-
if (indexProperty.equals(IndexProperty.QUANTIZATION_SIZE_IN_BYTES)) {
196+
if (indexProperty.equals(IndexProperty.QUANTIZATION_SIZE_IN_BYTES) || (indexProperty.equals(IndexProperty.VECTOR_INDEX_SHARD_KEYS))) {
161197
return vectorIndexType.equals(CosmosVectorIndexType.QUANTIZED_FLAT.toString()) ||
162198
vectorIndexType.equals(CosmosVectorIndexType.DISK_ANN.toString());
163199
}

0 commit comments

Comments
 (0)