Skip to content

Webmail header search (from:/to:/subject:) uses $regex causing full collection scan on large mailboxes #1001

@MartinCerny-awin

Description

@MartinCerny-awin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingDo not close automatically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions