unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
1.92k stars 109 forks source link

Bug: Replacing initial entry affects visibility of other entries #399

Open jaysenmarais opened 2 months ago

jaysenmarais commented 2 months ago

Describe the bug

Removing items from the index then re-adding them generally works, except when removing the first item added to the index then re-adding it. In that case, other items (all but the first-added) no longer appear in search results (though count still hints at their existence in the index).

Steps to reproduce

Here's an adaptation of a Swift test that illustrates the issue

let index = USearchIndex.make(
    metric: USearchMetric.l2sq,
    dimensions: 1,
    connectivity: 8,
    quantization: USearchScalar.F32
)
index.reserve(3)

// add 3 entries then ensure all 3 entries are returned
index.add(key: 1, vector: [1.1])
index.add(key: 2, vector: [2.1])
index.add(key: 3, vector: [3.1])
assert(index.count == 3)
assert(index.search(vector: [1.0], count: 3).0 == [1, 2, 3]) // v2.11.7 works 😎

// replace second-added entry then ensure all 3 entries are still returned
index.remove(key: 2)
index.add(key: 2, vector: [2.2])
assert(index.count == 3)
assert(index.search(vector: [1.0], count: 3).0 == [1, 2, 3]) // v2.11.7 works 😎

// replace first-added entry then ensure all 3 entries are still returned
index.remove(key: 1)
index.add(key: 1, vector: [1.2])
let afterReplacingInitial = index.search(vector: [1.0], count: 3).0
assert(index.count == 3)
assert(afterReplacingInitial == [1, 2, 3]) // v2.11.7 fails with "[1] != [1, 2, 3]" 😨

Expected behavior

In the above example the final assertion should succeed, but in fact it fails as the second and third items added to the index no longer appear in search results after the first-added item is replaced.

USearch version

v2.11.7

Operating System

macOS 14.4.1 (building for iOS 17.4)

Hardware architecture

Arm

Which interface are you using?

Other bindings

Are you open to being tagged as a contributor?

Is there an existing issue for this?

Code of Conduct

ashvardanian commented 2 months ago

Great catch! Can you please open a PR with those tests, I'll look into it 🤗

jaysenmarais commented 2 months ago

I've now created PR #400 which adds the above snippet as a runnable test.