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
2.27k stars 142 forks source link

Modify search method to always return BatchMatches for batched inputs #319

Closed altescy closed 11 months ago

altescy commented 11 months ago

In this pull request, I suggest modifying the search method in the Python binding to consistently return BatchMatches for batched inputs, irrespective of the batch size.

Currently, the return type depends on the batch size. The search method returns a Matches value for a batch size of one, and BatchMatches for larger sizes. This requires extra steps to handle the results, like below:

matches = index.search(queries, ...)
if isinstance(matches, Matches):
    ...
elif isinstance(matches, BatchMatches):
    ...

This is not ideal for batch processing, particularly when the batch size varies. Instead, we should use the query vector's dimension (queries.dim in the example) to decide the return type. If the query vector is 2-dimensional, we can assume it's for batch processing.

ashvardanian commented 11 months ago

Hey @altescy! You are right! This is a useful patch, but I am afraid to merge it into the main-dev branch right now, as it may break some of the user applications in the next minor release. Let's plan it for the v3 major release.

ashvardanian commented 9 months ago

:tada: This PR is included in version 2.9.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: