#community-help

Typesense Vector Search with Multiple Results Issue

TLDR Henrik had a problem with Typesense vector search returning more results than expected. Kishore Nallan clarified that both k and per_page parameters need to be set to 1 to get the desired number of results.

Powered by Struct AI
May 02, 2023 (7 months ago)
Henrik
Photo of md5-e9a91dadf7fdc83f3e5ac5bad060e992
Henrik
06:17 AM
Hello everyone,
this might be a noob question and I want to apologize if that question was already answered but I could not find anything that helps me solving my question.
I am currently evaluating Typesense and wanted to try the vector search. I 1:1 implemented the example on the documentation page (https://typesense.org/docs/0.24.1/api/vector-search.html#nearest-neighbor-vector-search) . First thing is, that the documentation seems to be incorrect since the vector field in the schema and search is called "vec" and in the document it is called "location". If I change the "location" field in the document to "vec" it works fine. I added several other documents with different values in the "vec" field.
When I now perform the search I always get all documents returned in the response even if I set k=1 in the vector search. As it is stated in the documentation "k" should determine how many results are returned. So why do I get more results than provided with the "k" parameter? Am I doing anything wrong here? Any help is highly appreciated. 😀
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:20 AM
👋 I think the Python example has a typo with location -- I will fix that!

> When I now perform the search I always get all documents returned in the response even if I set k=1 in the vector search.
Do you also pass any pagination parameters?
06:22
Kishore Nallan
06:22 AM
Since the number of results fetched can be controlled both by per_page and k parameters, Typesense picks the largest of those 2 values. By default per_page is 10 so you might be getting more than expected number of values.

Solution: set both k and per_page to 1.
Henrik
Photo of md5-e9a91dadf7fdc83f3e5ac5bad060e992
Henrik
12:06 PM
No - i did not provide any pagination parameters. So if I provide per_page and also set this to 1 I only get one result back. 👍
Maybe it would be worth noting that in the vector search example as well for anyone else who is wondering... 🙃
Thanks a lot for your help! 🙏
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:07 PM
Thanks, will document it. Default value for per_page is 10 so that's the confusion here.