Skip to content

Commit 21a89f2

Browse files
committed
addressing PR comments
1 parent 5026756 commit 21a89f2

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,25 +179,19 @@ private void computeNeighborhoods(float[][] centers, List<NeighborHood> neighbor
179179
}
180180
}
181181

182-
float[] scores = new float[clustersPerNeighborhood];
183182
for (int i = 0; i < k; i++) {
184183
NeighborQueue queue = neighborQueues.get(i);
185-
int neighborCount = queue.size();
186-
int[] neighbors = new int[neighborCount];
187-
float maxIntraDistance = queue.consumeNodesWithWorstScore(neighbors, scores);
188-
// Sort neighbors by their score
189-
for (int j = 0; j < neighborCount; j++) {
190-
for (int l = j + 1; l < neighborCount; l++) {
191-
if (scores[j] > scores[l]) {
192-
// swap
193-
int tmp = neighbors[j];
194-
neighbors[j] = neighbors[l];
195-
neighbors[l] = tmp;
196-
float tmpScore = scores[j];
197-
scores[j] = scores[l];
198-
scores[l] = tmpScore;
199-
}
200-
}
184+
if (queue.size() == 0) {
185+
// no neighbors, skip
186+
neighborhoods.set(i, NeighborHood.EMPTY);
187+
continue;
188+
}
189+
// consume the queue into the neighbors array and get the maximum intra-cluster distance
190+
int[] neighbors = new int[queue.size()];
191+
float maxIntraDistance = queue.topScore();
192+
int iter = 0;
193+
while (queue.size() > 0) {
194+
neighbors[neighbors.length - ++iter] = queue.pop();
201195
}
202196
NeighborHood neighborHood = new NeighborHood(neighbors, maxIntraDistance);
203197
neighborhoods.set(i, neighborHood);
@@ -283,7 +277,9 @@ void cluster(FloatVectorValues vectors, KMeansIntermediate kMeansIntermediate) t
283277
cluster(vectors, kMeansIntermediate, false);
284278
}
285279

286-
record NeighborHood(int[] neighbors, float maxIntraDistance) {}
280+
record NeighborHood(int[] neighbors, float maxIntraDistance) {
281+
static final NeighborHood EMPTY = new NeighborHood(new int[0], Float.POSITIVE_INFINITY);
282+
}
287283

288284
/**
289285
* cluster using a lloyd kmeans algorithm that also considers prior clustered neighborhoods when adjusting centroids

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,6 @@ public int pop() {
121121
return decodeNodeId(heap.pop());
122122
}
123123

124-
public float consumeNodesWithWorstScore(int[] dest, float[] scores) {
125-
if (dest.length < size()) {
126-
throw new IllegalArgumentException("Destination array is too small. Expected at least " + size() + " elements.");
127-
}
128-
float worstScore = Float.NEGATIVE_INFINITY;
129-
for (int i = 0; i < size(); i++) {
130-
long heapValue = heap.get(i + 1);
131-
float score = decodeScore(heapValue);
132-
dest[i] = decodeNodeId(heapValue);
133-
scores[i] = score;
134-
if (score > worstScore) {
135-
worstScore = score;
136-
}
137-
}
138-
return worstScore;
139-
}
140-
141124
public void clear() {
142125
heap.clear();
143126
}

0 commit comments

Comments
 (0)