#community-help

Increasing Relevance of Search Results in Specific Books

TLDR Dave wanted to prioritize search results from a specific book. Jason suggested using _eval with sort_by to achieve the desired outcome.

Powered by Struct AI

1

12
8mo
Solved
Join the chat
Feb 06, 2023 (8 months ago)
Dave
Photo of md5-e2c527f9876a4194f125c06cd599e78e
Dave
04:32 PM
Greetings! I’m evaluating typesense as a tools to look for content in the text of books. If someone is reading a particular book, and do a search, I’d like the results for that book to have the highest relevance, but I’d also like to incorporate results from other titles.

Each document contains the plaintext content of a paragraph, the code that identifies the book, and other stuff. My hack is to append the current book’s code to the user’s search before submitting it, and to set the queryBy to content, code, which makes the search return relevant content, and prefer results containing the code.

However, by appending the code to the user’s query, I effectively disable the prefix search, because their query is never at end of the query string (the code is).

Is there a better way of handling this?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
05:23 PM
Interesting use case!

You could split this into two searches in a multi_search request.

Search 1: You’d add a filter_by to only search inside the books that the user is reading
Search 2: Add a filter_by to exclude the books that the user is reading

Then show the results from Search 1 first, if it returns hits. Then show results from Search 2
Dave
Photo of md5-e2c527f9876a4194f125c06cd599e78e
Dave
07:14 PM
I looked at that, but I’m not sure how I could interleave results and still maintain some kind of relevance order.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:37 PM
But it sounds like you want to override the default relevance order if the user is reading a particular book right?
Dave
Photo of md5-e2c527f9876a4194f125c06cd599e78e
Dave
09:58 PM
That’s right. I guess what I was looking for was some way to add x to the relevance of results for one particular book, so that results from other books would have a higher threshold to meet before appearing in the first page.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:00 PM
It’s not possible to do that, but that reminded me, this feature might be closer to what you’re looking for: https://typesense.org/docs/0.24.0/api/search.html#sorting-based-on-conditions
10:01
Jason
10:01 PM
So you can essentially do something like sort_by: _eval(book_id:<code>)
Dave
Photo of md5-e2c527f9876a4194f125c06cd599e78e
Dave
10:39 PM
That seems to be the magic I was looking for
10:40
Dave
10:40 PM
sort_by: `_text_match:desc,_eval(code: "${book_code}"):desc`
10:40
Dave
10:40 PM
Thank you!

1

10:41
Dave
10:41 PM
Nice product you have here. Well done!
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:42 PM
Thanks Dave!