Skip to content

[bug] Approximate search in VectorStore sometimes fails or returns fewer results #129

Description

@MSM2002

Description

While testing the VectorStore class, we noticed issues with the approximate search feature (enabled with use_approx=True). This feature is supposed to make searches faster by grouping similar items into clusters and searching only the closest clusters instead of the entire dataset.

However, in some cases:

  1. Search fails completely with an error like:
ValueError: need at least one array to concatenate

This happens when the feature tries to look for items in a cluster but the cluster ends up empty.

  1. Search returns fewer results than requested, for example asking for 2 results but getting only 1.

These problems seem related to how clusters are created and queried in the approximate search.

Steps to Reproduce

  1. Create a VectorStore with a small number of items (e.g., 5 company names with 8-dimensional embeddings).
  2. Build the KMeans index with 2 clusters.
  3. Call search with use_approx=True and small n_probe_clusters values.

Example failing test cases:

def test_approx_search_fallback_to_exact(vector_store: VectorStore) -> None:
 vector_store.build_index(n_clusters=2)

 query: NDArray[np.floating] = vector_store.embeddings[2]
 results = vector_store.search(
     query,
     k=2,
     use_approx=True,
     n_probe_clusters=0,
 )
 # Fails with ValueError: need at least one array to concatenate


def test_approx_search_returns_results(vector_store: VectorStore) -> None:
 vector_store.build_index(n_clusters=2)

 query: NDArray[np.floating] = vector_store.embeddings[1]
 results = vector_store.search(
     query,
     k=2,
     use_approx=True,
     n_probe_clusters=1,
 )
 assert len(results) == 2
 # Currently returns only 1 result

Expected Behavior

  • Approximate search should never crash.
  • Approximate search should return up to the requested number of results, falling back to exact search if needed.

Observed Behavior

  • Crashes with ValueError in some cases.
  • Returns fewer results than requested in other cases.

Suggested Next Steps

  • Investigate why clusters can end up empty and cause concatenation errors.
  • Ensure the search function gracefully falls back to exact search if there are no items in the probed clusters.
  • Verify that k results are always returned whenever possible.

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions