Skip to content

Apache Lucene open-source search software

Two fixes for recently-added HnswGraphBuilder.connectComponents: (#13642)

Michael Sokolovf0558ae26392
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphBuilder.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphBuilder.java
index f31c7942680..8072bae462e 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphBuilder.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphBuilder.java
@@ -180,7 +180,6 @@ public class HnswGraphBuilder implements HnswBuilder {
public OnHeapHnswGraph getCompletedGraph() throws IOException {
if (!frozen) {
finish();
- frozen = true;
}
return getGraph();
}
@@ -414,6 +413,7 @@ public class HnswGraphBuilder implements HnswBuilder {
void finish() throws IOException {
connectComponents();
+ frozen = true;
}
private void connectComponents() throws IOException {
@@ -438,6 +438,7 @@ public class HnswGraphBuilder implements HnswBuilder {
maxConn *= 2;
}
List<Component> components = HnswUtil.components(hnsw, level, notFullyConnected, maxConn);
+ // System.out.println("HnswGraphBuilder.connectComponents " + components);
boolean result = true;
if (components.size() > 1) {
// connect other components to the largest one
@@ -448,7 +449,7 @@ public class HnswGraphBuilder implements HnswBuilder {
}
// try for more connections? We only do one since otherwise they may become full
// while linking
- GraphBuilderKnnCollector beam = new GraphBuilderKnnCollector(1);
+ GraphBuilderKnnCollector beam = new GraphBuilderKnnCollector(2);
int[] eps = new int[1];
for (Component c : components) {
if (c != c0) {
@@ -460,10 +461,14 @@ public class HnswGraphBuilder implements HnswBuilder {
graphSearcher.searchLevel(beam, scorer, 0, eps, hnsw, notFullyConnected);
boolean linked = false;
while (beam.size() > 0) {
- float score = beam.minimumScore();
int c0node = beam.popNode();
+ if (c0node == c.start() || notFullyConnected.get(c0node) == false) {
+ continue;
+ }
+ float score = beam.minimumScore();
assert notFullyConnected.get(c0node);
// link the nodes
+ // System.out.println("link " + c0 + "." + c0node + " to " + c + "." + c.start());
link(level, c0node, c.start(), score, notFullyConnected);
linked = true;
}