Replacing native regex query with Atlas Search

We have a query in our app that uses native regex in the query like so

const query = { username: { $regex: “^” + req.params.username, $options: “i” } }

as i understand this is not efficient as collection grows, so we are looking to use Atlas search.
Created and infex and search seems ok, but the behaviour is not same as previously where when searching for example “ale” does not return “aleksey” where previously it would return all the users with that prefix.

  1. is the practice of using regex in a query like that really bad?
  2. can we expect search to match behaviour of previous query?

using this search index and query:

const searchQuery = [
{
$search: {
index: ‘users’,
autocomplete: {
query: ${req.params.username},
path: ‘username’,
tokenOrder: “sequential”
}
}
},
{ $limit: limit },
{ $skip: skip },
{ $project: { _id: 1, username: 1 } }

search index:

{
“mappings”: {
“dynamic”: false,
“fields”: {
“username”: {
“analyzer”: “lucene.standard”,
“foldDiacritics”: true,
“maxGrams”: 15,
“minGrams”: 2,
“tokenization”: “edgeGram”,
“type”: “autocomplete”
}
}
}
}

First, you’ll want your $skip before $limit!

The autocomplete field type is tricky to get right. One option is to use ngramming (via a custom analyzer configuration) to achieve this in a more explicit way without the black box oddities that autocomplete can present.

The techniques from the github repo and this solution article can help: Relevant As-You-Type Suggestions Search Solution | MongoDB

Also, it is possible to use regex via Atlas Search too - and that can be a fine compromise between fine grained ngram indexing versus b-tree regeex of a the whole string versus tokens. Regex performance will depend on the scale of strings it must process.

Thanks for the tips !
Somehow unable to open the article, stuck in a login loop (takes me to altlas panel after logging in)

I tried regex with Atlas search with no results. Will try again and post the code.