Apache Lucene open-source search software
Reconnect HNSW nodes on cumulative degree decay during merge-reuse (GITHUB#16552) (#16559)
Ethan Baker091a987a14d9
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex d70c53abb1f..f218741c83a 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -217,6 +217,10 @@ OptimizationsBug Fixes---------------------+* GITHUB#16552: Reconnect HNSW nodes whose out-degree decays across successive+ merge-reuse generations, preventing progressive recall loss under low delete+ rates. (Ethan Baker)+* GITHUB#14049: Randomize KNN codec params in RandomCodec. Fixes scalar quantization div-by-zerowhen all values are identical. (Mike Sokolov)diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/InitializedHnswGraphBuilder.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/InitializedHnswGraphBuilder.javaindex 511474b2f70..d9ce25dd69a 100644--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/InitializedHnswGraphBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/InitializedHnswGraphBuilder.java@@ -46,10 +46,11 @@ import org.apache.lucene.util.BitSet;* <li>Allows incremental addition of new nodes while preserving initialized nodes* </ul>*- * <p><b>Disconnected Node Detection:</b> A node is considered disconnected if it retains less than- * {@link #DISCONNECTED_NODE_FACTOR} of its original neighbor count from the source graph. This- * typically occurs when many of the node's neighbors were deleted documents that couldn't be- * remapped.+ * <p><b>Disconnected Node Detection:</b> A node that lost neighbors this merge is flagged for+ * repair if it either kept less than {@link #DISCONNECTED_NODE_FACTOR} of its neighbors from the+ * source graph, or fell below {@link #CUMULATIVE_DEGREE_FLOOR_FACTOR} of the level's connection+ * budget. The first catches a large loss in a single merge; the second catches slow decay across+ * many merges.** @lucene.experimental*/@@ -78,6 +79,15 @@ public final class InitializedHnswGraphBuilder extends HnswGraphBuilder {*/private final double DISCONNECTED_NODE_FACTOR = 0.85;+ /**+ * The cumulative disconnection threshold. A node is disconnected when its out-degree falls below+ * this fraction of the level's connection budget ({@code M} on upper levels, {@code 2*M} at level+ * 0). Because the budget is fixed, the check holds a node to the same target on every merge, so+ * it catches out-degree that drifts down gradually across many merges rather than in a single+ * one.+ */+ private final double CUMULATIVE_DEGREE_FLOOR_FACTOR = 0.5;+// Tracks if the graph has deletesprivate boolean hasDeletes = false;@@ -201,13 +211,16 @@ public final class InitializedHnswGraphBuilder extends HnswGraphBuilder {* Copies the graph structure from the initializer graph, applying ordinal remapping and* identifying nodes that have lost neighbors.*- * <p>A node is considered disconnected if it retains less than {@link #DISCONNECTED_NODE_FACTOR}- * of its original neighbors. This happens when many neighbors were deleted documents that- * couldn't be remapped (indicated by -1 in newOrdMap).+ * <p>Deleted neighbors show up as -1 in newOrdMap and are dropped. A node that loses any this way+ * is flagged for repair when it crosses one of the two thresholds ({@link+ * #DISCONNECTED_NODE_FACTOR} and {@link #CUMULATIVE_DEGREE_FLOOR_FACTOR}); flagged nodes are+ * repaired afterwards.*- * <p><b>Example:</b> With DISCONNECTED_NODE_FACTOR = 0.9, if a node had 20 neighbors in the- * source graph but only 17 remain after remapping (17/20 = 0.85 < 0.9), it's marked as- * disconnected and will be repaired.+ * <p><b>Example:</b> at level 0 with {@code M=16} (budget {@code 2*M = 32}), a node that kept 16+ * of 20 neighbors is flagged by the per-merge check ({@code 16/20 = 0.8 <+ * DISCONNECTED_NODE_FACTOR = 0.85}); a node that has drifted to 15 neighbors is flagged by the+ * cumulative check ({@code 15 < 32 * CUMULATIVE_DEGREE_FLOOR_FACTOR = 16}) even if it lost only+ * one this merge.** @param initializerGraph the source graph to copy from* @param newOrdMap maps old ordinals to new ordinals; -1 indicates deleted documents@@ -256,8 +269,11 @@ public final class InitializedHnswGraphBuilder extends HnswGraphBuilder {}}- // Mark as disconnected if node lost more than the acceptable threshold of neighbors- if (newNeighbors.size() < oldNeighbourCount * DISCONNECTED_NODE_FACTOR) {+ // Flag a node that lost neighbors and crossed either disconnection threshold.+ int maxConnOnLevel = newNeighbors.maxSize() - 1; // M on upper levels, 2*M on level 0+ if (newNeighbors.size() < oldNeighbourCount * DISCONNECTED_NODE_FACTOR+ || (newNeighbors.size() < oldNeighbourCount+ && newNeighbors.size() < maxConnOnLevel * CUMULATIVE_DEGREE_FLOOR_FACTOR)) {disconnectedNodes.add(newOrd);}}diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/TestHnswFloatVectorGraph.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/TestHnswFloatVectorGraph.javaindex 52d1da3dfa8..6822511fa10 100644--- a/lucene/core/src/test/org/apache/lucene/util/hnsw/TestHnswFloatVectorGraph.java+++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/TestHnswFloatVectorGraph.java@@ -19,6 +19,7 @@ package org.apache.lucene.util.hnsw;import com.carrotsearch.randomizedtesting.RandomizedTest;import java.io.IOException;+import java.util.Random;import org.apache.lucene.document.Field;import org.apache.lucene.document.KnnFloatVectorField;import org.apache.lucene.index.FloatVectorValues;@@ -68,6 +69,54 @@ public class TestHnswFloatVectorGraph extends HnswGraphTestCase<float[]> {return MockVectorValues.fromValues(values);}+ public void testReconnectsNodeDecayedBelowFloorDuringMerge() throws IOException {+ // Pin vectors, both graph seeds, and similarity so the merged graph is identical every run.+ long savedRandSeed = HnswGraphBuilder.randSeed;+ try {+ HnswGraphBuilder.randSeed = 42;+ similarityFunction = VectorSimilarityFunction.EUCLIDEAN;+ int M = 16;+ int beamWidth = 100;+ int size = 128;+ int dim = 16;+ MockVectorValues vectors =+ MockVectorValues.fromValues(createRandomFloatVectors(size, dim, new Random(42)));+ RandomVectorScorerSupplier supplier = buildScorerSupplier(vectors);+ OnHeapHnswGraph graph = HnswGraphBuilder.create(supplier, M, beamWidth, 42).build(size);++ int entry = graph.entryNode();+ int target = entry == 0 ? 1 : 0;++ // Degree 10, below the floor (0.5 * 2M = 16). Deleting one neighbor leaves 9: too small a+ // loss for the per-merge check, but below the floor, so only the cumulative check flags it.+ int degreeBefore = 10;+ NeighborArray targetNeighbors = graph.getNeighbors(0, target);+ targetNeighbors.clear();+ for (int node = 0, added = 0; added < degreeBefore; node++) {+ if (node != target && node != entry) {+ targetNeighbors.addOutOfOrder(node, Float.NaN);+ added++;+ }+ }+ int deleted = targetNeighbors.nodes()[0];+ int[] newOrdMap = new int[size];+ for (int i = 0; i < size; i++) {+ newOrdMap[i] = i;+ }+ newOrdMap[deleted] = -1;++ OnHeapHnswGraph merged =+ InitializedHnswGraphBuilder.initGraph(graph, newOrdMap, size, beamWidth, supplier);++ // Old per-merge-only check leaves the target at 9; the cumulative check reconnects it.+ assertTrue(+ "target below the cumulative floor should be reconnected during merge",+ merged.getNeighbors(0, target).size() >= degreeBefore);+ } finally {+ HnswGraphBuilder.randSeed = savedRandSeed;+ }+ }+@OverrideMockVectorValues vectorValues(LeafReader reader, String fieldName) throws IOException {FloatVectorValues vectorValues = reader.getFloatVectorValues(fieldName);