#community-help

Prioritising Email in Collections Using Typesense

TLDR Pankaj had an issue ranking user emails first in a collection. Jason explained how 'sort_by' works and suggested using the new filter-based sorting mechanism, specifically "sort_by": "_eval(has_email:true):asc,id:desc". Pankaj agreed to try this solution.

Powered by Struct AI
3
11mo
Solved
Join the chat
Oct 26, 2022 (11 months ago)
Pankaj
Photo of md5-6eb5a1ce5c2a902eaa7ed5dae82ed54b
Pankaj
03:47 PM
Hi guys, i have a scenarios tried some ways but not able to get it right
my example collection

email 
id 
description 

i want to rank the result where users with having email are ranked first with id desc

tried doing sort_by
email(missing_values:last):desc,
_text_match:desc,
id:desc

but looks like missing_values is last one to sort so it always like email desc any other ways this can be done ? thanks in advance
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:42 PM
sort_by uses a tie-breaking to sort on multiple fields. So in your case it will first sort on email (and place missing values last), and then if two records tie by having the same email address, then _text_match is used to break the tie. And then if that also ties, then id is used to break the tie.

You could use the new filter-based sorting mechanism we’ve added in 0.24.0.rcn25 to implement your use case. Something like:

"sort_by": "_eval(has_email:true):asc,id:desc"

where has_email is a new boolean field you’ll have to create at indexing time to each record
Oct 27, 2022 (11 months ago)
Pankaj
Photo of md5-6eb5a1ce5c2a902eaa7ed5dae82ed54b
Pankaj
06:07 PM
will try this. thank you 🙂