Skip to content

Commit aad4b3b

Browse files
committed
minor test fixes and edge cases
1 parent 904f52d commit aad4b3b

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

server/src/main/java/org/elasticsearch/index/codec/vectors/cluster/HierarchicalKMeans.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public KMeansResult cluster(FloatVectorValues vectors, int targetSize) throws IO
5858
}
5959

6060
// if we have a small number of vectors pick one and output that as the centroid
61-
if (vectors.size() < targetSize) {
61+
if (vectors.size() <= targetSize) {
6262
float[] centroid = new float[dimension];
6363
System.arraycopy(vectors.vectorValue(0), 0, centroid, 0, dimension);
6464
return new KMeansIntermediate(new float[][] { centroid }, new int[vectors.size()]);

server/src/main/java/org/elasticsearch/index/codec/vectors/cluster/KMeansLocal.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ private int[] assignSpilled(FloatVectorValues vectors, List<int[]> neighborhoods
196196

197197
int bestAssignment = -1;
198198
float minSoar = Float.MAX_VALUE;
199+
assert neighborhoods.get(currAssignment) != null;
199200
for (int neighbor : neighborhoods.get(currAssignment)) {
200201
if (neighbor == currAssignment) {
201202
continue;
@@ -260,7 +261,7 @@ void cluster(FloatVectorValues vectors, KMeansIntermediate kMeansIntermediate, b
260261
computeNeighborhoods(centroids, neighborhoods, clustersPerNeighborhood);
261262
}
262263
cluster(vectors, kMeansIntermediate, neighborhoods);
263-
if (neighborAware) {
264+
if (neighborAware && clustersPerNeighborhood > 0) {
264265
int[] assignments = kMeansIntermediate.assignments();
265266
assert assignments != null;
266267
assert assignments.length == vectors.size();

server/src/test/java/org/elasticsearch/index/codec/vectors/cluster/HierarchicalKMeansTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void testHKmeans() throws IOException {
2121
int nClusters = random().nextInt(1, 10);
2222
int nVectors = random().nextInt(nClusters * 100, nClusters * 200);
2323
int dims = random().nextInt(2, 20);
24-
int sampleSize = random().nextInt(100, nVectors);
24+
int sampleSize = random().nextInt(100, nVectors + 1);
2525
int maxIterations = random().nextInt(0, 100);
2626
int clustersPerNeighborhood = random().nextInt(0, 512);
2727
float soarLambda = random().nextFloat(0.5f, 1.5f);
@@ -36,13 +36,14 @@ public void testHKmeans() throws IOException {
3636
int[] assignments = result.assignments();
3737
int[] soarAssignments = result.soarAssignments();
3838

39-
assertEquals(nClusters, centroids.length, 5);
39+
assertEquals(nClusters, centroids.length, 6);
4040
assertEquals(nVectors, assignments.length);
41-
assertEquals(nVectors, soarAssignments.length);
42-
43-
// verify no duplicates exist
44-
for (int i = 0; i < assignments.length; i++) {
45-
assert assignments[i] != soarAssignments[i];
41+
if (centroids.length > 1 && clustersPerNeighborhood > 0) {
42+
assertEquals(nVectors, soarAssignments.length);
43+
// verify no duplicates exist
44+
for (int i = 0; i < assignments.length; i++) {
45+
assert assignments[i] != soarAssignments[i];
46+
}
4647
}
4748
}
4849

server/src/test/java/org/elasticsearch/index/codec/vectors/cluster/KMeansLocalTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void testKMeansNeighbors() throws IOException {
2323
int nClusters = random().nextInt(1, 10);
2424
int nVectors = random().nextInt(nClusters * 100, nClusters * 200);
2525
int dims = random().nextInt(2, 20);
26-
int sampleSize = random().nextInt(100, nVectors);
26+
int sampleSize = random().nextInt(100, nVectors + 1);
2727
int maxIterations = random().nextInt(0, 100);
2828
int clustersPerNeighborhood = random().nextInt(0, 512);
2929
float soarLambda = random().nextFloat(0.5f, 1.5f);

0 commit comments

Comments
 (0)