When searching via the webmail UI with field-specific queries like from:user@example.com, the search takes 30+ seconds for users with 100K+ messages. The root cause is that prepare-search-filter.js and search-query.js both use $regex with $elemMatch on the headers array, which results in a full collection scan regardless of indexes.
The webmail sends GET /users/{userId}/search?from=user@example.com, which generates:
db.messages.aggregate([
{ $match: {
user: ObjectId("..."),
$and: [{
headers: { $elemMatch: {
key: "from",
value: { $regex: "user@example\\.com", $options: "i" }
}}
}]
}},
{ $group: { _id: 1, n: { $sum: 1 } } }
])
on 180k messages this takes 30s
I think this could be solved by using $text search instead.
When searching via the webmail UI with field-specific queries like from:user@example.com, the search takes 30+ seconds for users with 100K+ messages. The root cause is that prepare-search-filter.js and search-query.js both use $regex with $elemMatch on the headers array, which results in a full collection scan regardless of indexes.
The webmail sends GET /users/{userId}/search?from=user@example.com, which generates:
on 180k messages this takes 30s
I think this could be solved by using $text search instead.