I have indexes setup on customer schema like below
CustomerSchema.index({ firstName: 1, dob: 1 });
CustomerSchema.index({ firstName: 1, lastName: 1, dob: 1 });
CustomerSchema.index({
firstName: 'text',
lastName: 'text',
primaryPhone: 'text',
primaryEmail: 'text',
masterId: 'text',
'sourceIds.id': 'text',
});
CustomerSchema.index({ lastName: 1, dob: 1 });
CustomerSchema.index({ 'phones.value': 1, 'phones.type': 1 });
export const Customers = mongoose.model('customers', CustomerSchema);
I have a search functionality in the frontend and this is what we are doing for the search functionality in the backend , we are using node and mongoose
if (searchQuery) {
q.push({
$match: {
$or: [
{ primaryPhone: { $regex: searchQuery, $options: 'i' } },
{ primaryEmail: { $regex: searchQuery, $options: 'i' } },
{ 'sourceIds.id': { $regex: searchQuery, $options: 'i' } },
{ firstName: { $regex: searchQuery, $options: 'i' } },
{ lastName: { $regex: searchQuery, $options: 'i' } },
{ masterId: { $regex: searchQuery, $options: 'i' } },
],
},
});
}
Couple of issues with the above code
- It’s using regex so performance is slow
- when the search query is “Karthik surya” it does not match the records where firstName is “Karthik”
so for the reasons mentioned above I decided to use text search and changed the code to below
if (searchQuery) {
q.push({
$match: {
$text: {
$search: searchQuery,
$path: [
'primaryPhone',
'primaryEmail',
'sourceIds.id',
'firstName',
'lastName',
'masterId',
],
},
},
});
}
so when the query is executed I am getting the following error
MongoServerError: extra fields in $text
at Connection.onMessage (C:guardian-groupc360-customersnode_modulesmongoosenode_modulesmongodbsrccmapconnection.ts:421:18)
at MessageStream.<anonymous> (C:guardian-groupc360-customersnode_modulesmongoosenode_modulesmongodbsrccmapconnection.ts:251:56)
what am i doing wrong ? and is there a better way to achieve this? should i use a search index provided by atlas?