Search result sorting #509

Closed
opened 2026-06-06 21:27:47 +00:00 by guettli · 2 comments
guettli commented 2026-06-06 21:27:47 +00:00 (Migrated from codeberg.org)

Sorting of search result is wrong. See attached screenshot

sharedinbox.de

Property Value
App Version 0.1.1+1780774971
Git Commit 65173d3
Platform android
Android Version S1RXS32.50-13-25
Device Model motorola / moto g200 5G
Resolution 1080x2393 px (logical: 387x858 pt, ratio: 2.8x)
Dart Version 3.12.0
Processors 8
Dark Mode yes
Locale en_US
Text Scale 1.1×
DB Schema Version 40
IMAP Accounts 1
JMAP Accounts 0
Sorting of search result is wrong. See attached screenshot ## [sharedinbox.de](https://sharedinbox.de) | Property | Value | |----------|-------| | App Version | [0.1.1+1780774971](https://codeberg.org/guettli/sharedinbox/commit/65173d3) | | Git Commit | [65173d3](https://codeberg.org/guettli/sharedinbox/commit/65173d3) | | Platform | android | | Android Version | S1RXS32.50-13-25 | | Device Model | motorola / moto g200 5G | | Resolution | 1080x2393 px (logical: 387x858 pt, ratio: 2.8x) | | Dart Version | 3.12.0 | | Processors | 8 | | Dark Mode | yes | | Locale | en_US | | Text Scale | 1.1× | | DB Schema Version | 40 | | IMAP Accounts | 1 | | JMAP Accounts | 0 |
guettlibot commented 2026-06-06 21:35:04 +00:00 (Migrated from codeberg.org)

Here is the implementation plan I'll post as a comment:


Implementation Plan

Root cause: Both searchEmails() (folder-view) and searchEmailsGlobal() sort by FTS5 rank (BM25 relevance score) instead of received_at DESC. Every other query method in the repository — getEmailsByAddress(), searchAddresses() — already sorts newest-first. Users see relevance-ranked results instead of the expected chronological order.

Fix: Two one-line SQL changes in lib/data/repositories/email_repository_impl.dart:

  1. searchEmailsGlobal (~line 2927):

    -- before
    ORDER BY rank LIMIT 50
    -- after
    ORDER BY e.received_at DESC LIMIT 50
    
  2. searchEmails (~line 3060):

    -- before
    ORDER BY rank LIMIT 50
    -- after
    ORDER BY e.received_at DESC LIMIT 50
    

No schema changes, no migration needed — emails.received_at already exists and is indexed.

Testing:

  • Search for a term that appears in both old and new emails; verify newest appear first.
  • Search with a term matching only one email; verify it still appears (no regression from removing rank).
  • Run existing search-related widget/unit tests.
Here is the implementation plan I'll post as a comment: --- ## Implementation Plan **Root cause:** Both `searchEmails()` (folder-view) and `searchEmailsGlobal()` sort by FTS5 `rank` (BM25 relevance score) instead of `received_at DESC`. Every other query method in the repository — `getEmailsByAddress()`, `searchAddresses()` — already sorts newest-first. Users see relevance-ranked results instead of the expected chronological order. **Fix:** Two one-line SQL changes in `lib/data/repositories/email_repository_impl.dart`: 1. **`searchEmailsGlobal`** (~line 2927): ```sql -- before ORDER BY rank LIMIT 50 -- after ORDER BY e.received_at DESC LIMIT 50 ``` 2. **`searchEmails`** (~line 3060): ```sql -- before ORDER BY rank LIMIT 50 -- after ORDER BY e.received_at DESC LIMIT 50 ``` No schema changes, no migration needed — `emails.received_at` already exists and is indexed. **Testing:** - Search for a term that appears in both old and new emails; verify newest appear first. - Search with a term matching only one email; verify it still appears (no regression from removing `rank`). - Run existing search-related widget/unit tests.
guettlibot commented 2026-06-07 00:57:56 +00:00 (Migrated from codeberg.org)

Auto-merge API returned 422 — CI may need to pass first or the feature needs to be enabled differently. The PR is open and CI will validate it.

Here's a summary of what was done:


What I did

Root cause: Both searchEmailsGlobal and searchEmails used ORDER BY rank (FTS5 relevance score) to sort results, while the rest of the app sorts by received_at DESC (newest first). After merging FTS results with note-search results, the combined list was also never re-sorted.

Fix in lib/data/repositories/email_repository_impl.dart:

  • Changed ORDER BY rankORDER BY e.received_at DESC in the SQL for both searchEmailsGlobal and searchEmails
  • Added merged.sort((a, b) => b.receivedAt.compareTo(a.receivedAt)) after deduplication so the final merged list (FTS + note matches) is always newest-first

Tests added in test/unit/email_repository_impl_test.dart:

  • searchEmailsGlobal returns results sorted by receivedAt descending
  • searchEmails returns results sorted by receivedAt descending

Pull request: https://codeberg.org/guettli/sharedinbox/pulls/520

Auto-merge API returned 422 — CI may need to pass first or the feature needs to be enabled differently. The PR is open and CI will validate it. Here's a summary of what was done: --- ## What I did **Root cause:** Both `searchEmailsGlobal` and `searchEmails` used `ORDER BY rank` (FTS5 relevance score) to sort results, while the rest of the app sorts by `received_at DESC` (newest first). After merging FTS results with note-search results, the combined list was also never re-sorted. **Fix in `lib/data/repositories/email_repository_impl.dart`:** - Changed `ORDER BY rank` → `ORDER BY e.received_at DESC` in the SQL for both `searchEmailsGlobal` and `searchEmails` - Added `merged.sort((a, b) => b.receivedAt.compareTo(a.receivedAt))` after deduplication so the final merged list (FTS + note matches) is always newest-first **Tests added in `test/unit/email_repository_impl_test.dart`:** - `searchEmailsGlobal returns results sorted by receivedAt descending` - `searchEmails returns results sorted by receivedAt descending` **Pull request:** https://codeberg.org/guettli/sharedinbox/pulls/520
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: guettli/sharedinbox#509