Skip to content

Conversation

@mourner
Copy link
Owner

@mourner mourner commented Aug 8, 2025

Closes #63. Closes #45. Passes the item's bounding box values to the filterFn in the search method, enabling the following use case:

index.search(10, 10, 20, 20, (i, x0, y0, x1, y1) => {
    console.log(`Item found: ${items[i]}, bbox: ${x0} ${y0} ${x1} ${y1}`);
})

I chose this approach over adding a separate method because it's a minimal, unobtrusive change, doesn't affect performance in any way, doesn't introduce much new code, closures or additional allocations.

The main drawback is that I didn't extend this logic to the neighbors method, because the algorithm there is different — at the time where we iterate over items in the search and have access to bbox values, they're only pushed to a distance queue rather than added to results, so the filterFn is 1) called in a different order, 2) not guaranteed that the item it is called on will end up in results.

This produces an API inconsistency between how search and neighbors handle filterFn. So far I couldn't come up with a performant way to pass bbox values correctly in neighbors so that filterFn can be used the same way as above; another issue is that it's easy to shoot yourself in the foot here because while it's fine to not return anything in the above example, in neighbors, if you don't return true (adding to maxResults count), it will never stop the search until it goes through all items in the index. I tried documenting this discrepancy in the readme, and hope it's a net positive change overall. What do you think @muendlein @bryevdv @leeoniya?

@bryevdv
Copy link

bryevdv commented Aug 8, 2025

@mourner That looks like it will work perfectly for our use-case 🙏

@leeoniya
Copy link

leeoniya commented Aug 8, 2025

👍

@mourner mourner merged commit 6cef6f2 into main Aug 9, 2025
2 checks passed
@mourner mourner deleted the filter-gets-bbox branch August 9, 2025 15:54
@mourner
Copy link
Owner Author

mourner commented Aug 9, 2025

Released in v4.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generalize current filter function inside search. getting back bounding box coords of found indices?

4 participants