Full text search mongo or parse problem?

I’m trying to simply search our users with parse query based on phrase, so for example when I type: “mar” it will find “Mark” user (fullname) field. I’ve created mongodb text index, but it works only with full words. I can’t query for “Mar”, but need to type the full word “Mark”.
How to fix it?

Unfortunately there is no way to solve that. It is how mongodb full text search works (https://docs.mongodb.com/manual/reference/operator/query/text/#stemmed-words). You can achieve what you want with regex (very expensive) or maybe integrating to an elastic search solution.

Thank you,
maybe I could add another text field with all the sufixes “m ma mar mark” and then create index on that field, what do you think?

I am not sure. You need to test.

How long is your text? If you gonna create all possible substring by yourself, then maybe storing them in an array field and querying them with containedIn would perform better. Full text search is an expensive operation. But size of the text is important.

I guess it’s all a matter of your scale and use case. As @davimacedo says, regex is expensive, but if your data set isn’t too big, then this might be the quickest and easiest approach.

I’m currently doing a compound regex search matching across four user fields (username, name, surname, email) and it’s more than fast enough for me. I’ve ensured that I implement a debounce on the frontend so that I’m not hitting the server with every key stroke and this search feature is only used by admins, so I know the impact on the server is fairly limited.

Definitely be careful with this sort of thing though :slight_smile:

const usernameQuery = new Parse.Query("_User").matches("username", term, "i");
const nameQuery = new Parse.Query("_User").matches("name", term, "i");
const surnameQuery = new Parse.Query("_User").matches("surname", term, "i");
const emailQuery = new Parse.Query("_User").matches("email", term, "i");

const users = await Parse.Query.or(
  usernameQuery, nameQuery, surnameQuery, emailQuery
).find({ useMasterKey: true });

We tried with full text search as well and regex but we decided to use Elasticsearch instead which is much more efficient and allow us to search on different field with different rules (names, usernames, emails…)
We still have to try with MongoDB Atlas Search to see if we can do the same without Elasticsearch.