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.
- is the practice of using regex in a query like that really bad?
- 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”
}
}
}
}