Skip to content

Commit 77e6568

Browse files
authored
Merge branch 'main' into data-stream-mappings-no-doc
2 parents 003977a + b291dc3 commit 77e6568

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

server/src/main/java/org/elasticsearch/index/codec/vectors/DefaultIVFVectorsReader.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.apache.lucene.util.ArrayUtil;
1919
import org.apache.lucene.util.Bits;
2020
import org.apache.lucene.util.VectorUtil;
21-
import org.apache.lucene.util.hnsw.NeighborQueue;
21+
import org.elasticsearch.index.codec.vectors.cluster.NeighborQueue;
2222
import org.elasticsearch.index.codec.vectors.reflect.OffHeapStats;
2323
import org.elasticsearch.simdvec.ES91OSQVectorsScorer;
2424
import org.elasticsearch.simdvec.ES92Int7VectorsScorer;
@@ -243,22 +243,19 @@ public boolean hasNext() {
243243

244244
@Override
245245
public CentroidOffsetAndLength nextPostingListOffsetAndLength() throws IOException {
246-
int centroidOrdinal = neighborQueue.pop();
247-
updateQueue(); // add one children if available so the queue remains fully populated
246+
int centroidOrdinal = nextCentroid();
248247
centroids.seek(childrenFileOffsets + (long) Long.BYTES * 2 * centroidOrdinal);
249248
long postingListOffset = centroids.readLong();
250249
long postingListLength = centroids.readLong();
251250
return new CentroidOffsetAndLength(postingListOffset, postingListLength);
252251
}
253252

254-
private void updateQueue() throws IOException {
253+
private int nextCentroid() throws IOException {
255254
if (currentParentQueue.size() > 0) {
256-
// add a children from the current parent queue
257-
float score = currentParentQueue.topScore();
258-
int children = currentParentQueue.pop();
259-
neighborQueue.add(children, score);
255+
// return next centroid and maybe add a children from the current parent queue
256+
return neighborQueue.popAndAddRaw(currentParentQueue.popRaw());
260257
} else if (parentsQueue.size() > 0) {
261-
// add a new parent from the current parent queue
258+
// current parent queue is empty, populate it again with the next parent
262259
int pop = parentsQueue.pop();
263260
populateOneChildrenGroup(
264261
currentParentQueue,
@@ -273,7 +270,9 @@ private void updateQueue() throws IOException {
273270
globalCentroidDp,
274271
scores
275272
);
276-
updateQueue();
273+
return nextCentroid();
274+
} else {
275+
return neighborQueue.pop();
277276
}
278277
}
279278
};

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ public int pop() {
137137
return decodeNodeId(heap.pop());
138138
}
139139

140+
/** Removes the top element and returns it */
141+
public long popRaw() {
142+
return heap.pop();
143+
}
144+
145+
/**
146+
* if the new element is the new top then return its node id. Otherwise,
147+
* removes the current top element, returns its node id and adds the new element
148+
* to the queue.
149+
* */
150+
public int popAndAddRaw(long raw) {
151+
long top = heap.top();
152+
if (raw < top) {
153+
return decodeNodeId(raw);
154+
}
155+
heap.updateTop(raw);
156+
return decodeNodeId(top);
157+
}
158+
140159
public void clear() {
141160
heap.clear();
142161
}

0 commit comments

Comments
 (0)