This message was deleted.
# community-help
s
This message was deleted.
j
One way of accomplishing this is by using
_eval
. I created a collection with
Copy code
[
    {"title": "Heavy Cream", "product_id": "1"},
    {"title": "Cream", "product_id": "2"},
    {"title": "Best Cream", "product_id": "3"},
    {"title": "Wow I love this cream", "product_id": "4"},
]
which normally would give
Copy code
Cream
Wow I love this cream
Best Cream
Heavy Cream
for "cream", but if you set
Copy code
"sort_by": "_eval(product_id:[1,4]):desc,_text_match:desc"
you get
Copy code
Wow I love this cream <- favorite
Heavy Cream <- favorite
Cream
Best Cream
There could be some performance costs if the user has a lot of favorites, but in my experience with large filters it hasn't been a problem even with many hundreds of items. Note that this can result in worse matches marked as favorite coming before better matches, but I don't think it'll be much of a problem in practice (most of the time the entire candidate set will be quite good)
j
Nice, I will give this a try. Thank you, appreciated!
j
Nice, that’s a great use of optional filtering @John! Thank you for sharing.
Extending on your idea, another thing that could be done is adding an array field called say
favorited_by_user_ids
to each record, and store which users have favorited each product. Then you could do something like this:
Copy code
"sort_by": "_eval(favorited_by_user_ids:=<CURRENT_USERS_ID>):desc,_text_match:desc"
This way you don’t have to query your DB to get the list of user favorites, and can keep the search fully isolated to just Typesense