#community-help

Refining Typesense Query to Exclude Specific IDs

TLDR Andrew was manually ignoring chat IDs in their Typesense query, wondering if there was a better approach. Jason confirmed the approach was correct but suggested simplifying by generating the IDs in an array format.

Powered by Struct AI
2
9mo
Solved
Join the chat
Dec 12, 2022 (9 months ago)
Andrew
Photo of md5-7766f890de99fa72a6d81315691a6758
Andrew
05:41 PM
ok so the way i've been doing this screams incorrect but i didnt find anything on the docs.

So I have a list of strings, 'ignoreIds'. To make sure the resulting query does not have a chatId in this list, i do the following:


String ignore = '';
    //for each chatId, add it to a string that follows the form 'typsenseId:chatId || '
    for (String chatId in ignoreIds) {
      //if chatId is not the last element in the list, add ' || '
      if (chatId != ignoreIds.last) {
        ignore += 'chatId:!=$chatId && ';
      } else {
        //if chatId is the last element in the list, do not add ' || '
        ignore += 'chatId:!=$chatId';
      }
    }

then in the query i have
'filter_by': '(senderId:$userId || receiverId:$userId) && pending:false && ($ignore)',

So that way the id's are ignored. It feels like theres a better way to do this tho lol
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
05:45 PM
Typesense does not have a way to reference sub queries like in SQL, so your general approach of listing out each ID would indeed be the way to do it.

You can simplify the last $ignore part by generating it in this format: chatId:!=[id1,id2,id3]